【问题标题】:Fetch dynamically selected data from database using ajax php使用ajax php从数据库中获取动态选择的数据
【发布时间】:2019-01-02 18:46:56
【问题描述】:

我的 view.php 页面通过获得一个唯一的序列号来自另一个页面。现在在我的 view.php 页面中,我想显示来自charts.php 的指定序列号数据。我自己完成了我的代码。它正在获取数据。但不是选择的序列号。我该如何解决这个问题

view.php

<?php 
if(isset($_GET['serial'])){
        $serial = $_GET['serial'];
?>
<html>
<div class="container" id="output"></div>
</html>
<script>
    $(document).ready(function(){
        function getData(){
            $.ajax({
                type: 'POST',
                url: 'charts.php',
                success: function(data){
                    $('#output').html(data);
                }
            });
        }
        getData();
        setInterval(function () {
            getData(); 
        }, 1000);  // it will refresh your data every 1 sec

    });
</script>

charts.php

<?php 
   $sql = mysqli_query($con,"SELECT * FROM criminal WHERE rand = '$serial'");
    while($row = mysqli_fetch_assoc($sql)){
 ?>

请帮忙。

【问题讨论】:

  • 你永远不会向 PHP 脚本发送 $serial
  • 如何在 ajax 的 php 脚本中发送 $serial 变量
  • AJAX 请求需要一个data: 属性。
  • 数据:$("#whatever_your_form_id_is").serialize()
  • 卫生署!我看到了“POST”并思考了形式。我的错。没有注意到顶部的查询字符串。

标签: php html ajax mysqli


【解决方案1】:
  1. 首先,关闭你的第一个if(你忘了}
  2. 在 ajax 中,在 url 行之后,发送串行参数。 (data: {serial: "echo with php the variable"},)
  3. 在charts.php 查询中,获取post 值。 ($_POST['serial'])。

【讨论】:

  • 像这样:data: ({serial: ""}),
  • 在 url: 'charts.php' 之后,在下一行数据: {serial: ""},
  • IRGeekSauce的版本,用get发送序列也不错。
  • @Jay Blanchard 我所说的与表格无关。
  • 不是,但你说的是“发送序列号”。我的错,我误解了你的意思
【解决方案2】:

您想将 $serial 变量放在您的 url 中。这称为查询字符串。

$(function() {
    function getData(){
            $.ajax({
                type: 'POST',
                dataType: 'JSON',
                url: 'charts.php?serial=<?= $serial?>', //<-- RIGHT HERE
                success: function(data){
                    $('#output').html(data);
                }
            });
        }
})

然后你会得到你刚刚从 ajax 发送的数据。它看起来像您的第一个 GET 变量。您将在查询中使用该变量。

在你的 php 中:

<?php 
   $your_variable = $_GET['serial'];
   $sql = mysqli_query($con,"SELECT * FROM criminal WHERE rand = '$your_variable'");
    while($row = mysqli_fetch_assoc($sql)){
        $variable_to_send = $row['serial']; //<--- Whatever your column name is
    }
    echo json_encode($variable_to_send);
 ?>

PDO 版本 正如@JayBlanchard 所建议的那样,强烈建议您对 PDO 进行一些研究。它更安全。

我会给你一个 PDO 的例子:

$serial = $_GET['serial']; //The variable you're sending over from view.php

$hostname = 'your_hostname';
$username = 'your_username';
$password = 'your_passwd';
$dbname = 'your_db_name';

$pdo = new PDO("mysql:host=$hostname;$dbname=$dbname", $username, $password); //Create a new PDO object
$stmt = $pdo->prepare("SELECT * FROM criminal WHERE rand = :rand"); //prepare the query for execution
$stmt->bindValue(':rand', $serial); //bind your variable to your query
$stmt->execute(); //Run it
$result = $stmt->fetchColumn(); //Get a single column. No while loop.

echo json_encode($result); //Echo it back to your ajax function

我针对我自己的数据库对此进行了测试,它在我的屏幕上显示了没有错误的结果(当然是使用我自己的值)。

【讨论】:

猜你喜欢
  • 2019-11-26
  • 1970-01-01
  • 2015-04-29
  • 2014-05-03
  • 1970-01-01
  • 2017-03-04
  • 2012-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多