通过AJAX将2个值从文本表单传递到另一个页面


Passing 2 values from text form to another page through AJAX

我有一个文本表单,其中包含一个文本字段以及一个指定的ID号码,我想发送到另一个页面。这可能吗?

<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"];

不确定我理解你的权利,但试试这个:

<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"];