change php variable with ajax

后端 未结 5 417
时光说笑
时光说笑 2020-12-06 08:55

I have an php variable like this:

PHP Code:

$php_value = \'Am from PHP\';  

And I want to be able to change this variable with jQue

相关标签:
5条回答
  • 2020-12-06 09:33

    PHP code is run server-side, and jQuery runs on the client. The way to update a PHP variable from jQuery is to have a jQuery call which submits to the PHP page, and have the PHP look for it:

    $php_value = 'Am from PHP';  
    if exists($_POST['php_value_from_jquery']) {
       $php_value = $_POST['php_value_from_jquery'];
    }
    
    0 讨论(0)
  • 2020-12-06 09:33

    If I understand your question correctly, AJAX cannot post data to PHP code on the same page. I've been told that it can, but it is not trivial - still, I cannot imagine how that is possible. At any rate, AJAX is easy if a secondary PHP file is used.

    Here is an example of what I mean. If you try this:

    <?php
        echo 'Hello';
    ?>
    
    <html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $.ajax({
                    type: 'POST',
                    url: '',
                    success: function(data) {
                        alert(data);
                    }
                });
    
            }); //END $(document).ready()
    
        </script>
    </head>
    <body>
    
    
    </body>
    </html>
    

    The popup will contain the HTML for the page.


    However, if you use two files:

    file1.php

    <?php
        echo 'Hello';
    ?>
    

    file2.php

    <html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $.ajax({
                    type: 'POST',
                    url: 'file1.php',
                    success: function(data) {
                        alert(data);
                    }
                });
    
            }); //END $(document).ready()
    
        </script>
    </head>
    <body></body>
    </html>
    

    The popup will contain only the word "Hello".

    To use ajax, you must call an external PHP file.


    After considering the above, note that Quentin's answer is important -- even if you use AJAX to set a PHP variable on the server, that variable disappears after the AJAX completes -- just like the PHP variables all disappear after your index.php has finished rendering the DOM and presenting it to the visitor's browser.

    So, what's to be done? Two options.

    (1) As Quentin points out, you can store values permanently in a database, or

    (2) You can use a PHP superglobal, such as a $_SESSION variable. For example:

    Client side: file2.php

    var storeme = "Hello there";
    $.ajax({
        type: 'POST',
        url: 'file1.php',
        data: 'stored_on_server=' +storeme,
        success: function(data) {
            alert(data);
        }
    });
    

    file1.php

    <?php
        session_start();
        $SESSION['a_variable_name'] = $_POST['stored_on_server'];
    

    You can later retrieve that variable value thus:

    $.ajax({
        type: 'POST',
        url: 'file3.php',
        success: function(data) {
            alert(data); //a popup will display Hello There
        }
    });
    

    file3.php

    <?php
        session_start();
        echo $SESSION['a_variable_name'];
    
    0 讨论(0)
  • 2020-12-06 09:35

    You can't able to change the php value using javascript. i.e Server scripts runs first after that client side script will take effect in that case you cant able to modify the same, since they already rendered in browsers

    0 讨论(0)
  • 2020-12-06 09:37

    You can't.

    By the time the page has been delivered to the browser and the JavaScript has run, the PHP program that generated the page will have finished running and the variable will no longer exist.

    JavaScript will allow you to send new data to the server (Ajax), where the server could store the data somewhere (a database is usual), and read the response.

    JavaScript will also allow you to modify the page in in the browser (DOM) (including with the data included in the response for an Ajax request).

    0 讨论(0)
  • 2020-12-06 09:41

    If jQuery is going to be processing the data, then you can assign the PHP variable to a jQuery variable like this:

    <script>
        var jquery_value = <?php echo $php_value; ?>
    </script>
    

    As far as I know, because jQuery is client-side and php is server side, it's not possible to assign a jQuery variable back to PHP.

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