dongluo1853 2014-05-18 23:50
浏览 49
已采纳

通过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"];
  • 写回答

1条回答 默认 最新

  • dongsaohu6429 2014-05-19 00:32
    关注

    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"];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?