0

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??

Community
  • 1
  • 1
  • Post/get is the standard way of trasnmitting data over http – DavidB Jan 21 '13 at 16:17
  • so many duplicate questions, it's hard to know which to link to. You need to learn about Ajax, and more generally how prorgams in a client/server environment (like a browser/web server) work. – SDC Jan 21 '13 at 17:09

2 Answers2

2

Either submit the mail variable via submit() in jquery, or use AJAX.

Manuals: AJAX, submit

 $.ajax({
  type: "POST",
  url: window.location.href,
  dataType: "json",
  data: mail,      
  success: function(response) { 
      //do something with response     
  }
});
Teena Thomas
  • 5,139
  • 1
  • 13
  • 17
0

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     
  }
});
Community
  • 1
  • 1
Techie
  • 44,706
  • 42
  • 157
  • 243