2012-03-06 34 views
0

數據在jQuery中傳遞良好,但是當試圖將它傳遞給PHP時,它開始竊聽並且一切都爲空。我用的警報框,它似乎像數據和值存儲,然後我嘗試使用Ajax將它傳遞給我有一個PHP文件,然後我做了var_dump來測試變量,它是NULL。有人可以幫幫我嗎?問題與jQuery的PHP

請幫忙。

jQuery代碼:

  var dataString = 'Billing_Name=' + Billing_Name + '&Billing_Email=' + Billing_Email + '&Billing_Card=' + Billing_Card + '&Month=' + Month + '&Year=' + Year + '&Billing_StreetAddress1=' + Billing_StreetAddress1 + 'Billing_StreetAddress2=' + Billing_StreetAddress2 + '&Billing_City=' + Billing_City + '&Billing_State=' + Billing_State + '&Billing_Zip=' + Billing_Zip + '&Billing_Phone=' + Billing_Phone; 
    alert(dataString); 

    $.ajax({ 
     //cache: false, 
     type: "POST", 
     url: "test.php", 
     data: dataString, 
     // dataType: ($.browser.msie) ? "text" : "xml", 
    success: function(ret) { 
     alert(ret); 
     //alert("it has been sent"); 
     //$.get("https://affordableapparelny.com/ssl/checkoutprocess.php",dataString), 
     alert("it was sent"); 
     window.location.href="http://affordableapparelnewyork.com/ssl/test.php"; 
       }, 
    error: function(xhr, type, exception){ 
          alert("something went wront here"); 
          alert("Error: " + type); 
          console.log('@Error: '+errorThrown); 
          console.log('@Status: '+status); 
          console.log('@Status Text: '+xhr.statusText); 
          } 
    }); 

這裏是PHP代碼

<?php 

    $BName=$_POST['Billing_Name']; 
$BEmail=$_POST['Billing_Email']; 
$BCard=$_POST['Billing_Card']; 
var_dump($BName); 

$errormessages=array(); 
$errorcount=0; 
if(isset($_POST) && is_array($_POST) && count($_POST)){ 
    foreach ($_POST as $key=>$value){ 
    $$key=$value; $$key=trim($$key); 
    $$key=strip_tags ($$key); 
    $$key=stripslashes($$key); 
    $$key=htmlentities($$key); 
    echo "$key $value <br>"; 
    // echo "is it working"; 
} 
} 
else { $errormessages[]="Post variable not set"; } 
+0

你在哪裏定義進入'dataString'的值以及如何獲得這些值?這就是錯誤應該發生的地方。 – 2012-03-06 21:05:37

+0

var Billing_Name = $(「input#Billing_Name」)。val(); – user1154295 2012-03-06 21:07:18

+0

對於每個變量 – user1154295 2012-03-06 21:07:34

回答

1

也許你只是缺少一個符號&這裏:

Billing_StreetAddress1 + 'Billing_StreetAddress2=' 

要在客戶端看到您的錯誤,請在服務器端使用開發人員控制檯,只需轉儲整個$ _POST變量即可查看您收到的內容。

以及最後但並非最不重要的,檢查出發送數據的這種方式:

var dataObj = { 
    'Billing_Name' : $('#Billing_Name').val(), 
    'Billing_Email' : $('#Billing_Email').val() 
}; 

$.ajax({type: "POST", url: "test.php", data: dataObj, success: ... 
+0

比你這麼多的捕捉,我修好了,但變量仍然爲空:-( – user1154295 2012-03-06 21:10:52

+0

可悲它仍然爲空:-( – user1154295 2012-03-06 21:22:32

+0

你需要顯示更多的代碼,但減少它顯示html結構和整個js函數定義(帶簽名和用法,但不檢查單個參數) – mindandmedia 2012-03-06 21:33:54

0

嘗試發送數據作爲JSON作爲@mindandmedia指出,也許你的環境還沒有設置接受內容類型application/x-www-form-urlencoded