dongluo1853 2019-02-04 05:45
浏览 457
已采纳

获取select选项的值而不刷新页面

My problem is very simple.

This is my code:

<?php 
    include "../../../koneksi.php";  
?>
<html>
<body>
<form action="" method="POST">
    <?php  
        if($_POST['menu'] === "lunchmenu"){
            $result = mysqli_query($conn,"SELECT * FROM tb_lunch");
        } else{
            $result = mysqli_query($conn,"SELECT * FROM tb_dinner");
        }
    ?>
    <table>
        <tr>
            <td>Kategori Menu</td>
            <td>
                <select name="menu" onchange="this.form.submit();">
                    <option selected="true" disabled>-- Menu Category --</option>
                    <option value="lunchmenu">Lunch Menu </option>
                    <option value="dinnermenu">Dinner Menu </option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Menu Name</td>
            <td>
                <select>
                    <?php  
                    if($_POST['menu'] === "lunchmenu")
                        $i = 1;
                        while ( $row = mysqli_fetch_assoc($result)):
                    ?>
                    <option> <?=$row[menu_name]; ?></option>
                    <?php       
                        $i++;
                        endwhile; 
                    ?>
                    <?php
                    if($_POST['menu'] === "dinnermenu")
                        $i = 1;
                        while ( $row = mysqli_fetch_assoc($result)):
                    ?>
                    <option> <?=$row[menu_name]; ?></option>
                    <?php       
                        $i++;
                        endwhile; 
                    ?>
                </select>
            </td>
        </tr>
    </table>
</form>
</body>
</html>

The results that I want is very simple. When I choose "Lunch Menu" from --Menu Category--, it will show me the available lunch menu. I got no problem in here. My problem is: After I choose a menu (lunch or dinner), the page will refresh and it will be back just like the default value of --Menu Category--, though it shows the correct menu below.

I think the problem is at onchange="this.form.submit();

Do you guys has any solution for this?

  • 写回答

2条回答 默认 最新

  • doujiaochan7317 2019-02-04 06:14
    关注

    You can maintain two empty variables one for lunchmenu and another for dinner menu, and one for defaultmenu, change these variables to "selected" and "" based on the POST variable as below.

    $lunchmenu changes from "" to "selected" when $_POST['menu'] === "lunchmenu"
    $dinnermenu changes from "" to "selected" when $_POST['menu'] === "dinnermenu"

    <?php 
            include "../../../koneksi.php";  
        ?>
        <html>
        <body>
        <form action="" method="POST">
            <?php  
                $defaultmenu = 'selected';
                $lunchmenu = '';
                $dinnermenu = ''; 
                if($_POST['menu'] === "lunchmenu"){
        $lunchmenu = "selected"';
        $defaultmenu  = '';
                    $result = mysqli_query($conn,"SELECT * FROM tb_lunch");
                } else{
                    $dinnermenu = "selected"';
    $defaultmenu  = '';
                    $result = mysqli_query($conn,"SELECT * FROM tb_dinner");
                }
            ?>
            <table>
                <tr>
                    <td>Kategori Menu</td>
                    <td>
                        <select name="menu" onchange="this.form.submit();">
                            <option <?php echo $defaultmenu ;?> disabled >-- Menu Category --</option>
                            <option value="lunchmenu" <?php echo $lunchmenu;?> > Lunch Menu </option>
                            <option value="dinnermenu"  <?php echo $dinnermenu;?>  > Dinner Menu </option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Menu Name</td>
                    <td>
                        <select>
                            <?php  
                            if($_POST['menu'] === "lunchmenu")
                                $i = 1;
                                while ( $row = mysqli_fetch_assoc($result)):
                            ?>
                            <option> <?=$row[menu_name]; ?></option>
                            <?php       
                                $i++;
                                endwhile; 
                            ?>
                            <?php
                            if($_POST['menu'] === "dinnermenu")
                                $i = 1;
                                while ( $row = mysqli_fetch_assoc($result)):
                            ?>
                            <option> <?=$row[menu_name]; ?></option>
                            <?php       
                                $i++;
                                endwhile; 
                            ?>
                        </select>
                    </td>
                </tr>
            </table>
        </form>
         
        <script>
          $( document ).ready(function() {
            $('html, body').animate({
              scrollTop: $("#menuform").offset().top
              }, 2000);
              return false;
          });
        </script>
        </body>
        </html>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能