duanluan3651 2017-03-30 08:48
浏览 55
已采纳

HTML通过Ajax在同一页面上将所选选项发送到PHP

What I want to acheive: I have a HTML/PHP page where I display the home page of the website. I have a highscore section on this page. The data for the highscore page is retrived from a database. There is a Select box where you can choose how the highscores are sorted.

The highscore section on the page

I want to be able to select an option from the dropdown. When an option is selected, the way that the data is displayed changes dynamically, without the page refreshing.

Here is the code on the home.php page (Where the highscore section is)

<div class="third third-center">
            <h2>Highscores</h2>
            <div class="sortby">
                <form id="" method="post" action="home.php">
                    <select id="selectForm" name="sort">
                      <option value="score ASC">Score Ascending</option>
                      <option value="score DESC">Score Descending</option>
                      <option value="userName ASC">Name Ascending</option>
                      <option value="userName DESC">Name Descending</option>
                    </select>
                </form>
            </div>
            <div class="inner">
                <table>
                    <tr>
                        <th style="font-size: 20px">Position</th>
                        <th style="font-size: 20px">Name</th>
                        <th style="font-size: 20px">Score</th>
                    </tr>
                    <?php
                        $i = 0;

                        $choice = "score ASC";

                        $search = mysqli_query($connect, "SELECT * FROM accounts ORDER BY ".$choice."") or die(mysqli_error($connect));


                        while($row = mysqli_fetch_array($search)){
                            $i++;
                            echo "<tr>";
                            echo "<th>" . $i . "</th>";
                            echo "<th>" . $row['userName'] . "</th>";
                            echo "<th>" . $row['score'] . "</th>";
                            echo "</tr>";
                        }
                    ?>
                </table>
            </div>
        </div>

I am using the Mysqli_Query to get all the data from the database. I am passing in a variable in place of the ORDER BY so that I can change the way the data is retrieved.

I know I can use Ajax to get the data, but Im not sure how to do it, I have looked at many other questions posted on this forum.

Any help would be greatly appreciated!

  • 写回答

4条回答 默认 最新

  • dounie5475 2017-03-30 08:57
    关注

    Use JQuery AJAX something like so:

    Make two pages one where you display data(index.php) and other from where you fetch data (process.php).

    The content of index.php would be something like:

    <div class="third third-center">
                <h2>Highscores</h2>
                <div class="sortby">
                    <form id="" method="post" action="home.php">
                        <select onChange="getData();" id="selectForm" name="sort">
                          <option value="score ASC">Score Ascending</option>
                          <option value="score DESC">Score Descending</option>
                          <option value="userName ASC">Name Ascending</option>
                          <option value="userName DESC">Name Descending</option>
                        </select>
                    </form>
                </div>
                <div class="inner">
                    <table>
                        <thead>
                        <th>
                            <th style="font-size: 20px">Position</th>
                            <th style="font-size: 20px">Name</th>
                            <th style="font-size: 20px">Score</th>
                        </th>
                        </thead>
                        <tbody id="data"></tbody>
                    </table>
                </div>
            </div>
    

    The code of process.php should be something liek:

                <?php
                    $i = 0;
                    if($_REQUEST['sortBy'] == "score ASC")
                    {
                    $choice = "score ASC";
                    }
                     else if ()//and so on
    
                    $search = mysqli_query($connect, "SELECT * FROM accounts ORDER BY ".$choice) or die(mysqli_error($connect));
    
    
                    while($row = mysqli_fetch_array($search)){
                        $i++;
                        echo "<tr>";
                        echo "<th>" . $i . "</th>";
                        echo "<th>" . $row['userName'] . "</th>";
                        echo "<th>" . $row['score'] . "</th>";
                        echo "</tr>";
                    }
                ?>
    

    Your Javascript should be like:

                function getData()
                {
                $.ajax({
                    url     : 'process.php',
                    method    : 'GET',
                    data:{ sortBy:$('#selectForm').val() }
                    success   : function(response)
                    {
                       $('#data').html(response);
                    },
                    error : function(e)
                    {
                        alert('error');
                    }
                });
              }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改