【问题标题】:How to display the result of my php in ajax如何在ajax中显示我的php结果
【发布时间】:2014-07-17 05:14:29
【问题描述】:

我想做的是在我的索引页面中显示我的 count.php 的结果。我的计划是每次访问或查看页面时自动计算 user_code 的行数并在索引页面中显示结果。

我的问题是 ajax 脚本没有收到 count.php 的结果来显示在 index 页面的 count 输入框中。

索引页面:

<input type="text" value="1" name="countValue" id="countValue" style="width: 12px;" /><br />
Count: <input type="text" name="count" id="count" readonly="readonly" /><br /><br /><br />

<script>
$(document).ready(function(){
var countTimer = setInterval(
        function ()
        {
        codeValue();        
        }, 500);



var  $count = $('#count');

function codeValue(){
        $.ajax({
        url:"count.php",
        type:"GET",
        data: { term : $('#countValue').val() },
        dataType:"JSON",
        success: function(result) {

         $("#count").val(result.user_code);

        }

    });

};

});      
</script>

count.php

<?php
error_reporting(-1);
ini_set('display_errors', 'On');

$host = "localhost";
$user = "root";
$pass = "";
$db = "test";

$dbc = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo $_GET['term'];

if (isset($_GET['term'])) {

$q = $_GET['term'];

$sql = "SELECT user_code FROM students";
$query = $dbc->prepare($sql);
$query->execute();
$num_rows = $query->rowCount(); 

echo json_encode(array('user_code'=>$num_rows)); 

}

?>

【问题讨论】:

  • 你得到什么错误信息?
  • 请在控制台检查错误
  • 您的 count.php 页面的响应是什么??
  • 也去掉这一行var $count = $('#count');
  • @sharif 除了未被使用外,不会导致 OP 的问题

标签: php jquery html ajax pdo


【解决方案1】:

你的问题

echo $_GET['term'];

这会破坏您的脚本要生成的预期 JSON 格式。另外,您甚至没有使用任何请求参数,所以我不知道您为什么还要打扰。

这是你的 PHP,全部清理完毕

<?php

try {
    $dbc = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', '',
        [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);

    $stmt = $dbc->query('SELECT COUNT(1) FROM students');

    header('Content-type: application/json');
    echo json_encode(['user_code' => $stmt->fetchColumn()];
} catch (Exception $e) {
    http_response_code(500);
    echo $e->getMessage();
}

我还建议在您的 $.ajax 选项中添加一个 error 回调来处理 HTTP 错误。

【讨论】:

  • 哦,问题是 echo $_GET['term'] 我忘了删除它,因为我试图测试 count.php 是否接收到数据。
【解决方案2】:

我试试你的代码,它适用于我的表,我有 (4) 条记录!检查您的查询是否有数据

确保您的标题中有 jquery 文件。我没有更改任何您的 html 文件。我只是在我的表上创建自己的查询。那好吧。

【讨论】:

    【解决方案3】:

    您需要在indexfile中创建一个div,并且该div的id在ajax文件中提供,以便在该div中填充数据表单count.php。

    这里我会给你一个类似的例子,通过你的相关更改来试试

    index.php

    <div id="content">                        
    </div>                        
        <script type="text/javascript">
            $(document).ready(function(){ 
        $.ajax({
          url: 'count.php',
          success: function(data) {
            $('#content').html(data);
          }
        });
    <script>
    

    count.php

    <?php
    error_reporting(-1);
    ini_set('display_errors', 'On');
    
    $host = "localhost";
    $user = "root";
    $pass = "";
    $db = "test";
    $con=mysqli_connect($host,$user,$pass,$db);
    $result = mysqli_query($con,"SELECT user_code FROM students");
    ?>
    <table>//this table will be loaded in div
        <?php
        while($row = mysqli_fetch_array($result))
        {
        ?>
            <tr>
                <td><?php echo $row['user_code']; ?></td>
            </tr>
        <?php
        }
        ?>
    </table>
    

    您还可以添加自动刷新 ajax 脚本,它只会在特定时间间隔后(以下代码中为 1 秒)重新加载特定的 div,而不重新加载整个页面

    <script type="text/javascript">
    var auto_refresh = setInterval(
    function ()
    {
    $('#content').load('count.php').fadeIn("slow");
    }, 1000); // refresh every 10000 milliseconds
    
    </script> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 2015-06-09
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 2021-10-21
      • 2017-01-25
      相关资源
      最近更新 更多