Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript AJAX POST does not work for mysqli

Hi, Geniuses:
I have followed the instructions on this page using a database on my machine.
Everything works fine.
However, the code does not work when I change all "GET"s to "POST"s and change
xmlhttp.open("GET","getuser.php?q="+str,true); xmlhttp.send();
to
xmlhttp.open("POST","getuser.php",true); xmlhttp.send("q="+str);
So what do I miss?
 
Last edited:
So it works ok with GET ? Then why would you want to use POST instead ?
It seems pretty clear why it's not working, seeing that the PHP code gets the username from the GET request :

PHP:
$q = intval($_GET['q']);
 
So it works ok with GET ? Then why would you want to use POST instead ?
It seems pretty clear why it's not working, seeing that the PHP code gets the username from the GET request :

PHP:
$q = intval($_GET['q']);

Thanks a lot, cbreemer, for replying.
Actually I have changed $q = intval($_GET['q']); to $q = intval($_POST['q']);. It still does not work.
Then why would you want to use POST instead ?
I'd like to play with different methods/options.
 
Thanks a lot, cbreemer, for replying.
Actually I have changed $q = intval($_GET['q']); to $q = intval($_POST['q']);. It still does not work.

I'd like to play with different methods/options.
Hi there,
Not sure what you mean by 'play with different methods/options'...can you clarify please?
 
Learn and compare different methods/options.
Not much to compare lol.
GET requests are for when you need to `get` user data from the database
POST requests are for when you need to save data to the database
PUT requests are for when you need to update/replace records on the database
PATCH requests are for when you need to update/modify records on the database
DELETE requests... self explanatory

 
Not much to compare lol.
GET requests are for when you need to `get` user data from the database
POST requests are for when you need to save data to the database
PUT requests are for when you need to update/replace records on the database
PATCH requests are for when you need to update/modify records on the database
DELETE requests... self explanatory


Thanks a lot, Antero360, for teaching.
Anyway, can we make the code on this page work if we change "GET" to "POST"?
 
Hi, Geniuses:
I have followed the instructions on this page using a database on my machine.
Everything works fine.
However, the code does not work when I change all "GET"s to "POST"s and change
xmlhttp.open("GET","getuser.php?q="+str,true); xmlhttp.send();
to
xmlhttp.open("POST","getuser.php",true); xmlhttp.send("q="+str);
So what do I miss?
So what are you trying to do here?
 
Hi, Geniuses:
I have followed the instructions on this page using a database on my machine.
Everything works fine.
However, the code does not work when I change all "GET"s to "POST"s and change
xmlhttp.open("GET","getuser.php?q="+str,true); xmlhttp.send();
to
xmlhttp.open("POST","getuser.php",true); xmlhttp.send("q="+str);
So what do I miss?
What language is this code for?
 
Thanks a lot, cbreemer, for replying.
Actually I have changed $q = intval($_GET['q']); to $q = intval($_POST['q']);. It still does not work.

I'd like to play with different methods/options.
Ah, it was not apparent from your post that you had changed the php as well.
Now, your code is basically ok but you have to keep in mind that POST is meant for HTML form submit requests. That means you have to set the content type accordingly. Add this line

JavaScript:
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

before calling xmlhttp.send. That will make it work.
 
Last edited by a moderator:
Ah, it was not apparent from your post that you had changed the php as well.
Now, your code is basically ok but you have to keep in mind that POST is meant for HTML form submit requests. That means you have to set the content type accordingly. Add this line

JavaScript:
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

before calling xmlhttp.send. That will make it work.

Thanks for teaching, cbreemer.
I followed your instruction, but it still does not work.
It seems that the $q does not get into getuser.php at all.
 
Thanks for teaching, cbreemer.
I followed your instruction, but it still does not work.
It seems that the $q does not get into getuser.php at all.
Hm, strange, I did test it. Admittedly, I used a different W3C example : https://www.w3schools.com/xml/ajax_php.asp
This uses GET and I got it to work with POST by making your changes (which did not work), then adding my line (which made it work).
 
Last edited by a moderator:
Solution
Hm, strange, I did test it. Admittedly, I used a different W3C example : https://www.w3schools.com/xml/ajax_php.asp
This uses GET and I got it to work with POST by making your changes (which did not work), then adding my line (which made it work).
Yeah, I thought the same for setting the header for content type. I wonder if the file that is receiving the POST (or GET originally) is expecting a different content-type. Worth a check
 
Hm, strange, I did test it. Admittedly, I used a different W3C example : https://www.w3schools.com/xml/ajax_php.asp
This uses GET and I got it to work with POST by making your changes (which did not work), then adding my line (which made it work).
Thanks a lot, cbreemer.
I changed $q = intval($_POST['q']) to $q = intval($_REQUEST['q']), it works immediately.
When I changed back to intval($_POST['q']), it works too. Quite strange, but works anyway.
Thanks again.
 
Back
Top Bottom