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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建