0

i am new to javascript and php and i need your help. I have a javascript function inside a php file. In this javascript function i have a variable that i want to pass it to another php file. Can anyone help me how to do this.

Thanks

user2487943
  • 13
  • 1
  • 4
  • 2
    PHP is server side while JS is client side, the only way to do this is through ajax. Lookk up how to make an ajax call to a url and pass your js variable as a parameter to php through a POST. take a look at this question, should give you a starting idea: http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php – Andriy Lysak Nov 14 '14 at 16:16
  • `window.location = 'otherscript.php?foo=' + var_you_want_to_pass` – Marc B Nov 14 '14 at 16:22

2 Answers2

2

There are several ways you can do this. Probably the simplest way is:

var myData = 'whatever';
window.location.href = 'otherscript.php?myData=' + myData;

Then your script 'otherscript.php' can access that variable with $_GET['myData'].

This method requires a page refresh. You can do it without refreshing the page by using ajax, but that is a little bit trickier.

Raphael Serota
  • 2,157
  • 10
  • 17
  • Thanks very much for your anwser. Now "otherscript.php" can access to the variable that i want but the problem is that leave the php that i am and goes to "otherscript.php". Is any way just to pass the variable without load "otherscript.php"? – user2487943 Nov 15 '14 at 13:42
-1

Something like that should work:

<script type="text/javascript">
       function yourFunction(yourParameter) {
              <?php $abc = "<script>document.write(yourParameter)</script>"?>   

       }    
</script>   

<?php 
     echo $abc;
?>