php - ajax $.get的data参数无法传递
PHP中文网
PHP中文网 2017-04-11 10:13:15
0
2
485
  1. 使用$.get方法,点击一次button获取两条数据,点击一次之后变量+1,之后把这个值传递到getajax.php页面的数据库查询语句中作为查询条件,如果把"page="+pageno改成page=1。程序能正常运行,但现在是"page="+pageno,运行getajax.php,首先提示“false”,就是var_dump($result)的结果,然后提示“Fatal error: Call to a member function fetch_assoc() on a non-object in ”,这个应该是$result出错了?后推应该是查询语句的错误吗,那么这个"page="+pageno应该怎么写才正确?求指教

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="jquery.js" type="text/javascript"></script>
    <title></title>
</head>
<body>
    <p id="showdata"></p>
    <!-- 点击一次,value值变化一次 -->
    <input type="hidden" id="currentresult" value="1" />
    <button id="show">Load</button>
    <script type="text/javascript">
        $(document).ready(function(){
            $(document).on('click','#show',function(e){
 
                var pageno = $('#currentresult').val(); 
                $.get("getajax.php","page="+pageno ,function(data){ 
                    
                    pageno++;
                    $('#currentresult').val(pageno); 
                    $('#showdata').append(data);                                          $('#showdata').html(data);
                    
                });
            });
        });

    </script>
</body>
</html>

getajax.php

<!-- This is ajax data. -->
<?php
    $con = mysqli_connect("localhost", "root", "", "maroon5");
    $page=isset($_GET["page"])?$_GET["page"]:0;
    $pageNo = $page;

    $startLimit = ($pageNo-1)*2;

    $query = "SELECT * from tour LIMIT $startLimit,2 ";
    $result = mysqli_query($con,$query);//如果查询失败,则返回false
    var_dump($result);

    while($row = $result -> fetch_assoc()){

    ?>
    <p>
        <h4>month:<?php echo $row["month"]; ?>day:<?php echo $row["day"]; ?></h4>
    </p>
<?php

    }

?>
PHP中文网
PHP中文网

认证高级PHP讲师

Antworte allen(2)
巴扎黑

ajax中的$.get不是这么传参数的,第二个参数是对象

把这一行

$.get("getajax.php","page="+pageno ,function(data){

改成

$.get("getajax.php", {page: pageno}, function(data){
小葫芦
while($row = $result -> fetch_assoc()){

echo "<p>
        <h4>month:{$row['month']}day:{$row['day']}</h4>
    </p>";

    }

里边加?>的话就结束了,所以会报错。这种一般是用双引号echo吧,因为双引号是解析变量的,带花括号更保险。当然我php也是新手,如果还有问题烦请指出,谢谢~

参考

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!