I learn about the script autodidact and only PHP and Mysql and i don't know how to tell the things about the name part of script or syntrac or anything i just know how to use it and hard to for me find my question from goolge, please understand me, sorry.
There is posible to filter 2 field ‘firstname’ and ‘year’ from 1 database?
Condition 1 : When i type firstname by input only, it work, start from refresh first if not go to condition 3.
Condition 2 : When i select year by select option only, it work, start from refresf first if not go to condition 3.
Condition 3 : When i select year and type by input, it doesn’t work.
What i need is, when i select year 2009 and than when i just type ‘a’ Abdul and Agus selected from database, should only abdul selected from database.
I think the problem is my PHP and MYsql so i focus on them because i just know about them and forget about javascript.
Here my database ‘directory’ :
| firstname | status | year |
|:——— |:——:| :—-:|
| Agus | Alumni | 2008 |
| Abdul | Alumni | 2009 |
| David | Alumni | 2009 |
| Jelita | Student| |
index.php
<html>
<head>
<script>
function sortResult(str)
{
if (str=="")
{
document.getElementById("result").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("result").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","results.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="sortby" onchange="sortResult(this.value)">
<option>Tahun</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
</select>
<input name="sortby" onKeyUp="sortResult(this.value)" type="text">
</form>
<br>
<div id="result"><b>Results will be listed here.</b></div>
</body>
</html>
results.php
<?php
$q = $_GET['q'];
$con = mysqli_connect('localhost','root','','school');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"database");
$sql="SELECT * FROM directory where
status='Alumni' and tahun Like '%".$q."%' or
status='Alumni' and firstname Like '%".$q."%'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Fistname</th>
<th>Title</th>
<th>Graduation</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" .$row['noid']. "</td>";
echo "<td>" .$row['firstname']. "</td>";
echo "<td>" .$row['status']. "</td>";
echo "<td>" .$row['tahun']. "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>