-1

So here's what I'm trying to do. I have a form and one of the values is the Username. I want to check if that Username already exists in the database. So I set a JavaScript function as the onsubmit for the form. In the function I have

xhttp.open("POST","AjaxTest.php",false);
xhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhttp.send("Username=Leroy1981&Found=0");

A it directs to the right file, but I don't know how to access the data send in the send command and I still have no idea how to send the result, once the php file has checked the database for the supplied username. Username is an input type in the form, so it gets sent but I get undefined constant for Found.

Another question , how do I set the XMLHttpRequest response in the php file? I could probably figure out how to retrieve that and just send "Found" or "Not Found", but don't know how to access it in the php file. Also it doesn't automatically return from the php file when it's done, which is how I thought this worked.

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
Leroy1981
  • 3
  • 2
  • Another question , how do I set the XMLHttpRequest response in the php file? I could probably figure out how to retrieve that and just send "Found" or "Not Found", but don't know how to access it in the php file. Also it doesn't automatically return from the php file when it's done, which is how I thought this worked – Leroy1981 Apr 09 '19 at 11:37
  • Super new to web programming btw. In case that part wasn't obvious – Leroy1981 Apr 09 '19 at 11:43
  • I have voted to close this as a duplicate as it really seems to be. If this is NOT the case, please edit your question with a good deal more code including the complete set of code you are trying with details on why/where it does NOT work so we can assist you better. – Mark Schultheiss Apr 09 '19 at 12:07
  • `xhttp.open("POST","AjaxTest.php",false);` — You are making a synchronous request. This is **deprecated** and locks up the UI while the request is in flight. Don't do this. – Quentin Apr 09 '19 at 12:10
  • "how do I set the XMLHttpRequest response in the php file" —The same as you set the response in any other PHP file. The fact the request was triggered by JS is irrelevent. – Quentin Apr 09 '19 at 12:10
  • `I don't know how to access the data send in the send command` in ajaxtest.php, the data will be in the $_POST array. – James Apr 09 '19 at 12:13
  • It's giving me an error when I access $_POST[]. Undefined constant or id. At work right now so can't check – Leroy1981 Apr 09 '19 at 13:49

1 Answers1

0

You can call your file with this function

function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "AjaxTest.php?Username=Leroy1981", true);
  xhttp.send();
}

The output will be in the variable this.responseText

Your PHP file should look something like this

$user = $_GET['User'];
//Your db operations
$yourResponse = "whatever you want";
echo $yourResponse; //This will be your responseText in JavaScript
Donny
  • 516
  • 3
  • 9
  • So whatever I echo at the end of the php script is what's in the responseText then? I guess I could use GET with just sending the username and set $_POST['username] to EMPTY if it's not already in the database – Leroy1981 Apr 09 '19 at 13:53
  • Thanks for the info. Looked it up and saw that was the case. About echo setting the responseText – Leroy1981 Apr 09 '19 at 17:20
  • If my answer helped you mark it so other people with the same issue can see it – Donny Apr 09 '19 at 19:48