hi I am currently working on simple passing of variable value from a Javascript/Jquery variable to a php file.
function show(str){
if (str == "") {
document.getElementById("div_window").innerHTML="";
return;
}else if(str != ""){
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("div_window").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","server.php?q="+str,true);
xmlhttp.send();
}
}
the code above is the only thing i used up until now, but it is for retrieving of html, how do i start to use ajax for simple passing of variable value? where should i start and are there articles you can recommend?
3
Answers
You have to think differently about this. You can only request data from a server, not sent data to it. But while requesting data, you can pass data with your request.
Also, I really recommend not using jQuery. https://youmightnotneedjquery.com/
Checkout this post on how to request data with js: https://stackoverflow.com/a/29823632/4563136
You should then be able to access this data in PHP with $_POST: https://www.php.net/manual/en/reserved.variables.post.php
Just use var_dump($_POST) and it should print all variables back to your JS as a string. Use .text() instead of .json() and put the content variable into console.log(). You should see a pretty print of what you sent to the server.
It’s right you not need jQuery but you need to know its simplicity.
Take an work example, play with the variables and have fun
in JQuery
In PHP