Possible Duplicate:
How to pass JavaScript variables to PHP?
Passing a Javascript string to PHP
I have this javascript code inside a PHP file:
<script>
function(){
var mail="comitalia@faac.it";
for(var i=0; i<json_load.length; i++){
if(json_load[i].custom.tipologo == "FAAC FILIALI"){
mail=json_load[i].custom.email;
}
}
return mail;
}
</script>
I need to return the "mail" variable to a PHP variable. How can I do that??
Either submit the mail variable via submit()
in jquery, or use AJAX
.
$.ajax({
type: "POST",
url: window.location.href,
dataType: "json",
data: mail,
success: function(response) {
//do something with response
}
});
If you understand the basics, you can not pass java-script values to the php variable. Because PHP is rendered at the server-side and java-script is rendered at the client side. Read this.
What you can do is, You can pass the data to the server using AJAX,
$.ajax({
type: "POST",
url: path/to/the/php/file.php,
mail: mail,
success: function(response) {
//if you return something from the server-side you get it here
}
});