2012-04-24 115 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