• 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 How to send a PHP save request in client side Javascript?


Bronze Coder
Good hi, I am trying to just set a variable to a database and like my starting point is Javascript client side where I am trying to find if this works.
var xhttp = new XMLHttpRequest();
xhttp.onload = function() {
    // use this.responseText;
xhttp.open("SET", "likeMyPHP.php?q=MyData");

I am trying to find something that works as far back as possible and on all (or maybe most) browsers.
I am trying not to use jQuery as that is like for only most up to date browsers.
I do not need Internet Explorer support, that is too old now.
Just trying to find how to call a PHP script from Javascript and have it just save stuff and not go away from current page.
This seems like it should work according to research and Microsoft Edge browser testing (it 404 d but it sent a request seemingly).
I think I understand how PHP would need to handle like this but does it work?

I am using placeholders.
With placeholders, likeMyPHP.php is a placeholder and so is MyData.
I know someone may need PHP and a database maybe to test this.
I would use responseText for whether to allow something so I need like that.

Absent PHP needed for this, does this run correctly if a PHP script is present?
I would be setting a country code and a timestamp if that helps.
I would also be using MySQL databases.
I realize I may need to use GET not SET.
X E.
Last edited:
Not the way I'd do it, but looks good.
FYI: To check if my AJAX works I start my PHP sending back a "Made it!" Message and have JS flash it as an alert.
Josiah, It looks like you have a grasp on how AJAX works, but are unsure how to use it.
Let's look at the errors in your post. Things that are wrong for what you want”.
From your quote:
xhttp.onload = function() {
Note that onload{...} executes immediately after a page has been loaded: Meaning BEFORE the user enters any information. - You are trying to send user info to your DB correct? OK Saw my mistake later in the post.

I am using placeholders.
With placeholders, likeMyPHP.php is a placeholder and so is MyData.
This is not what a placeholder is. Read this: w3schools.com/tags/att_placeholder.asp

I would be setting a country code and a timestamp if that helps.
You should have led with this. Yes, finding the country can be done on page load.
Use Navigator.geolocation to find the country.

Use SET not GET to send to PHP.

Hope this helps.
So if not onload, how do I get a PHP response?
By placeholders I mean like I would replace that with relevant paths and data before actually using this.
It would be like on an index.php, SET from another PHP file, info saved to DB in that PHP file, and it returns whether success.
That does like help but what am I supposed to do about response?
X E.
I found this.
Can you confirm this is how to get response from an AJAX request like this?
I know to use SET now.
I know there is xhttp.addEventListener("readystatechanged"...) or like that too.
X E.
I found it would be like this.readyState == 4 for start in xhttp.onreadyStateChange.
Then this.status check, if 200, then success, otherwise failure and check for that.
For response, use xhttp.responseText.
Use data in xhttp.send("likeMyData") and no need for that "?q=" and after it.
I got this from W3Schools.
I guess it is correct but is there anything I am missing?
I asked zzzcode.ai and it said there may be other stuff like setting header before sending.
Anyway, can you confirm this?
I looked, and it seems like I can use all this on modern and not so modern browsers.
I am not trying for on page load, I am trying for on button click.
Yes I am using navigator.geolocation in a custom check and after that I am calling that PHP to save.
Anyway, I guess like my PHP call using SET is correct now, and according to CanUse.com and its sources, this stuff is all fully supported a while back.
I just am trying to know, is like my way of using this thing to call PHP correct now?
X E.

New Threads

Latest posts

Buy us a coffee!

Top Bottom