How can I store JavaScript variable output into a PHP variable?

前端 未结 8 1117
时光取名叫无心
时光取名叫无心 2020-11-30 04:09

I have the following code in JavaScript:


PHP Code:-



        
相关标签:
8条回答
  • 2020-11-30 05:07

    You can solve this problem by using AJAX. You don't need to load JQuery for AJAX but it has a better error and success handling than native JS.

    I would do it like so:

    1) add an click eventlistener to all my anchors on the page. 2) on click, you can setup an ajax-request to your php, in the POST-DATA you set the anchor id or the text-value 3) the php gets the value and you can setup a request to your database. Then you return the value which you need and echo it to the ajax-request. 4) your success function of the ajax-request is doing some stuff

    For more information about ajax-requests look back here:

    -> Ajax-Request NATIVE https://blog.garstasio.com/you-dont-need-jquery/ajax/

    A simple JQuery examle:

    $("button").click(function(){
      $.ajax({url: "demo_test.txt", success: function(result){
        $("#div1").html(result);
      }});
    });
    
    0 讨论(0)
  • 2020-11-30 05:08

    You have to remember that if JS and PHP live in the same document, the PHP will be executed first (at the server) and the JS will be executed second (at the browser)--and the two will NEVER interact (excepting where you output JS with PHP, which is not really an interaction between the two engines).

    With that in mind, the closest you could come is to use a PHP variable in your JS:

    <?php
    $a = 'foo'; // $a now holds PHP string foo
    ?>
    <script>
        var a = '<?php echo $a; ?>'; //outputting string foo in context of JS
                                     //must wrap in quotes so that it is still string foo when JS does execute
                                     //when this DOES execute in the browser, PHP will have already completed all processing and exited
    </script>
    <?php
    //do something else with $a
    //JS still hasn't executed at this point
    ?>
    

    As I stated, in this scenario the PHP (ALL of it) executes FIRST at the server, causing:

    1. a PHP variable $a to be created as string 'foo'
    2. the value of $a to be outputted in context of some JavaScript (which is not currently executing)
    3. more done with PHP's $a
    4. all output, including the JS with the var assignment, is sent to the browser.

    As written, this results in the following being sent to the browser for execution (I removed the JS comments for clarity):

    <script>
        var a = 'foo';
    </script>
    

    Then, and only then, will the JS start executing with its own variable a set to "foo" (at which point PHP is out of the picture).

    In other words, if the two live in the same document and no extra interaction with the server is performed, JS can NOT cause any effect in PHP. Furthermore, PHP is limited in its effect on JS to the simple ability to output some JS or something in context of JS.

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