游.程 2017-04-11 18:31 采纳率: 0%
浏览 63

AJAX随机数

I would like to practice on using AJAX. I have a MySQL database, and I have inserted some names in the column storeName. I made a SQL query that output random text in column in the database. That is working fine, but only when I refresh my browser:

<?php
    $sql ="SELECT * FROM stores ORDER BY RAND ( ) LIMIT 1";
    $res = $mysqli->query($sql);
    //print($res);
    if ($res->num_rows > 0) {
    // output data of each row
    while($row = $res->fetch_assoc()) {
        echo "Storename: " . $row["storeName"]. "<br>" .     
    }
} else {
    echo "0 results";
}
?>

I would like to make an AJAX function, that calls my SQL query without I have to refresh my browser. I tried to make the following code, but when I hit the shuffle button, nothing is happening. What am I doing wrong here?

<body>
<div id="shuffle" >
  <h4>Shuffle Stores</h4>
    <!-- This php function loads everytime I refresh the browser -->
    <?php include 'function/select_shuffle.php' ?>

      <form action="function/select_shuffle.php" method="post">
        <button>shuffle</button>
      </form>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <!-- Shuffle AJAX method-->
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                $.ajax({url: "function/select_shuffle.php", success: function(result){
                    $("#shuffle").html(result);
                }});
            });
        });
    </script>
</body>

AJAX

@Sehdev: AJAX_1

  • 写回答

2条回答 默认 最新

  • weixin_33682719 2017-04-11 18:43
    关注

    You don't need to include select_shuffle.php file in your current page if you are making an ajax call.

    Please try the following code. Also you can check your console for any ajax error. Right click on your page and choose Inspect. Then click on "Network" tab and after that click on XHR heading. Here you can track your ajax request. You can check header, response or any ajax error.

    <body>
    <div id="shuffle" >
    </div>
    <div>
      <h4>Shuffle Stores</h4>
          <button id="btn">shuffle</button>
    </div>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <!-- Shuffle AJAX method-->
        <script>
            $(document).ready(function(){
                $("#btn").click(function(){
                    $.ajax({
                        url: "function/select_shuffle.php", 
                        success: function(result)
                        {
                            $("#shuffle").html(result);
                        }
                    });
                });
            });
        </script>
    </body>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示