简体   繁体   中英

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

The HTML

<html>
<head>
<script>
var xmlhttp;

function showUser(str,age)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="getuser.php";
url=url+"?q="+str+"&a="+age+"&c="+c;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}

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;
}
</script>
</head>
<body>

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

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>
</select>

<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'))">
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

and PHP

<?php
$q=$_GET["q"];
$a=$_GET["a"];
$c=$_GET["c"];

$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'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
</tr>";

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>";

mysql_close($con);
?>

How can i add the date picker to the search, for the moment it works only with two <select> 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

您需要将c变量添加为showuser函数的参数。

function showUser(str,age, c)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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