2014-05-18 40 views
0

我有一個文本形式,它包含一個文本字段以及一個指定的ID號碼,我想將其發送到另一個頁面。這可能嗎?通過AJAX將文本形式的2個值傳遞給另一個頁面

<form name="cForm" id="cForm"> 
      Comment: <input type="textBox" name="cText" id="cText" /> 
      <input type="button" name="submitCreate" id="submitCreate" value="Create" onclick="showCreate('.$row['ID'].')" /><br> 
     </form> 

<script> 
function showCreate(str) { 
    if (str=="") { 
    document.getElementById("showCreate").innerHTML=""; 
    return; 
    } 
    if (window.XMLHttpRequest) { 
    // code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } else { // code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function() { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
     document.getElementById("showCreate").innerHTML=xmlhttp.responseText; 
    } 
    } 
    xmlhttp.open("Post","showCreate.php?q="+str,true); 
    xmlhttp.send(); 
} 
</script> 

我想獲得在不同的變量文本字段和ID的值,例如

$comment = $_REQUEST["q"]; 
$ID = $_REQUEST["p"]; 

回答

1

不知道,我理解你的權利,但試試這個:

<form name="cForm" id="cForm"> 
      Comment: <input type="textBox" name="cText" id="cText" /> 
      <input type="button" name="submitCreate" id="submitCreate" value="Create" onclick="showCreate('.$row['ID'].')" /><br> 
     </form> 

<script> 
    function showCreate(str) { 
     if (str=="") { 
     $("#showCreate").html(""); 
     return; 
     } 
    $.ajax({ 
     type: "POST", 
     url: "showCreate.php", 
     data: "q="+str+"&p="+$("#cText").val() 
     }).done(function(result) { 
      $("#showCreate").html(result); 
     }); 
    } 
</script> 

所以你會能夠使用PHP的變數像你想要的

$comment = $_REQUEST["q"]; 
$ID = $_REQUEST["p"]; 
+0

hi @volkinc我有一個'Uncaught ReferenceError:s沒有被​​定義爲''q =「+ s +」&p =「+ $(」#cText「)。val()'是否需要' s'裏面? – user2691544

+0

對不起。它錯字。 s = str。你傳遞給功能的戰爭 – volkinc

+0

這正是我所需要的。謝謝@volkinc – user2691544

相關問題