2012-04-24 117 views
0

我采用这样的形式,做变量检查jQuery的,然后把它传递到AJAX的PHP文件,但我收到此通知未定义指数

注意:未定义指数:your_name在C:\ XAMPP \ htdocs中\在线3 东西process.php是错在这里 注意:未定义指数:your_email在C:\ XAMPP \ htdocs中\ process.php线路7

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

我不知道为什么我收到此通知 显然我的$ _POST值没有设置。
我以我的智慧结束了这一次。你知道为什么/何时没有设置$ _POST?

+7

'var_dump($ _ POST);'---总是使用'var_dump()'来查看实际变量中的内容。 *知识* - 这是什么程序员不同于算命先生 – zerkms 2012-04-24 07:36:25

+0

谢谢@zerkms值是NULL :-(我很困惑,为什么 – user1154295 2012-04-24 11:53:23

回答

2

你是第一个让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 
} 
+0

谢谢@哈桑汗我的后期变量不存在,当我做一个var_dump它的全部内容都是空的,我在这里以我的智慧结束,你是否知道为什么$ _POST索引不存在? – user1154295 2012-04-24 11:50:20

+0

它不存在,因为它可能没有被提交?检查发布这些变量的表单。确保名字相同 – 2012-04-24 12:10:32

0

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

<?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"]; 
} 

注:PHP错误处理应该处理得当,也可以禁用PHP的警告

1

$ _POST ['your_name']不能被分配给变量$ your_name当“your_name”没有发布,因为它不存在..你必须检查它是否存在,然后分配它的变量..代码应该是这样的:

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

谢谢@艾哈迈德穆罕默德:-)你明白为什么它不存在吗? – user1154295 2012-04-24 11:54:26

+0

当你提交表单..如果'your_name'没有提交,那么它不会被添加到$ _POST数组 – 2012-04-25 09:50:21