通过AJAX将2个值从文本形式传递到另一个页面
问题描述:
I have a text form that contains a text field as well as a designated ID number that i would like to send over to another page. Would that be possible ?
<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>
i would like to get the value of the text field and ID in separate variables as such
$comment = $_REQUEST["q"];
$ID = $_REQUEST["p"];
答
not sure that i understand you right but try this:
<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>
so you gonna be able to use vars at php like you wanted
$comment = $_REQUEST["q"];
$ID = $_REQUEST["p"];