PHPpost中未定义的索引

我拿一个表单并在jquery中进行变量检查然后将其传递给ajax中的php文件,但我收到了这个通知

注意:未定义的索引:第3行的C:\ xampp \ htdocs \ process.php中的your_name这里有问题注意:未定义的索引:第7行的C:\ xampp \ htdocs \ process.php中的your_email

这是我的jquery代码

$(".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"); }, 

这是我的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 "
"; } 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 "
"; } 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 "
"; }

我不知道为什么我收到这个通知显然我的$ _POST值没有设置。
我对这个问题最终有所了解。 你知道为什么/什么时候没有设置$ _POST?

您首先获得your_name的值,然后检查它是否存在

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

做类似以下的事情

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

如果未发布“your_name”,则无法将$ _POST [‘your_name’]分配给变量$ your_name,因为它不存在..您必须首先检查它是否存在,然后将其分配给变量..代码应如下所示:

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

没有看到你的js代码中的任何问题,但你的PHP代码有lil缺陷

   

注意:应该正确完成PHPerror handling,或者您可以禁用php警告