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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?