Assign js variable value to php variable

I have declared a javascript variable ,

 var myJavascriptVar = 12345;

And unable to assign that value to php variable;

 $myPhpVar = 'myJavascriptVar'; 

I know Ajax may be the solution of my problem. But i don't know how to use Ajax and solve the problem.


    
        
        
     

Assign js variable value to php variable

Punit Gajjar

4,6217 gold badges32 silver badges64 bronze badges

asked Feb 7, 2014 at 5:37

2

Using Cookie is the better solution i think -



answered Oct 12, 2016 at 19:58

Assign js variable value to php variable

1

Try using ajax with jQuery.post() if you want a more dynamic assignment of variables.

The reason you can't assign a variable directly is because they are processed in different places.

It's like trying to add eggs to an already baked cake, instead you should send the egg to the bakery to get a new cake with the new eggs. That's what jQuery's post is made for.

Alert the results from requesting test.php with an additional payload of data (HTML or XML, depending on what was returned).

$.post( "test.php", { name: "John", time: "2pm" })
  .done(function( data ) {
    alert( "Data Loaded: " + data );
  });

answered Feb 7, 2014 at 6:31

I have a better solution: Here, in the .php file, I have a variable called javascriptVar. Now, I want to assign the value of javascriptVar to my php variable called phpVar. I do this by simply call javascript variable by document.writeln in the script tag.

    
                var javascriptVar = 'success';
             ";
        
    
        $phpVar = "";
    ?>

answered Oct 13, 2020 at 4:11

Nazmul81Nazmul81

1321 gold badge3 silver badges12 bronze badges

3

PHP is server side language and JS is client side.best way to do this is create a cookie using javascript and then read that cookie in PHP




answered Mar 26, 2017 at 19:39

Assign js variable value to php variable

1

$msg = "";

echo $msg;

answered Sep 15, 2017 at 6:46

1

I guess you can use cookies for it.

1) First add a cookie jquery plugin.

2) Then store that window width in a cookie variable.

3) Access your cookie in PHP like $_COOKIE['variable name'].

http://www.w3schools.com/php/php_cookies.asp

answered Feb 7, 2014 at 5:53

hizbul25hizbul25

3,7394 gold badges25 silver badges37 bronze badges

Javascript will be interpreted in Client's browser and you can not assign it to PHP variable which is interpreted on SERVER .

Feasible Solution : You can submit the Javascript value via ajax or through form submit.

answered Feb 7, 2014 at 5:58

Assign js variable value to php variable

Dimag KharabDimag Kharab

4,4211 gold badge21 silver badges45 bronze badges

You should see these links:

Assign Javascript value to PHP variable

Passing Javascript vars to PHP

Or you can use AJAX request or POST and GET methods to achieve this.

Snippet below may helpful for you:

 

Your name:

answered Mar 26, 2014 at 9:10

Assign js variable value to php variable

Sohail xIN3NSohail xIN3N

2,8812 gold badges28 silver badges29 bronze badges

Use this code to solve your problem.

"?>   





answered Aug 25 at 10:42


    
        
        return myvar;" ?>
    

answered Jun 27, 2021 at 13:52

Assign js variable value to php variable

1

Please use this code and it will works fine in all cases.


document.writeln(width);";
?>

answered Jul 13, 2021 at 5:01

Assign js variable value to php variable

1

Can we assign JavaScript variable to PHP variable?

Javascript will be interpreted in Client's browser and you can not assign it to PHP variable which is interpreted on SERVER .

How do I pass JavaScript variable to PHP?

The way to pass a JavaScript variable to PHP is through a request. This type of URL is only visible if we use the GET action, the POST action hides the information in the URL. Server Side(PHP): On the server side PHP page, we request for the data submitted by the form and display the result. $result = $_GET [ 'data' ];

How use JavaScript variable on same page in PHP?

You can easily get the JavaScript variable value on the same page in PHP. Try the following codeL. php echo "