在PHP中检索JS数据

Retrieving JS data in PHP

本文关键字:JS 数据 检索 PHP      更新时间:2023-09-26

我正在使用data()方法记录信息

   optionScope.data().stage = 'a';
    where optionScope = $(this);

我想在我的php代码中存储这个值:

<?php
  include("functions/db.php");
  $format = "Online Course";
  $stage = GETTING THIS DATA;
  $topic = "Idea Generation";
  $resources = "select * from resources where format LIKE  '".$format."' and stage LIKE '%".$stage."%' ";
  $run_query = mysqli_query($con, $resources);
  while($row = mysqli_fetch_array($run_query)) {

     $stage = $row['stage'];

   echo "$stage  <br>";
 }
 ?>

更新:如何发送数据

optionScope.data().stage = 'b';
  $.ajax({
        url: "functions/contact.php",
        type: "post",
        data: {stage: optionScope.data().stage}
    });

如何检索数据

<?php
      $stage = $_POST['stage'];
      echo $stage;
?>

可以使用AJAX方法。这是将JS数据转换为PHP脚本的唯一方法。您将在get或POST变量中获取Jquery数据。jQuery AJAX有多种方法,您可以根据自己的需求选择。http://api.jquery.com/category/ajax/

你不能直接在JS和PHP之间传输数据(除非你在<script>标签中使用字符串插值,这是一个大禁忌),因为JS处理客户端,PHP处理服务器端。

您需要向服务器发出请求,然后在那里操作数据。

看一下AJAX请求。要了解最简单的实现方法,请参见JQuery的AJAX方法。