Set phps $_REQUEST['variable_name'] in JavaScript

后端 未结 4 675
轮回少年
轮回少年 2021-01-21 20:00

is it possible to store a variable through javascript so that I can read it in php using $_REQUEST[\'variable_name\'].

for eg. let\'s say i have

$adcat         


        
相关标签:
4条回答
  • 2021-01-21 20:38

    Does this make any sense? Is this possible?

    Yes, you need to bind it to the request string.

    If you're calling foobar.php, change the onsubmit behavior of the form and make it:

    'foobar.php?category_id=' + your_javascript_category_id_value
    
    0 讨论(0)
  • 2021-01-21 20:43

    Assuming you are using Apache:

    Try setting the default in a file (called "my_vars.php" in this example), then in your .htaccess file:

    php_value auto_prepend_file /absolute/path/to/my_vars.php

    For the js side, take the value of what you set in that file at page load.

    <script type="text/javascript">
    var = <?=$what_i_set_in_my_vars?>;
    </script>
    

    Research: http://www.google.com/search?aq=f&sourceid=chrome&ie=UTF-8&q=php_value+auto_prepend_file

    I have found that auto prepend is good if you want to use a sort of settings file for storing stuff like this, IMHO. That way you don't need to jump through hoops, the draw back is that it will be included on each page.

    0 讨论(0)
  • 2021-01-21 20:51

    It's less of a technical problem, more of how you structure your code and interaction between the PHP backend and in-page Javascript.

    To get the $category_id variable into Javascript, the typical approach is:

    <?php print "<script>category_id = $category_id;</script>";
    

    To have your Javascript code send a catid back to a PHP page:

    $("#id").load("page.php?category_id="+category_id);
    

    This would ping it back to PHPs $_REQEUST[] array. But the question is why you need the variable available in Javascript first.

    0 讨论(0)
  • 2021-01-21 20:55

    Does this make any sense? Is this possible?

    No and no :)

    PHP executes on server side before anything else, Javascript in the browser. The only way to do this in JavaScript would be to manipulate the form that makes that request before it gets submitted.

    0 讨论(0)
提交回复
热议问题