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

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?