Javascript 在Ajax中,如何通过脚本代码获得2个输入值?
我正在做2个输入值来详细搜索我的数据库。 但在ajax中,脚本代码擅长于1个输入值,但不擅长2个输入值。 所以我给你看我的代码如下。 首先,我的html代码和脚本代码Javascript 在Ajax中,如何通过脚本代码获得2个输入值?,javascript,php,ajax,Javascript,Php,Ajax,我正在做2个输入值来详细搜索我的数据库。 但在ajax中,脚本代码擅长于1个输入值,但不擅长2个输入值。 所以我给你看我的代码如下。 首先,我的html代码和脚本代码 <script> function showUser1(str) { ---- if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtHint10").innerHTML=xmlhttp.respo
<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>
函数showUser1(str)
{
----
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“txtHint10”).innerHTML=xmlhttp.responseText;
}
}
var lang20=document.getElementById('lang20').value;
open(“GET”,”/database/db1_“+lang20+”.php?q=“+qstr+”&p=“+pstr,true);
xmlhttp.send();
}
한국어
英语
南非荷兰语
输入:
输入:
疾病信息将列在这里。
其次,我的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."%'" ;
更改其他输入的名称,例如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();
你可以试试这个
脚本:
<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>
函数showUser1()//调用不带参数的函数
{
if(window.XMLHttpRequest)
{
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{
//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
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;//获取qstr的值
var pstr=document.getElementById('pstr').value;//获取pstr的值
open(“GET”,”/database/db1_“+lang20+”.php?q=“+qstr+”&p=“+pstr,true);
xmlhttp.send();
}
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>
한국어
英语
南非荷兰语
输入:
输入:
疾病信息将列在这里。
您是否在jsno中定义了qstr
和pstr
。。我的脚本代码是。。函数showUser1(str){if(str==“”){document.getElementById(“txtHint10”).innerHTML=“”;return;}if(window.XMLHttpRequest){//IE7+、Firefox、Chrome、Opera、Safari xmlhttp=new XMLHttpRequest();}否则{//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;请检查我的答案。然后再试一次谢谢!!您的代码非常完美而且令人惊讶…两个输入名称不同??就像search1、search2一样
<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>