2011-09-06 29 views
0

我正在通過jQuery發送數據。我的等價代碼是...通過jquery發送數據時出現問題

$('#pcpmnum').blur(function(){ 
//alert("HIiiiiii"); 
var pcpmnum = $("#pcpmnum").val(); 
if(pcpmnum === "" | pcpmnum === null) 
    { 
     alert("Please Enter Mobile Number"); 
    } 
else 
    { 
     alert(pcpmnum); 
     $.post("searchpcp.php", {cntctnumber: "+pcpmnum+"}, function(){ 
      alert("Success"); 
        } 
    } 

}); 

我的php文件我簡單地使用過。

echo "HIIII"; 

這個$ .post函數是否相當於Ajax函數?

+0

.post的$是$ .ajax的快捷方式,因此您不必填寫儘可能多的選項。 –

+0

'$ .post'是'$ .ajax'的縮寫,帶有某些預設(http://api.jquery.com/jQuery.post)。從代碼中它應該正確地調用'searchpcp.php',但爲什麼你將'pcpmnum'作爲一個字符串傳遞,而不是傳遞它的值(沒有引號和+ es)? – m90

回答

2

根本就

{cntctnumber: pcpmnum, second:"second variable" } 

,並在PHP文件作爲

$contact = $_POST["cntctnumber"]; // you will get the value of pcpmnum here 
$sec = $_POST["second"]; // you will get "second variable" here 
在成功回叫你的情況的說法 data

包含服務器響應例如,你可以得到價值在你的PHP文件你回聲ing

... 
echo"howdy"; 
在客戶端 data

會拿着這個響應

$.post("searchpcp.php", {cntctnumber: pcpmnum, second:"second variable" }, function(data){ 
      alert(data);//howdy 
      }); 

這裏有一些有用的鏈接

jQuery, Ajax, Json and Php

Returning JSON from PHP to JavaScript?

+0

如果我需要發送2個變量?其次是這相當於發送Ajax請求嗎?這是什麼意思如果我使用'function(data)' –

+0

ill編輯兩個變量的答案,並且是'$ .post'在後端使用ajax調用它只是'$ .ajax'的簡寫。 – Rafay

+0

我想要返回一組數據。並從返回的數據中填充相應的文本字段。假設我有'id = firstname'的輸入,而我的第二個是'id = middlename'。我在我的PHP文件中發射querey。我將如何返回這些數據。 –

相關問題