繁体   English   中英

AJAX 将变量传递给 PHP

[英]AJAX Passing Variable to PHP

智慧就此结束。 I'm trying to use AJAX to pass a variable (table) over to PHP ($Table) to control a parameter of a SQL Query, but PHP outputs the variable ($Table) as null.

我在这里做错了什么?

<?php $Table = $_POST[table];?>

<p class="tab">The number of records in the <?php echo $Table?> table is: 
   <?PHP
     $sql="SELECT * FROM $Table";
     $result=mysqli_query($connect,$sql);
     $records=mysqli_num_rows($result);
     echo($records);
   ?>
</p>

<script>
  $(document).ready(function(){
    var table = "Members";

    $.ajax({
    method:"POST",
    data:{table:table},
    });
    });

  </script>

PHP 代码:

<?php
   if(isset($_GET["table"])){
     $table = $_GET["table"];
     echo "<h2>".$table."</h2>";
   }
?>

jQuery / AJAX 代码:

<script>
  $(document).ready(function(){
    var table = "Members";
    $.ajax({
    type:"GET",
    data:{table:table},
    success: function( data ) {
            console.log( data );
        }
    });
    });
</script>

暂无
暂无

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

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