-1

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.

  • The URL is https? – ficuscr Jun 07 '19 at 18:33
  • Check your browser console. CORS blocking the request or some other error? – waterloomatt Jun 07 '19 at 18:34
  • @waterloomatt it can't be CORS since he's going to the same domain. – Barmar Jun 07 '19 at 18:42
  • It could be redirecting - https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSExternalRedirectNotAllowed. Unlikely, but still. – waterloomatt Jun 07 '19 at 18:43
  • First argument of `XMLHttpRequest.open()`, *method*, is ignored for non-HTTP(S) URLs. I don't like the ambiguity in using `$_REQUEST` and a common key like `id`. Possible collision.... Anyway, OP could help us out doing some basic debugging in the browser's developer tools console. – ficuscr Jun 07 '19 at 18:47
  • +1 `OP could help us out doing some basic debugging`. Could you please get your PHP to echo out `$serviceSql` and initiate it from AJAX? Want to see if `5` is actually making through from AJAX request. – waterloomatt Jun 07 '19 at 18:55
  • Also, pls turn on error reporting: http://stackoverflow.com/q/5438060/296555 and http://stackoverflow.com/q/14578243/296555 – waterloomatt Jun 07 '19 at 18:56
  • It is echoed out as SELECT * FROM services WHERE id = '5' – Austin Allen Carlson Jun 07 '19 at 18:58
  • `Notice: Undefined variable: connect in /homepages/19/d753843171/htdocs/dashboard/pagebuild/getcost.php on line 11` Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /homepages/19/d753843171/htdocs/dashboard/pagebuild/getcost.php on line 11 SELECT * FROM services WHERE id = '5' `Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /homepages/19/d753843171/htdocs/dashboard/pagebuild/getcost.php on line 15` `Not Found` – Austin Allen Carlson Jun 07 '19 at 19:04
  • Strange error to get when you told us it worked when invoked directly... You [understand your error](https://stackoverflow.com/questions/2973202/mysql-fetch-array-mysql-fetch-assoc-mysql-fetch-row-mysql-num-rows-etc) yes? – ficuscr Jun 07 '19 at 19:07
  • @ficuscr Yes- Thank you all. I forgot I changed the header and didn't include the db connection- Hopefully that will fix the issue – Austin Allen Carlson Jun 07 '19 at 19:10
  • We've all been there ;) – ficuscr Jun 07 '19 at 19:12

1 Answers1

-1

I didn't have the database include loaded through my header:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
double-beep
  • 5,031
  • 17
  • 33
  • 41