(I will just try to show the relevant parts)
I am trying to follow the Ajax example given here:
https://www.w3schools.com/xml/tryit.asp?filename=tryajax_database
https://www.w3schools.com/xml/ajax_database.asp
This is the table I am using right now:
My interface, in summary my $rowAddress gets the column from my table (merchant_address)
<div class="row">
<div class="col-md-12">
<select name="province-list" onchange="showAddress(this.value)">
<?php
foreach($lstAddress as $rowAddress) {
echo '<option value="'.$rowAddress['city'].'">'.$rowAddress['province'].' - '.$rowAddress['city'].'</option>';
}
?>
</select>
</div>
<div class="col-md-12">
<div id="city_address">Clinic Address Goes Here</div>
</div>
</div>
My for each loop is already doing its job at displaying the options available (inspect element):
Im trying to display the addresses for each city:
ajax-list-address.php
<?php
$routePath = "../";
require_once($routePath . "_config/db.php");
$dbConfig = new config_db();
$db = $dbConfig->init();
$display_address = $db->prepare("SELECT clinic_address FROM merchant_address WHERE city = ?");
$stmt = $db->prepare($display_address);
$stmt->bind_param("s", $_GET['q']);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($clinic_address);
$stmt->fetch();
$stmt->close();
echo "<p>".$clinic_address."</p>"
?>
my ajax code:
<script>
function showAddress(str) {
var xhttp;
if (str == "") {
document.getElementById("city_address").innerHTML = "";
return;
}
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("city_address").innerHTML = this.responseText;
}
};
xhttp.open("GET", "ajax-list-address.php?q="+str, true);
xhttp.send();
}
</script>
I get the error shown below when I select something from the dropdown. Would like to know what I am missing, already using "city_address" and "showAddress" in my ajax javascript.
Warning: PDO::prepare() expects parameter 1 to be string, object given in C:\xampp\htdocs\mwc_j\mwc_j\cards\ajax-list-address.php on line 11
Fatal error: Uncaught Error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\mwc_j\mwc_j\cards\ajax-list-address.php:12 Stack trace: #0 {main} thrown in C:\xampp\htdocs\mwc_j\mwc_j\cards\ajax-list-address.php on line 12
line 12 is: $stmt->bind_param("s", $_GET['q']);
UPDATE: after calling prepare() only once, I now get this error:
Fatal error: Uncaught Error: Call to undefined method PDOStatement::bind_param() in C:\xampp\htdocs\mwc_j\mwc_j\cards\ajax-list-address.php:11 Stack trace: #0 {main} thrown in C:\xampp\htdocs\mwc_j\mwc_j\cards\ajax-list-address.php on line 11
line 11 is still the same as the one from the top: $stmt->bind_param("s", $_GET['q']);