dongzhang0418 2016-10-30 04:28
浏览 47
已采纳

在PHP / MySQL中使用下拉菜单预填充表单?

I am creating a simple CRUD application that will be used as a blog. For the edit page, I want to have a dropdown menu with the blog titles of each post. When an option/blog post is selected, I want it to populate the "Title" and "Message" fields, so it can then be edited and saved to the database.

I got it to retrieve the titles of the blog posts, but I am struggling to make it populate the "Title" and "Message" fields so it can be edited when the option is selected.

I have 4 rows in my database: row[0] is the title, row[1] is the message, row[2] is the timestamp and row[3] is the ID.

Thanks guys. I appreciate it.

    <form action="edit.php" id="myform" method="post" autocomplete=off>
      <input type="hidden" name="action" value="show">

      <p><label>Entry:</label><select name="blog">
      <?php

          $result = mysqli_query($con, "SELECT * FROM blog");
          while ($row = mysqli_fetch_array($result)) {
              $chosen = $row['bid'];
          }

          if (isset($_GET['blog'])) {
                $id = $_GET['blog'];
                $result = mysqli_query($con, "SELECT * FROM blog WHERE bid='$id'");
                $row = mysqli_fetch_array($result);
          }

          $result = mysqli_query($con, "SELECT * FROM blog");
          while ($row = mysqli_fetch_array($result)) {
              $id = $row['bid'];
              $title = $row['title'];

              $selected = '';
              if ($id == $chosen) {
                  $selected = "selected='selected'";
              }

              echo "<option value='$id' $selected>$title</option>
";
          }

      ?>
    </select></p>

  <p><label>Title:</label> <input type="text" id="newtitle" name="newtitle" value="<?php echo $row[0]; ?>"></p>
  <p><label>Message:</label> <input type="text" id="newmessage" name="newmessage" value="<?php echo $row[1]; ?>"></p>
  <p><input type="hidden" name="id" value="<?php echo $row[3]; ?>"></p>

  <br><p><input type="submit" name="submit" value="Submit"></p>
</form>
  • 写回答

1条回答 默认 最新

  • doubijiao2094 2016-10-30 05:52
    关注

    I have managed to somewhat figure out the answer on my own. It's probably not the most ideal way of doing it, but for anyone else potentially stuck on this - here is my code:

    The only issue I'm having now is figuring out how to keep my option selected.

        <form action="edit.php" id="myform" method="post" autocomplete=off>
          <input type="hidden" name="action" value="show">
    
          <p><label>Entry:</label><select name="blog" onchange="window.location.href = blog.options[selectedIndex].value">
              <option selected disabled>Select One</option>
          <?php
              $result = mysqli_query($con, "SELECT * FROM blog");
              while ($row = mysqli_fetch_array($result)) {
                  $id = $row['bid'];
                  $title = $row['title'];
    
                  echo "<option value='edit.php?edit=$row[bid]'>$title</option>
    ";
              }
    
              if (isset($_GET['edit'])) {
                    $id = $_GET['edit'];
                    $result = mysqli_query($con, "SELECT * FROM blog WHERE bid='$id'");
                    $row = mysqli_fetch_array($result);
              }
    
          ?>
        </select></p>
    
      <p><label>Title:</label> <input type="text" id="newtitle" name="newtitle" value="<?php echo $row[0]; ?>"></p>
      <p><label>Message:</label> <input type="text" id="newmessage" name="newmessage" value="<?php echo $row[1]; ?>"></p>
      <p><input type="hidden" name="id" value="<?php echo $row[3]; ?>"></p>
    
      <br><p><input type="submit" name="submit" value="Submit"></p>
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 高有偿提问!求优化设计微信小程序
  • ¥15 matlab在安装时报错 无法找到入口 无法定位程序输入点
  • ¥15 收益高的广告联盟有哪些
  • ¥15 Android Studio webview 的使用问题, 播放器横屏全屏
  • ¥15 删掉jdk后重新下载,Java web所需要的eclipse无法使用
  • ¥15 uniapp正式环境中通过webapi将本地数据推送到设备出现的跨域问题
  • ¥15 xui建立节点,显示错误
  • ¥15 关于#单片机#的问题:开始、复位、十进制的功能可以实现,但是切换八进制的功能无法实现(按下按键也没有效果),把初始状态调成八进制,也是八进制可以实现但是切换到十进制不行(相关搜索:汇编语言|计数器)
  • ¥15 VINS-Mono或Fusion中feature_manager中estimated_depth是特征的深度还是逆深度?
  • ¥15 谷歌浏览器如何备份抖音网页数据