2012-07-12 140 views
0

我正在使用PHP發送AJAX請求。在表單方法中我使用下面的代碼。無法發送PHP上的AJAX請求

rohit1.php

<form action="index1.php" onsubmit="callfunc()"> 
<span id="raj"></span> 
<input type="submit" value="submit"></div> 

</FORM> 

在javascript I M使用: callfun.js

function callfunc() 
{ 

var http =new XMLHttpRequest(); 
    var name=prompt('enter name of the form'); 
    var name="rohit"; 
    var url = "index1.php"; 
    var s = document.getElementById('raj').innerHTML; 
    alert(http); 
    http.open("POST", "index1.php?name="+name, true); 

    http.onreadystatechange = function() 
    { 
     if(http.readyState == 4 && http.status == 200) 
     { 


     } 




    } 

    http.send(); 
} 

當我使用該功能。然後提交這是重定向在index.php?label =而不是index.php?name =我該怎麼做這個?

+0

只是試圖重定向到新的一頁。或者你想要做一些其他 – Rab 2012-07-12 07:21:16

+1

window.location.href =「index1.php?name =」+ name;將爲你工作然後 – Rab 2012-07-12 07:28:01

回答

0

只需在callfunc()函數的末尾添加return false即可。它會阻止表單的默認事件,並且不會作爲標準表單。

+0

什麼都沒有happennd .... :( – 2012-07-12 07:17:09

+0

是的,目測沒有什麼應該發生,如果你想發送一個ajax請求到服務器,你必須處理'if(http.readyState == 4 && http.status == 200)'陳述,你寫了。 – haynar 2012-07-12 07:20:22

+0

,我沒有看到任何包含單詞「label」的代碼,我猜這個問題可能是你的代碼中的某個地方出現了一個錯字? – haynar 2012-07-12 07:21:57

0
<form action="index1.php" onsubmit="callfunc()"> 
    <span id="raj"></span> 
    <input type="submit" value="submit"></div> 
</form> 

和相應的JavaScript看起來像這樣:

function callfunc() { 
    var name = "some name here"; // get this name from whereever you want 
    window.location.href = "index1.php?name="+name; 
    return false; 
} 

這應該爲你工作

+0

我也試過這一個,但它得到相同的迴應 – 2012-07-12 08:41:03

+0

我已經刪除標籤的名稱,但這也不值得... – 2012-07-12 09:00:13