Link to home
Start Free TrialLog in
Avatar of BR
BRFlag for Türkiye

asked on

assigning javascript variable to php variable

I have a javascript variable named "myvariable" . I want to assign this variable to a php variable
how can I do that?
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BR

ASKER

the javascript value is like this
<script> var myvariable="something"; </script>

what I want to do is like
<?php
echo $myphpvariable = '<script> here I need to call my variable but I don't know how </script>'
let's say using:

<script> var myvariable="something"; 
window.location.href = "yourphp.php?myvariable=" + myvariable;
</script>

Open in new window


then in yourphp.php:

<?php
$myphpvariable = isset($_GET['myvariable']) ? $_GET['myvariable'] : '';
echo "your variable = ". $myphpvariable;
?>

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BR

ASKER

thank you all
Avatar of BR

ASKER

I'm ill now, i will reconsider the problem and  how to make myself understood and reask the question on monday. Thank you
Avatar of BR

ASKER

Sorry i was answering another question. I don't know how did it happen. Sorry
Avatar of Vikas Chauhan
Vikas Chauhan

<script type="text/javascript">
    document.cookie = "myJavascriptVar =12345";
</script>

<?php
   $phpVar =  $_COOKIE['myJavascriptVar'];

   echo $phpVar;
?>

from https://stackoverflow.com/questions/21620133/how-to-assign-javascript-variable-value-to-php-variable 
second answer it may be beneficial for you
@Vikas Chauhan  - posting answers from the "Expert Exchange Hater Site" is not really a great idea. Also the cookie is not available to PHP until the PHP page is then loaded using an HTTP request
@Michel Plungjan We can also follow another approach -

The approach is when site is getting loaded send device width from client side using ajax and save it in the database and then fetch where you want to use.

It might me be the solution m not sure ....