简体   繁体   English

在第二个 php 文件(通过 jQuery load())中更改后,如何确保 index.php 中的 PHP 变量可用?

[英]How do I ensure a PHP variable is available in index.php after being changed in a second php file (via a jQuery load())?

In my index.php I have the following jQuery code for responding to clicking an image:在我的index.php我有以下 jQuery 代码用于响应单击图像:

<script>
    $(document).on('click',".i-give-up", function() {
        $("#take-a-guess").load("get_snippet.php");

        [...snip...]

        generate_HSK2_circle(<?php echo $answer_data["HSK2level"]; ?>);
    });
</script>

When I click the image, it loads get_snippet.php which performs and processes a MySQL query.当我单击图像时,它会加载执行和处理 MySQL 查询的get_snippet.php Moreover, if I add此外,如果我添加

echo "<script>console.log('get_snippet[new] HSK2 level: " . $answer_data["HSK2level"] . "' );</script>";

to the end of get_snippet.php , I can see the correct variable for $answer_data["HSK2level"] in get_snippet.php .到年底get_snippet.php ,我可以看到正确的变量$answer_data["HSK2level"]get_snippet.php

The problem is that the variable $answer_data["HSK2level"] in index.php does not change as a result of loading get_snippet.php .问题是index.php中的变量$answer_data["HSK2level"]不会因为加载get_snippet.php而改变。

Question : How do I ensure a PHP variable is available in index.php after being changed in a second php file (via a jQuery load())?问题:在第二个 php 文件(通过 jQuery load())中更改后,如何确保 index.php 中的 PHP 变量可用?

The index.php file is not going to change after it is already rendered, unless PHP is processed on the server. index.php文件在呈现后不会更改,除非在服务器上处理 PHP。

In order to pass this data, you may want to look into using a query string and redirecting back to index.php with a get variable.为了传递这些数据,您可能需要考虑使用查询字符串并使用 get 变量重定向回 index.php。

https://www.php.net/manual/en/reserved.variables.get https://www.php.net/manual/en/reserved.variables.get

Then in your get_snippet.php , you would direct to index.php?answer=... and retrieve it via htmlspecialchars($_GET["answer"])然后在您的get_snippet.php ,您将指向index.php?answer=...并通过htmlspecialchars($_GET["answer"])检索它

It seems to me that you are actually wanting to store the php value of $answer_data["HSK2level"] in a javascript variable so each time you call generate_HSK2_circle() you pass in the most current value在我看来,您实际上是想将$answer_data["HSK2level"]的 php 值存储在一个 javascript 变量中,因此每次调用generate_HSK2_circle()您都会传入最新的值

Note that ajax is asynchronous also so you need to use a complete callback before proceeding with new data请注意,ajax 也是异步的,因此您需要在继续处理新数据之前使用完整的回调

To do that your js would look like:为此,您的 js 将如下所示:

<script>
    // new js variable recieves value on page load from php
    var answer_data = <?php echo $answer_data["HSK2level"]; ?>;

    $(document).on('click',".i-give-up", function() {
        // use complete callback of load to assure new data loaded
        $("#take-a-guess").load("get_snippet.php", function(){
          // new data has loaded
          [...snip...]
          // use the js variable in function call
          generate_HSK2_circle(answer_data);
        });

       
    });
</script>

Then in your new script tag (from load) update that js variable value然后在你的新脚本标签(来自加载)中更新那个 js 变量值

echo "<script>answer_data=". $answer_data['HSK2level']. "</script>";

If this new script tag is the only data being sent from server in load() a more appropriate approach would be to send json and use $.getJSON and manually update the js variable from the response object如果这个新的脚本标签是load()从服务器发送的唯一数据,更合适的方法是发送 json 并使用$.getJSON并从响应对象手动更新 js 变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM