Get cool snippets on php, css, jQuery, Magento and wordpress

Passing Javascript variables to PHP

It’s easy to get Javascript variable into PHP.

What you have to do is follow below code.

//in script block
var veg = 'Carrot';
//in php block
$veg = "<script type='text/javascript'>document.write(veg);</script>";
echo 'Printed My JS variable into PHP: '.$veg;

Result will be:
Printed My JS variable into PHP: Carrot

karthi s

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: