我試圖通過使用Ajax的下拉檢索數據& PHP。 選擇選項值在與數據庫列名配對時工作正常,但當我將其更改爲日期特定時,它不起作用。 請通過我的代碼&建議所需的更改。基於日期檢索數據的Ajax PHP查詢
HTML:
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">July</option> // I doesn't work if when I change the value to "\'2015-07-01\' AND \'2015-07-31\'"
<option value="2">August</option> // I doesn't work if when I change the value to "\'2015-08-01\' AND \'2015-08-31\'"
<option value="3">September</option> // I doesn't work if when I change the value to "\'2015-09-01\' AND \'2015-09-30\'"
<option value="4">October</option> // I doesn't work if when I change the value to "\'2015-10-01\' AND \'2015-10-31\'"
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>
的Javascript
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
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("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","getleads.php?q="+str,true);
xmlhttp.send();
}
}
PHP:
<?php
$q = intval($_GET['q']);
/*Comments Start, I even tried if else with the select values being only numeric
if($q==1)
{
$q == "\'2015-07-01\' AND \'2015-07-31\'";
}
elseif ($q==2)
{
$q == "\'2015-08-01\' AND \'2015-08-31\'";
}
elseif ($q==3)
{
$q == "\'2015-09-01\' AND \'2015-09-30\'";
}
} elseif ($q==4){
$q == "\'2015-10-01\' AND \'2015-10-31\'";
} else ($q==)
{
$q == "*";
}
Comments End*/
$con = mysqli_connect('localhost','user','password','database');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM table WHERE ID = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>Name</th>
<th>Number</th>
<th>Email</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['frm_name'] . "</td>";
echo "<td>" . $row['frm_mobile'] . "</td>";
echo "<td>" . $row['frm_email'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
我甚至通過去除背部試圖從斜線的日期。 請幫忙!!