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条)

报告相同问题?

悬赏问题

  • ¥15 爬虫爬取网站的一些信息
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错