简体   繁体   English

如何将表单的值传递给另一个表单 PHP?

[英]How to pass value of a form to another form PHP?

I have a form A in index.php which I need to pass the value of 'notebook_id' to formB in create_new_note.php.我在 index.php 中有一个表单 A,我需要将“notebook_id”的值传递给 create_new_note.php 中的 formB。 I used POST method to do this but it is not working.我使用 POST 方法来做到这一点,但它不起作用。

form A in index.php 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>

Form B表格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>

I used MVC framework, hence this is my Controller code and Model code我使用了 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;
}

Please help me, I'm new to this I tried to make way through it, but cant seem to figure out whats wrong请帮助我,我是新手,我试图通过它,但似乎无法弄清楚出了什么问题

As I see your code is creating a new row in the database.正如我所看到的,您的代码正在数据库中创建一个新行。 After saving row into database you want to know the last inserted id of the entity called Notebooks.将行保存到数据库中后,您想知道名为 Notebooks 的实体的最后插入id Reuse that value and pass it into your template of creating titles.重用该值并将其传递到创建标题的模板中。 If your MVC framework does not know the last inserted id, you should customize it and add functionality to do that.如果您的 MVC 框架不知道最后插入的 id,您应该自定义它并添加功能来执行此操作。 Check last_inserted_id检查last_inserted_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM