【问题标题】:How to pass value of a form to another form PHP?如何将表单的值传递给另一个表单 PHP?
【发布时间】:2021-10-31 09:54:32
【问题描述】:

我在 index.php 中有一个表单 A,我需要将“notebook_id”的值传递给 create_new_note.php 中的表单 B。我使用 POST 方法来执行此操作,但它不起作用。

index.php 中的表单 A

    <!-- Modal -->
    <div class="modal" id="newNotebookModal" tabindex="-1" aria-labelledby="newNotebookModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <center><h5 class="modal-title" id="newNotebookModalLabel">Create a New Notebook</h5></center>
            </div>
            <div class="modal-body">
                <form class="forms-sample" method="post">
                    <div class="form-group">
                        <label for="notebook_name_label">Notebook Name</label>
                        <input type="text" class="form-control" name="notebook_name" id="notebook_name" placeholder="Notebook Name">
                    </div>
            </div>
            <div class="modal-footer">
              <button class="btn btn-outline-secondary btn-fw">Cancel</button>
                <input type="submit" class="btn btn-primary" name="submit" value="Create"/>
            </div>
                </form>
            </div>
        </div>
    </div>
    </div>

表格 B

<form class="forms-sample" method="post">
   <div class="form-group">
      <label for="note_title_label">Title</label>
      <input type="hidden" name="notebook_id" value="<?php echo $notebook_id; ?>">
      <input type="text" class="form-control" name="note_title" id="note_title" placeholder="Title">
    </div>
     <div class="form-group">
         <textarea class="form-control" name ="note_content"id="note_content" rows="40"></textarea>
     </div>
     <button class="btn btn-outline-secondary btn-fw">Cancel</button>
     <input type="submit" class="btn btn-primary" name="submit" value="Create"/>
</form>

我使用 MVC 框架,因此这是我的控制器代码和模型代码

// Controller

function addNotebook($std_id) {
    $notebook = new ManageNotesModel();
    $notebook->std_id = $std_id;
    $notebook->notebook_name = $_POST['notebook_name'];
   
    if($notebook->addNotebook() > 0) {
      $message = "Your notebook has been created!";
      echo "<script type='text/javascript'>alert('$message');
      window.location = '../ManageNotesView/create_new_note.php'</script>";
    }
  }
// Model
function addNotebook(){
  $sql = "insert into notebook(std_id,  notebook_name)values(:std_id, :notebook_name)";
  $args = [':std_id'=> $this->std_id, ':notebook_name'=>$this->notebook_name];
  $stmt = DB::run($sql, $args);
  $count = $stmt->rowCount();

  return $count;
}

请帮助我,我是新手,我试图通过它,但似乎无法弄清楚什么问题

【问题讨论】:

  • 如果您从一个网页导航到另一个网页,您可以将数据保存在查询字符串或会话中,然后在其他页面中检索

标签: php model-view-controller


【解决方案1】:

我看到您的代码正在数据库中创建一个新行。将行保存到数据库后,您想知道最后插入的名为 Notebooks 的实体的id。重用该值并将其传递到创建标题的模板中。如果你的 MVC 框架不知道最后插入的 id,你应该自定义它并添加功能来做到这一点。 检查last_inserted_id

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 2017-06-01
    相关资源
    最近更新 更多