在Ajax中,如何通过脚本代码获取2个输入值?

时间:2014-03-01 05:56:29

标签: javascript php ajax

我正在制作2个输入值以详细搜索我的数据库。 但是在ajax中,脚本代码擅长1输入值,但在2输入值时很差。 所以我给你看了我的代码,如下所示。 首先,我的HTML代码和脚本代码。

<script>
function showUser1(str)
{
----
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint10").innerHTML=xmlhttp.responseText;
 }
}
var lang20 = document.getElementById('lang20').value;

xmlhttp.open("GET","./database/db1_" +lang20 + ".php?q="+qstr+ "&p="+pstr,true);
xmlhttp.send();
}
</script>

<form>
 <select name="lang20" id="lang20" title="choose the language you want">
    <option value="co">한국어</option>
    <option value="en">English</option>    
    <option value="af">Afrikaans</option>
 </select>
 Input: <input name="search" onkeyup="showUser1(this.value)" >
 Input: <input name="search" onkeyup="showUser1(this.value)" >
</form>
<div id="txtHint10"><b>Disease information will be listed here.</b></div>

其次,我的db1_co.php代码如下所示。

<?php
$q = htmlspecialchars($_GET['q']);
$p = htmlspecialchars($_GET['p']);

$con = mysqli_connect('localhost',---);
if (!$con)
 {
 ---
 }

 mysqli_select_db($con,"ajax_demo");
 $sql = "SELECT code_co.code, code_co.disease_co, note.note, inclusion.inclusion,  

 advertiser.drug, subject.subject, subject.icd_category FROM code_co left join subject 

 on subject.code=code_co.code left JOIN note ON code_co.code = note.code left JOIN 

 inclusion ON code_co.code = inclusion.code left JOIN advertiser ON code_co.code = 

 advertiser.code WHERE code_co.disease_co LIKE '%".$q."%' and code_co.disease_co LIKE 

 '%".$p."%' OR code_co.code like '%".$q."%' and code_co.code like '%".$p."%'" ;

上面,我猜我的剧本可能是错的。也就是说,'php?q =“+ qstr +”&amp; p =“+ pstr,true);'是错的。 请帮我。 请给我一点建议。 谢谢你的关注。

2 个答案:

答案 0 :(得分:1)

更改其他输入的名称,例如name="search2"和giv ID。

<input name="search" id="srch1"  onkeyup="showUser1(this.value)" />
<input name="search2" id="srch2"  onkeyup="showUser1(this.value)" />


var lang20 = document.getElementById('lang20').value;
var srch1 = document.getElementById('srch1').value;
var srch2 = document.getElementById('srch2').value;
xmlhttp.open("GET","./database/db1_" +lang20 + ".php?srch1="+srch1+ "&srch2="+srch2,true);
xmlhttp.send();

然后在PHP中获取值,

  $srch1 = $_GET['srch1'];
  $srch2 = $_GET['srch2'];

答案 1 :(得分:1)

你可以试试这个

<强> SCRIPT:

<script>
function showUser1() // call function without parameter
{
  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("txtHint10").innerHTML=xmlhttp.responseText;
     }
  }
var lang20 = document.getElementById('lang20').value;
var qstr = document.getElementById('qstr').value; // get value of qstr
var pstr = document.getElementById('pstr').value; // get value of pstr 



  xmlhttp.open("GET","./database/db1_" +lang20 + ".php?q="+qstr+ "&p="+pstr,true);
    xmlhttp.send();
    }
   </script>

HTML:

<form>
 <select name="lang20" id="lang20" title="choose the language you want">
    <option value="co">한국어</option>
    <option value="en">English</option>    
    <option value="af">Afrikaans</option>
 </select>
 Input: <input name="search" id="qstr" onkeyup="showUser1()" > <!-- give id to qstr and call function withoud this.value -->
 Input: <input name="search" id="pstr" onkeyup="showUser1()" > <!-- give id to pstr and call function withoud this.value -->
</form>
<div id="txtHint10"><b>Disease information will be listed here.</b></div>