使用jQuery将值分配给php变量

我正在网站上使用按钮,每个按钮启动一个自举模态,包含来自mysql数据库的数据,我从jquery传递了一个变量,从而将模型启动到模态中的mysql查询中,问题是php变量无法获取数据从jquery发送给它任何提示吗?!

我通过jquery传递数据到ajax.php文件

模式代码

<div class="modal" id="myModal">

              <div class="modal-dialog">
                <div class="modal-content">

                  <!-- Modal Header -->
                  <div class="modal-header">
                    <span id="codeElem"></span>
                    <?php

                      $resultHead = mysqli_query($con2,"SELECT *  FROM coops WHERE Code = $getcode ");// WHERE Code IN ('".$codeArrayStr."')
                      ?>
                      <?php
                      $i=0;
                      $row3 = mysqli_fetch_array($resultHead);

                        ?>
                    <h4 class="modal-title"><?=$row3['CoopName'];?></h4>

                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                  </div>
                  <!-- Modal body -->
                  <div class="modal-body">
                    <?php
                    $result = mysqli_query($con,"SELECT DISTINCT *  FROM reports WHERE host = $getcode GROUP BY host DESC");
                    ?>
                    <?php
                    $i=0;
                    while($row = mysqli_fetch_array($result)) {
                      ?>

                      <div class="card card-figure has-hoverable">
                      <figure class="figure">
                      <img class="img-fluid" src="http://www.iroof.tv/screenshots/<?=$row['screenshot'];?>" alt="Card image cap">
                      <figcaption class="figure-caption">
                      <h6 class="figure-title"><?=$row['host'];?></h6>
                      <p class="text-muted mb-0"> <?=$row['timestamp'];?> </p>
                      <?php
                         // Assign JSON encoded string to a PHP variable
                         $statusJson = $row['status'];
                         // Decode JSON data into PHP associative array format
                         $arr = json_decode($statusJson,true);
                         // Call the function and print all the values
                      //   $result2 = printvalues($arr);
                         echo "<hr>";
                         echo "<h3> Player </h3>";
                         // Print a single value
                         echo "Status: ".$arr["player"]["status"] . "<br>";
                         echo $arr["player"]["filename"] . "<br>";
                         echo "<hr>";
                         echo "<h3> Graphics </h3>";
                         echo "Display: ".$arr["video"]["display"] . "<br>";
                         echo "Resolution: ".$arr["video"]["resolution"] . "<br>";
                         echo "Colors: ".$arr["video"]["colors"] . "<br>";
                         echo "<hr>";
                         echo "<h3> System </h3>";
                         echo "CPU: ".$arr["cpu"] . "<br>";
                         echo "Ram: ".$arr["ram"] . "<br>";
                         //echo "Temprature: ".$arr["temperature"] . "<br>";
                         echo "Fan: ".$arr["fan"] . "<br>";
                       ?>
                      </figcaption>
                      </figure>
                      </div>
                      <?php $i++;
                      }
                      ?>
                  </div>

                  <!-- Modal footer
                  <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                  </div>-->

                </div>

jQuery脚本

  <script>
    $(document).ready(
      function() {
        setInterval(function() {

          $('.card');
        },5000);  //Delay here = 5 seconds
         var gen;
        $(".btn").click(function(){
           gen = $(this).attr("data-code");

           //$.post("ajax.php",{"code": gen},function(data){console.log(data);});

       });
       $('#myModal').on('shown.bs.modal',function () {
         $.post("ajax.php",function(data){console.log(data);});
         //var phpCode = "<? $getcode = $_POST["code"]; ?>";
         //$('#codeElem').html(phpCode);
       })

        });
    </script>

ajax.php文件


<?php
if(isset($_POST['code']) && isset($_POST['code'])){
  //$_SESSION["code"] = $_POST["code"];
  $_SESSION['header'] = $_POST['code'];
  $getcode = $_POST["code"];
  echo   $_SESSION['header'];
}

?>

我希望变量$ getcode从jquery获取代码以完成模态内的mysql查询:

$resultHead = mysqli_query($con2,"SELECT *  FROM coops WHERE Code = $getcode"); 
skfywj 回答:使用jQuery将值分配给php变量

我认为不可能将变量发布到模式中,当我搜索弹出模式登录时,我没有看到类似的示例。 猜想您需要一个操作页面才能使信息变态。 这是发布变量的解决方案: 根据您的需要更改变量。

$(document).ready(function(){
    $("form").submit(function(event){
        event.preventDefault();
        var post_id =$("#mail-id").val();
        var name =$("#mail-name").val();
        var email =$("#mail-email").val();
        var message =$("#mail-message").val();
        var type =$("#mail-type").val();
        var captcha_code =$("#captcha_code").val();
        var submit =$("#mail-submit").val();
        $(".form-message").load("heads/comment.php",{
            post_id: post_id,name: name,email: email,message: message,type: type,captcha_code: captcha_code,submit: submit
        });
    });
});

您可能需要这样的东西才能在弹出窗口中显示变量

<p class="form-message"></p>    

或者您可以检查Bootstrap模式表: Here

本文链接:https://www.f2er.com/3129142.html

大家都在问