繁体   English   中英

在 PHP 中使用来自 AJAX 请求的 JSON 字符串

[英]Use JSON string from AJAX request in PHP

以下内容让我一头雾水……

使用 AJAX,我通过外部 PHP 文件从我的数据库中请求了一些数据,该文件返回一个 JSON 字符串。 我想将返回的 JSON 字符串转换为 PHP 变量(数组),以便我可以再次在页面上回显我的数组索引。 它可能不会那样工作,但这就是我在这里尝试做的......我如何再次在 PHP 中使用返回的 JSON?

index.php ,用户选择触发 AJAX 请求的单选按钮(也在同一页面上):

$(document).ready(function() {
  $("input[name='select']").click(function() {
    var select = $("input[name='select']:checked").val();
    $.ajax({
      type: "POST",
      url: "processing_file.php",
      data: { select: select },
      cache: false,
      success: function(data) {

        // WHAT DO I DO HERE INSTEAD OF alert(data); ???

      },
      error: function(xhr, status, error) {
        console.error(xhr);
      }
    });
  });
});

该请求由processing_file.php ,其中包含一个用于处理 AJAX 请求的类。 (该类扩展了数据库类,其中包含所有凭据和查询处理,它工作正常。)

class Ajax extends Database {

  public function getInfo() {
    
    $id = $_POST['select'];

    $query = "SELECT * FROM table WHERE table.id = '$id';";
    
    $data = $this->execute($query);
    $info = $data[0];

    $json = json_encode($info);

    echo $json;
    /* I don't want to echo it, I need this variable on index.php */

  }

} # End class

$ajax = new Ajax;
$ajax->getInfo();

回到index.php ,在那里 AJAX 请求开始并返回,我想将返回的 JSON 字符串转换为 PHP 变量(数组)。 所以我可以在需要显示的地方回显我的数组数据。 这是index.php其余部分的样子:

<form class="form" action="process_form.php" method="POST">
  <div class="tab">

      <?php
        // Loop through results of earlier query
        foreach ($post_data as $post) {
      ?>
      
        <!-- The PHP echo results come from an earlier (irrelevant) database query
          It creates a group of radio buttons, which the user selects to trigger AJAX
        -->
        <label class="pselect-container"> <?php echo $post['name']; ?>
          <input type="radio" name="select" value="<?php echo $post['id']; ?>">
          <span class="pselect-checkmark" style="width:100%;">
            <?php echo $post['name']; ?>
          </span>
        </label>

      <?php
        }
      ?>
      
  </div>
  <div class="tab">
    Tab 2:

    <!-- The PHP echo results below should come from the AJAX request
      My idea was to convert the returned JSON to a PHP array, so I can
      echo the array data here.
    -->
    The first piece of data I want to show is <?php echo $info[0]; ?> </br>
    <div style="width:100%;"> But there is also more: <?php echo $info[1]; ?> </div>
    <p>And finally, for <?php echo $info[2]; ?> another paragraph element.</p>

  </div>

  <div class="tab">
    Tab 3:
    <!-- Might want to show more AJAX result data here -->
  </div>
  <div class="tab">
    Tab 4:
    <!-- Might want to show more AJAX result data here -->
  </div>
</form>

在这里,我尝试使用<?php echo $info[index]; ?>来回显 JSON 数据<?php echo $info[index]; ?> <?php echo $info[index]; ?>为了这个问题。 我知道它不能那样工作,但它澄清了我在这里想要完成的工作。 如果我可以将我的 JSON 数据转换为 PHP 数组,我会这样做。

请注意:出于隐私原因,我更改了一些变量和名称,并简化了我的数据库查询。

如果你需要在另一个后端脚本中使用这个变量,你应该用另一个 ajax 将它传递到那里。

但是,如果您执行一个 ajax 来获取 json,然后将其传递给另一个脚本,为什么不在您的第一个 ajax 调用中执行此操作呢? 做这样的事情:

$(document).ready(function() {
  $("input[name='select']").click(function() {
    var select = $("input[name='select']:checked").val();
    $.ajax({
      type: "POST",
      url: "processing_file.php" // do whole work here not in index.php,
      data: { select: select },
      cache: false,
      success: function(data) {
    
      },
      error: function(xhr, status, error) {
        console.error(xhr);
      }
    });
  });
});

暂无
暂无

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

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