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="[email protected]";
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??

2
  • Post/get is the standard way of trasnmitting data over http Commented Jan 21, 2013 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. Commented Jan 21, 2013 at 17:09

2 Answers 2

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     
  }
});
Sign up to request clarification or add additional context in comments.

Comments

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     
  }
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.