Undefined index in PHP post

∥☆過路亽.° 提交于 2019-12-20 07:19:21

问题


I take a form and do variable checking in jquery then pass it to a php file in ajax but I am getting this notice

Notice: Undefined index: your_name in C:\xampp\htdocs\process.php on line 3 something is wrong here Notice: Undefined index: your_email in C:\xampp\htdocs\process.php on line 7

Here is my jquery code here

        $(".button").click(function(){
    $('.error').hide(); 
    var your_email=$("input#your_email").val(); 
    if(your_email ==""){
        $("label#youremail_error").show();  
        $("input#your_email").focus(); 
        return false; 
    }
    var your_name=$("input#your_name").val(); 
    if(your_name==""){
        $("label#yourname_error").show();
        $("input#your_name").focus(); 
        return false; 
    }
    var friends_email=$("input#friends_email").val(); 
    if(friends_email==""){
        $("label#friendsemail_error").show(); 
        $("input#friends_email").focus(); 
        return false; 
        }
    var friends_name=$("input#friends_name").val(); 
    if(friends_email==""){
        $("label#friendsname_error").show(); 
        $("input#friends_name").focus(); 
        return false;
        }
        var dataString = 'your_email=' + your_email + '&friends_email=' + friends_email + '&your_name=' + your_name + '&friends_name=' + friends_name; 
        //alert(dataString); 
        $.ajax({
        type: "POST", 
        url:"process.php",
        data: dataString, 
        success: function(ret) {
            alert(ret); 
            //alert("thank you for signing up"); 
        },

and here is my PHP

   <?php
include 'inc/class.phpmailer.php';
if(isset($_POST['your_name'])){
    $your_name=$_POST['your_name'];
    }
else{
    echo "something is wrong with your name having:";
    var_dump($_POST['your_name']);
    echo "<br/>"; 
    }
if(isset($_POST['your_email'])){
    $your_email=$_POST['your_email']; 
}
else{
    echo "something is wrong with your email having:";
    var_dump($_POST['your_email']); 
    echo "<br/>"; 
    }
if(isset($_POST['friends_name'])){
    $friends_name=$_POST['friends_name']; 
    }
else{
    echo "something is wrong with friends name having:"; 
    var_dump($_POST['friends_name']);
    echo "<br/>"; 
    }

I am not sure why I am getting this notice Apparently my $_POST values are not set.
I am at my wits end with this one. Do you know why/when a $_POST is not set?


回答1:


You are getting value of your_name first and then checking if it exists

$your_name=$_POST["your_name"];  <--- this line should be inside an if isset
if(!isset($_POST['your_name'])){

Do something like the following

if (isset($_POST['your_name'])) {
   $your_name = $_POST['your_name'];
   // do whatever here
}



回答2:


$_POST['your_name'] cannot be assigned to the variable $your_name when the "your_name" isn't posted cause it doesn't exist .. you have to check if it exists first and then assign it to the variable .. the code should be like the following :

if (isset($_POST['your_name'])) {
   $your_name = $_POST['your_name'];
}
else { 
   echo "something is wrong here";
}



回答3:


not seen any issue in your js code but your php code have lil flaw

 <?php
include 'inc/class.phpmailer.php';

//it can be like that or
if(!isset($_POST['your_name'])){
    echo "something is wrong here"; 
}
else{
    $your_name=$_POST["your_name"]; 
}

Note: PHP Error handling should be properly done or you can disable the php warnings



来源:https://stackoverflow.com/questions/10293644/undefined-index-in-php-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!