从模态、Codeigniter插入数据

2w3rbyxf  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(189)

我试图插入来自模态的输入数据,但我不知道如何实现。我对 AJAX 不是很熟悉(我为此道歉)。我做了一些阅读和研究,但我感到困惑。以下是我的模态内容reserve_form.php

<form action="" id="book-form">
        <div class="row flex-lg-row-reverse align-items-center pt-1">
            <h3>Reservation Details</h3>
            <div class="col-10 col-sm-8 col-lg-6">
                <input type="hidden" name="package_id" value="<?php  ?>">
                <div class="form-group">
                    <label for="date_start" class="control-label">Reserve Date:</label>
                    <input type="date" name="date_start" id="date_start" class="form-control form-conrtrol-sm rounded-0" min="<?php echo date("Y-m-d") ?>" value="<?php echo date("Y-m-d") ?>" required>
                </div>
                <div id="msg" class="text-danger"></div>
                <div id="check-availability-loader" class="d-none">
                    <center>
                        <div class="d-flex align-items-center col-md-6">
                            <strong>Checking Availability...</strong>
                            <div class="spinner-border ml-auto" role="status" aria-hidden="true"></div>
                        </div>
                    </center>
                </div>
                <div class="form-group">
                    <label for="quantity" class="control-label">Number of Person <span><i>(Max. of 15 person)</i></span></label>
                    <input type="number" class="form-control form" required name="person" value="">
                </div>
                <div class="form-group">
                    <label for="amount" class="control-label">Additional Inclusions</label>
                    <div>
                        <input type="checkbox" id="inclusion" name="inclusion" value="">
                        <label for="inclusion"> Unlimited Samgyupsal &amp; Al Fresco dining experience</label>
                    </div>
                </div>

                <div class="form-group">
                    <label for="amount" class="control-label">Total Amount</label>
                    <input type="number" name="cost" id="amount" class="form-control form-conrtrol-sm rounded-0 text-right" value="<?php echo isset($cost) ? number_format($cost, 2) : 0.00 ?>" required readonly>
                </div>

            </div>
            <div class="col-lg-6">
                <div class="form-group">
                    <label for="name" class="control-label">First Name</label>
                    <input type="text" class="form-control mb-3" name="firstname" placeholder="Enter First Name" value="">
                </div>
                <div class="form-group">
                    <label for="name" class="control-label">Last Name</label>
                    <input type="text" class="form-control mb-3" name="lastname" placeholder="Enter Last Name" value="">
                </div>
                <div class="form-group">
                    <label for="name" class="control-label">Contact</label>
                    <input type="text" class="form-control mb-3" name="contact" placeholder="09xx-xxx-xxxx" value="">
                </div>
                <div class="form-group">
                    <label for="name" class="control-label">Email</label>
                    <input type="text" class="form-control mb-3" name="email" placeholder="example@email.com" value="">
                </div>
            </div>
        </div>
        <div class="modal-footer py-2">
            <button class="btn btn-warning" type="submit" name="details">Save</button>
            <button class="btn btn-secondary "><span class="close-sm">Close</span></button>
        </div>
    </form>

这是我的模式view.php

<!-- Modal content -->

<div class="modal-content">
    <div class="modal-header py-1">
        <h3><?php echo $packages->title; ?></h3>
    </div>
    <div class="modal-body py-2">
        <?php $this->load->view('forms/reserve_form'); ?>
        
    </div>
</div>

这里是我的控制器(page.php)

public function reservation_details()
  {
    $details = array();
    $details2 = array();

    if ($this->input->post('details')) {
      $this->form_validation->set_rules('firstname', 'First Name', 'required');
      $this->form_validation->set_rules('lastname', 'Last Name', 'required');
      $this->form_validation->set_rules('contact', 'Contact', 'required');
      $this->form_validation->set_rules('email', 'Email', 'required');
      $this->form_validation->set_rules('date_start', 'Reserve Date', 'required');
      $this->form_validation->set_rules('person', 'Number of Person', 'required|max_length[15]');

      if ($this->form_validation->run() == true) {
        $details = array(
          'firstname' => $this->input->post('firstname'),
          'lastname' => $this->input->post('lastname'),
          'contact' => $this->input->post('contact'),
          'email' => $this->input->post('email'),
        );
        $this->UI_model->reserve_details($details);

        $details2 = array(
          'date_start' => $this->input->post('date_start'),
          'person' => $this->input->post('person'),
          'inclusion' => $this->input->post('inclusion'),
        );
        $this->UI_model->reserve_add($details2);
      }
    }
  }

以下是模型

public function reserve_details(){
        if (!empty($data)) {
            $this->db->insert('clients', $data);

            return $this->db->insert_id();
        }
        return false;
    }

    public function reserve_add(){
        if (!empty($data)) {
            $this->db->insert('book_list', $data);

            return $this->db->insert_id();
        }
        return false;
    }

另外,我正在尝试插入多个表的数据。任何帮助都将不胜感激。谢谢!提交按钮也可以放在模态页脚?或者应该放在保留位置。php?

ldioqlga

ldioqlga1#

通过这些函数传递数据数组。

  1. $this->UI_model->reserve_details($details);
  2. $this->UI_model->reserve_add($details2);
    但在模型中,你不会捕捉到这些。
  3. public function reserve_details(){
  4. public function reserve_add(){
    应该是这样
  5. public function reserve_details($data){
  6. public function reserve_add($data){
    由于您正在检查if (!empty($data)) {,因此它始终返回false,除非Undefixed Variable错误。

相关问题