1

I need to pass some JS variable to PHP and am having some trouble.

I have tried the following:

$product_id = "<script> var prod_id_one = $('ul.products li:nth-child(1) a.button').attr('data-product_id')</script>";
echo $product_id;

But this just prints it as a string:

`<script> var prod_id_one = $('ul.products li:nth-child(1) a.button').attr('data-product_id');</script>`

How would I store that JS variable and then echo it using PHP? I am quite new to PHP, so any help would be appreciated!

5
  • 1
    PHP is server-side, Javascript is client-side. The work PHP does comes therefore before Javascript's. If you want to pass some info as you say "from Javascript to PHP" you have to issue a request to the server,(through a link, a form or an Ajax call for example). Commented Mar 3, 2015 at 8:15
  • I think it should work the way you did it. Are you using a framwork, and can you make sure you don't have something like htmlspecialchars somewhere in your code? Commented Mar 3, 2015 at 8:18
  • Thanks. @Pierre-Jean No special framework and no htmlspecialchars. I will have to re-think the previous step and rework this a bit. Commented Mar 3, 2015 at 8:21
  • @Pierre-Jean Absolutely not... It just CAN'T work the way he did it. Commented Mar 3, 2015 at 8:31
  • With this code I understand he wants to print javascript code with PHP. It's possible and easy to do. But indeed if he wants do to the opposite (javascript var to php), it won't work... Commented Mar 3, 2015 at 8:32

3 Answers 3

2

By doing it your way, it's just impossible. PHP can't "read" or "interact with" javascript directly in the same page.

You have to understand that PHP is a preprocessor, it generates HTML on the server, then the generated page is sent to the client. In this page, the PHP code has entirely disappeared. You can only see what it generated (that is, HTML or JS). Then, the javascript code runs, and it has no idea it was generated using PHP, and no idea of PHP's presence whatsoever.

In order to pass variables to a PHP script, you have to call the file with GET or POST methods :

(JS)

$.get( 'myScript.php', { // This is calling the PHP file, passing variables (use get or post)
     variable1 : "Hello",
     variable2 : "world!"
   }, function(data){ // PHP will then send back the response as "data"
      alert(data); // will alert "Hello world!"
});

(myScript.php)

    $variable1 = $_GET['variable1']; // or POST if you're using post
    $variable2 = $_GET['variable2'];

    echo $variable1 . " " . $variable2; // Concatenates as "Hello world!" and prints it out.
//The result of the PHP file is sent back to Javascript when it's done.

Of course, this is a very basic example. Never read and use directly what is sent to PHP (as I just did), because anyone could inject whatever they'd want. Add securities.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the comprehensive answer.
No worries. If you find it valuable, I would appreciate if you validated it :)
0

JavaScript runs on the client side while PHP runs on the server. They execute at completely different times in the page lifecycle, so they cannot communicate in the manner you have used.

Instead you could to use AJAX to send a value in JS to a PHP page.

1 Comment

Thank you. I have a few ideas I will try.
0

Thanks for the assistance, very much appreciated! I have managed to get it done by using ajax as suggested:

<script type="text/javascript">
jQuery("document").ready(function(){
    var $ = jQuery
    $("form").submit(function(){
        var data = "";
        data = $(this).serialize() + "&" + $.param(data);
        var prod_id_one =   $('#prod1').val();
        var prod_id_two =   $('#prod2').val();
        var prod_id_three = $('#prod3').val();
        var prod_id_four =  $('#prod4').val();

        $.ajax({
            type: "GET",
            url: "my_ajax_url_here", 
            data: data,
            success: function(data){ 
                window.location = "price-calculator?width="+ $('#wpti-product-x').val() + "&height=" +  $('#wpti-product-y').val() + "&id1=" + prod_id_one + "&id2=" + prod_id_two + "&id3=" + prod_id_three + "&id4=" + prod_id_four;
            }
        });
        return false;
    });
});
</script> 

It is now working with the above code. Thanks again!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.