Kimpoy
Kimpoy

Reputation: 73

Insert data from modal, Codeigniter

I'm trying to insert the input data from a modal but I don't know how to achieve this. I'm not really familiar with AJAX(I apologize for that). I did some readings and research but I'm getting confused. Here is my modal content 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="[email protected]" 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>

Here's my modal in 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>

Here' my Controller(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);
      }
    }
  }

Here's the Model

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;
    }

Also, I'm trying to insert the data for multiple tables. Any help will be much appreciated. Thanks! Is is also fine to place the submit button at modal footer? or it should be at reserve.php?

Upvotes: 1

Views: 263

Answers (1)

Abdulla Nilam
Abdulla Nilam

Reputation: 38652

You're passing data array through these functions.

  1. $this->UI_model->reserve_details($details);
  2. $this->UI_model->reserve_add($details2);

But in the model, you're not catching those.

  1. public function reserve_details(){
  2. public function reserve_add(){

So it should be

  1. public function reserve_details($data){
  2. public function reserve_add($data){

Since you're checking if (!empty($data)) { its always retuns false, unless Undefixed Varibale error.

Upvotes: 1

Related Questions