In Ajax, I want to have 2 and more input function. How Can I do this? That is, usually "q" is popular. But I want to add "p" parameter in ajax program. Below is my html-script code.
<form>
<input type="text" name="FirstName" maxlength="20" onkeyup="showUser(this.value)">
<input type="text" name="LastName" maxlength="20" onkeyup="showUser(this.value)">
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
<script>
function showUser(str){
if (str.length==0)
{
document.getElementById("txtHint1").innerHTML="";
return;
}
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint1").innerHTML=xmlhttp.responseText;
}
}
var selectedLang = document.getElementById('lang').value;
xmlhttp.open("GET","ds_hint_"+selectedLang+".php?q="+str,true);
xmlhttp.open("GET","ds_hint_"+selectedLang+".php?p="+str,true);
xmlhttp.send();
}
</script>
In script, "xmlhttp.open("GET","ds_hint_"+selectedLang+".php?p="+str,true);" is added to re-search the first search result by "q" parameter.
So, my php code is below.
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM persons WHERE FirstName = '".$q."' and LastName = '".$p."'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I think PHP code is normal, but, script code is not sufficient for re-search function. How can i solve my script code?? Please, help me.
your query string should be like
xmlhttp.open("GET","ds_hint_"+selectedLang+".php?q="+qstr+"&p="+pstr,true);
and your php
$q=$_GET['q'];
$p=$_GET['p'];
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM persons WHERE FirstName = '".$q."' and LastName = '".$p."'";
though as pointed out this is not filtering for sql injection I personally prefer pdo prepared statemants