duankuang1046 2013-06-17 22:09
浏览 63
已采纳

onsubmit表单值到php sql请求

I have many input values from a form, and I would like to push them to PHP code using ajax code. Here's an example of what i'm trying to do. I have test1 and test2 that i want to keep track when user press search button.

Right now it only get the value of test1 "getResults(this.test1.value)" I would like to know how to get the test2 value using the same method.

<form name="input" action="" method="" onsubmit="getResults(this.test1.value); return false;">

  <input type="text" name="test1">

<select id="test2" name="test2">
  <option value="">Make a choice ...</option>
  <option value="c1">choice 1</option>
  <option value="c2">choice 2</option>
  <option value="c3">choice 3</option>
</select>    


<input type="submit" value="Search">

</form>

<div id="here"><b></b></div> 

The getresults method, right now it only support one string, how can I add more arguments ?

 function getResults(str)
 {
 if (str=="")
   {
   document.getElementById("here").innerHTML="";
   return;
   } 
 else (window.XMLHttpRequest)
   {
   xmlhttp=new XMLHttpRequest();
   }
 xmlhttp.onreadystatechange=function()
   {
   if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
     document.getElementById("here").innerHTML=xmlhttp.responseText;
     }
   }
 xmlhttp.open("GET","info.php?q="+str,true);
 xmlhttp.send();
 }

and finaly and more importantly how can I retreive values from info.php ? Thanks a lot

<?php
$test1 = $_GET['test1'];
echo "$test1";

$test2 = $_POST["test2"];
echo "$test2";
?> 
  • 写回答

2条回答 默认 最新

  • doudan4834 2013-06-17 22:39
    关注

    You don't need to pass argument to the function. We can grab the form fields value there.

    <html>
        <head>
        <script type="text/javascript">
            function getResults()
            {
                var xmlhttp;
                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("here").innerHTML=xmlhttp.responseText;
                    }
                }
    
                var data = "test1="+document.getElementById("test1").value+"&test2="+document.getElementById("test2").value;
    
                xmlhttp.open("POST","info.php",true);
                xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                xmlhttp.send(data);
            }
        </script>
        </head>
        <body>
            <form name="input" action="" method="" onsubmit="getResults(); return false;">
    
            <input type="text" name="test1" id="test1">
    
            <select id="test2" name="test2">
                <option value="">Make a choice ...</option>
                <option value="c1">choice 1</option>
                <option value="c2">choice 2</option>
                <option value="c3">choice 3</option>
            </select>    
    
            <input type="submit" value="Search">
    
            </form>
    
            <div id="here"><b></b></div>
        </body>
    </html>
    

    Than in your info.php file do this to grab the variables sent via Ajax,

    <?php
        $test1 = $_POST["test1"];
        $test2 = $_POST["test2"];
    
        echo "Test1: ".$test1."<br/>Test2: ".$test2."";
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名