简体   繁体   English

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

[英]In Ajax, How can I get 2 input values by the script code?

I am making the 2 input values to search my database in detail. 我正在输入2个输入值来详细搜索数据库。 But in ajax, the script code is good at 1 input value, but poor at 2 input values. 但是在ajax中,脚本代码在1个输入值时好,但在2个输入值时差。 So I have show you my codes as like below. 因此,我向您展示了我的代码,如下所示。 First, My html code and script code. 首先,我的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>

Second, my db1_co.php code is as like below. 其次,我的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."%'" ;

Above, I guess my script may be wrong. 以上,我想我的脚本可能是错误的。 That is, 'php?q="+qstr+ "&p="+pstr,true);' 也就是说,'php?q =“ + qstr +”&p =“ + pstr,true);' is wrong. Please help me. Give me a piece of advice, please. Thank you for your concern. 是错的,请帮助我,请给我一些建议,谢谢您的关注。

Change name of other input, for example name="search2" and giv ID to them too. 也更改其他输入的名称,例如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();

And then in PHP get values as this, 然后在PHP中获取这样的值,

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

you can try this 你可以试试这个

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 : 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM