doufulian4076 2018-09-23 12:00
浏览 39

如何使用选项标记在URL上显示链接

I am trying to do a forum and trying to create a section where admin can create categories and sub categories. I have got my category page to work with a drop down menu but I am not sure how to get it to display the category id in the url? I thought that I might have to type something in the option value tag but it automatically sends me to the link once I have selected the tag instead of waiting for me to press the submit button:

    <?php
include_once 'header2.php';
if(!$_SESSION['u_uid']) {
  echo "<meta http-equiv='refresh' content='0;url=index.php?create_music_sub=notlogin>"; 
   exit();
} else {
if($_SESSION['u_permission'] == 0){
    echo "<meta http-equiv='refresh' content='0;url=header2.php?create_music_sub=nopermission'>"; 
    exit();
} else {
  $admin = 0;
    $date = date("Y-m-d H:i:s");


    $sql = "SELECT * FROM music_forum_cats WHERE admin = ?;";

                 $stmt = mysqli_stmt_init($conn);

                if(!mysqli_stmt_prepare($stmt, $sql)) {
                       echo "SQL error";
                    } else {
                     mysqli_stmt_bind_param($stmt, "i", $admin);
                      mysqli_stmt_execute($stmt);

                 $result = mysqli_stmt_get_result($stmt);
                      $resultCheck = mysqli_num_rows($result);

                 echo 'You have'.$resultCheck.'results';



                 echo '<form action="create_music_sub_process.php" method="POST">';

                 echo '<table class="create_music_sub">

                      <tr>
                      <th>Category</th><td>';
                     echo '<select><option disabled selected>Pick a Category</option>';
                  while($row = mysqli_fetch_assoc($result)) {

                    $id = $row['id'];

                      echo '<option value="create_music_sub.php?cat='.$id.'">'.$row['category'].'</option>' ;

                      }

                      echo '</select></td>';


                      '</tr>';




                  echo '
                      <tr>
                      <th>Sub Category</th><td><input type="text" name="sub_cat"></td>
                      </tr>
                      <tr>
                      <th>Creator</th><td>'.$_SESSION["u_uid"].'</td>
                      </tr>
                      <tr>
                      <th>Date Created</th><td><input type="text" name="date" value='.$date.'></td>
                      </tr>
                      <tr>
                      <th></th><td colspan="2"><input type="submit" name="submit" value="Create Sub Category"></td>
                      </tr>
                      </table>
                    </form>';

                } 

                  }
              }
            ?>

enter image description here

I have changed the following but shouldn't I be using $row? instead of $post?

echo '<form action="create_music_sub_process.php" method="POST">';

             echo '<table class="create_music_sub">

                  <tr>
                  <th>Category</th><td>';
                 echo '<select name="create_music_sub"><option disabled selected>Pick a Category</option>';
              while($row = mysqli_fetch_assoc($result)) {

                $id = $row['id'];

                  echo '<option value="'.$id.'">'.$POST['create_music_sub'].'</option>' ;

                  }

                  echo '</select></td>';


                  '</tr>';
  • 写回答

1条回答 默认 最新

  • duanpendan8067 2018-09-23 13:26
    关注

    If you are not using an JS you will need to define the name parameter of the select and value of the option elements.

    <select name="POST_NAME"> and <option value="'.$id.'" >

    The form will direct you and the form data to create_music_sub_process.php, where you can access the data stored in the $_POST variable.

    To access the select elements data use <?php echo $_POST['POST_NAME']; ?>

    If you want to redirect on selecting the element, with jquery you could do something like:

    <script type="text/javascript">
        $(function(){
            // on change of the element, send user to 
            $("#idToTarget").on('change', function(){
                // change window location to the url
                window.location.href = 'http://example.com/id=' + $('select' . $this).val();;
            });
        });
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用