mjeshtri mjeshtri - 2 years ago 98
Javascript Question

How to add extra filter options to an Ajax PHP search using mysql

Based on this example http://www.w3schools.com/php/php_ajax_database.asp
i have made some search and found this sample which looks what i need


var xmlhttp;

function showUser(str,age)
if (xmlhttp==null)
alert ("Browser does not support HTTP Request");
var url="getuser.php";

function stateChanged()
if (xmlhttp.readyState==4)

function GetXmlHttpObject()
if (window.XMLHttpRequest)
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
if (window.ActiveXObject)
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
return null;

Select a User:
<select name="users" id="users">
<option value="1">Marko</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>

Select an age:
<select name="age" id="age">
<option value="1">1</option>
<option value="40">40</option>
<option value="41">41</option>
<option value="42">42</option>

<input type="text" name="c" id="c" value="2016-06-01">

<input type='button' value='Refresh Data' onclick="showUser(document.getElementById('users').value,document.getElementById('age').value,document.getElementById('c'))">
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>


and PHP


$con = mysql_connect('localhost', 'username', 'password');
if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db("databasename", $con);

$sql="SELECT * FROM data WHERE id = '".$q."' and website = '".$a."' and date = '".$c."'";

$result = mysql_query($sql);

echo "<table border='1'>

while($row = mysql_fetch_array($result))
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['imp'] . "</td>";
echo "<td>" . $row['rev'] . "</td>";
echo "<td>" . $row['cpm'] . "</td>";
echo "</tr>";
echo "</table>";


How can i add the date picker to the search, for the moment it works only with two
but i want to be able to add a Start date & End date
I tried adding
<input type="text" name="c" id="c" value="2016-06-01">
but could not figured out where the mistake is

Answer Source

you need to add the c variable as a parameter of your showuser function.

function showUser(str,age, c)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download