I am trying to do what should seem like a very basic ajax request. I am sending "id" thru ajax to get the service cost of an item with that "id".
From what I am seeing the ajax is sending the id to my "getcost.php", however it isn't showing the "cost" on the page that made the ajax request.
When I run "getcost.php?id=5" it shows the cost of "$75" so I know that the problem isn't with my database or SQL query. I'm stuck and can't figure out what's wrong.
getcost.php
<?php
$servicequery = $_REQUEST["id"];
$cost = "";
//DEBUG- echo to see if id is passed (Success)
echo $servicequery."<br>";
$serviceSql = "SELECT * FROM services WHERE id = '$servicequery'";
$result = mysqli_query($connect, $serviceSql);
while($rowitem = mysqli_fetch_array($result)){
$cost = $rowitem['Cost'];
}
echo $cost === "" ? "Not Found" : $cost;
?>
HTML
<span id="txtHint"></span><br>
<button type="button" class="btn btn-primary" onclick="getCost('5')">Get Cost (5)</button>
Javascript
function getCost(num) {
if (num.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "/pagebuild/getcost.php?id=" + num, true);
xmlhttp.send();
}
}
The button should return the cost (75) of the item (id#5). Instead it is returning (Not Found)
Thanks in advance bee stuck for two days.