I need to send variables (username+password) from a js function inside a .js file, to a PHP file, and store them inside the file.
I tried to use ajax but it doesn't work for me:
function func(){
var user = ...
var pass = ...
$.ajax({
type:"POST",
url:"myphp.php",
data: user,
success: function(){
alert("success!");
},
error: function(){
alert("error!")
}
});
}
when I open the PHP file I see nothing.
EDIT: my problem is much more basic. My ajax code just doesn't run inside the function. I have a .js file that contains only one function (the function above) and the ajax code doesn't get executed. I get no success nor error messages.
You need to pass the data to jQuery like this:
$.ajax({
type:"POST",
url:"myphp.php",
data: {user: user, password: pass},
success: function(data){
alert(data);
}
});
PHP won't have any problems parsing this, since jQuery automatically sends it in application/x-www-form-urlencoded
. If you need to send JSON and have PHP read JSON, then that's a different story.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.