简体   繁体   中英

Using AJAX, PHP and MySQL to display table data

I would like to display one column of data, [pin], based on the [plan] and [order_id] values. plan=9 and order_id=0. Would like to load data without reloading page, using ajax.

Here is my HTML/Script:

<script>
function showPins(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","getPins.php?q="+str,true);
xmlhttp.send();
}
}
</script>

HTML:
    <div align="center">
    <h3>View PIN's</h3>
    <form>
    <select name="users" onchange="showPins(this.value)">
      <option value="">Select Plan Type:</option>
      <option value="1">Plan1</option>
      <option value="2">Plan2</option>
      <option value="3">Plan3</option>  
    </select>
    </form>
    <br/>
    <div id="txtHint"></div>
    </div>

This is my PHP file (getPins.php):

<?php
$q = intval($_GET['q']);

$con = mysqli_connect('myHost','myUsername','myPw','my_db');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"my_db");
$sql="SELECT * FROM attPins WHERE (order_id=0, plan=9 and id = '".$q."')";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>PIN's</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";    
    echo "<td>" . $row['pin'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($con);

This is based off the tutorial shown here: http://www.w3schools.com/php/php_ajax_database.asp

Trying to make it work for showing the correct pins for plan type chosen.

your query would be wrong read manual where

  $sql="SELECT * FROM attPins WHERE (order_id=0, plan=9 and id = '".$q."')";

It would be

 WHERE (order_id=0 and plan=9 and id = '".$q."')

Or

WHERE (order_id=0 OR plan=9 and id = '".$q."')

according to your requirment

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