1

I am making the 2 input values to search my database in detail. But in ajax, the script code is good at 1 input value, but poor at 2 input values. So I have show you my codes as like below. First, My html code and script code.

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

<?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);' is wrong. Please help me. Give me a piece of advice, please. Thank you for your concern.

4
  • have you defined qstr and pstr in your js Commented Mar 1, 2014 at 5:59
  • no.. my script code is .. <script> function showUser1(str) { if (str=="") { document.getElementById("txtHint10").innerHTML=""; return; } 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; Commented Mar 1, 2014 at 6:03
  • please check my answer. and try it Commented Mar 1, 2014 at 6:06
  • Thank you!! YOUR code is perfect and amazing ... Commented Mar 1, 2014 at 6:21

2 Answers 2

1

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 :

<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>
Sign up to request clarification or add additional context in comments.

Comments

1

Change name of other input, for example name="search2" and giv ID to them too.

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

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

1 Comment

2 input names is different?? that is as like search1, search2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.