How do I pass along variables with XMLHTTPRequest
If you want to pass variables to the server using GET that would be the way yes. Remember to escape (urlencode) them properly!
It is also possible to use POST, if you dont want your variables to be visible.
A complete sample would be:
var url = "bla.php";
var params = "somevariable=somevalue&anothervariable=anothervalue";
var http = new XMLHttpRequest();
http.open("GET", url+"?"+params, true);
http.onreadystatechange = function()
{
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(null);
To test this, (using PHP) you could var_dump $_GET
to see what you retrieve.
XMLHttpRequest GET params undefined in request nodejs api
As mentioned, GET or HEAD requests cannot have a body.
If your data is large, you should move to a POST request.
However, if the parameters you want to use are short like the ones in the example, you should use query strings:
var url = "bla.php";
var params = "somevariable=somevalue&anothervariable=anothervalue";
var http = new XMLHttpRequest();
http.open("GET", url+"?"+params, true);
http.send(null);
on the node side, assuming you use express, you can read the variables using:
var somevariable = req.query.somevariable;
var anothervariable = req.query.anothervariable;
Passing variable into xmlHTTPRequest as a URL parameter
You have extra double quotes "
in following line :
ajax.open("GET", "'mexx.php?market='+market", true);
_________________^_________________________^
Should be :
ajax.open("GET", 'mexx.php?market='+market, true);
Also put double qoutes "
arround :
market = <?php echo $market; ?>;
Should be :
market = "<?php echo $market; ?>";
_________^______________________^
Hope this helps.
Passing multiple parameters by POST using XMLHttpRequest and HTML
The data variable you are using needs to be a JSON object. In your example here it is a string, so the individual values are not passed, just one single string.
Try:
var data = `{"ssid": "${network}", "psk": "${presharedkey}"}`;
passing a javascript variable to PHP with xmlhttprequest
Instead of
print_r($test);
use the echo
echo $test;
As $test
is not an array is a string value. print_r
is used to print the array. that's why is given the null value.
And your send function in ajax should be like this:
hr.send("value="+value);
In the send function, the parameter that passed must be a string like this:
"name=value&anothername="+encodeURIComponent(myVar)+"&so=on"
More tutorial is here.
Passing more then one value with XMLHttpRequest?
Add &
to your url when you pass another variable.
You also forgot a +
.
Change :
xmlhttp.open("GET","adminfunction.php?status="+status"id="+id,false);
To :
xmlhttp.open("GET","adminfunction.php?status="+status+"&id="+id,false);
Is there a way to get params from a XMLHttpRequest response?
As Bergi said it's not possible to retrieve the parameters that were sent with the request on the response. So I'm closing the question.
Thanks to everyone who helped!
How to store $_SESSION variables in Javascript for XMLHttpRequest purposes?
You don't have to use javascript to share information between PHP files (even if on separate servers).
$_SESSION is used explicitly to keep data between pages and you should handle it within them as such.
If you have to send user_id to a file on a different server, it's advised to do it inside of your PHP files and not Javascript (which is clientside and thus very prone to be abused).
You could simply use curl to perform a HTTP POST request.
Related Topics
"Status Code:200 Ok (From Serviceworker)" in Chrome Network Devtools
Extract the Current Dom and Print It as a String, with Styles Intact
Programmatically Editing Less (Css) Code with Jquery-Like Selector Syntax
Disabling the Context Menu on Long Taps on Android
Jquery Ajax Success Callback Function Definition
Document.Getelementbyid VS Jquery $()
How to Subtract Date/Time in JavaScript
Cross-Browser Method to Determine Vertical Scroll Percentage in JavaScript
Check If Element Exists in Jquery
Making Sure at Least One Checkbox Is Checked
Ios8 Safari After a Pushstate the :Nth-Child() Selectors Not Works
Safari in iOS8 Is Scrolling Screen When Fixed Elements Get Focus
R Shiny Datatable Selected Row Color
JavaScript Alert Not Working in Android Webview
Call Python Function from JavaScript Code
How to Convert a Float Number to a Whole Number in JavaScript
Javascript: Get Deep Value from Object by Passing Path to It as String