Home > Enterprise >  Insert data from modal, Codeigniter
Insert data from modal, Codeigniter

Time:06-10

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 >
            <h3>Reservation Details</h3>
            <div >
                <input type="hidden" name="package_id" value="<?php  ?>">
                <div >
                    <label for="date_start" >Reserve Date:</label>
                    <input type="date" name="date_start" id="date_start"  min="<?php echo date("Y-m-d") ?>" value="<?php echo date("Y-m-d") ?>" required>
                </div>
                <div id="msg" ></div>
                <div id="check-availability-loader" >
                    <center>
                        <div >
                            <strong>Checking Availability...</strong>
                            <div  role="status" aria-hidden="true"></div>
                        </div>
                    </center>
                </div>
                <div >
                    <label for="quantity" >Number of Person <span><i>(Max. of 15 person)</i></span></label>
                    <input type="number"  required name="person" value="">
                </div>
                <div >
                    <label for="amount" >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 >
                    <label for="amount" >Total Amount</label>
                    <input type="number" name="cost" id="amount"  value="<?php echo isset($cost) ? number_format($cost, 2) : 0.00 ?>" required readonly>
                </div>

            </div>
            <div >
                <div >
                    <label for="name" >First Name</label>
                    <input type="text"  name="firstname" placeholder="Enter First Name" value="">
                </div>
                <div >
                    <label for="name" >Last Name</label>
                    <input type="text"  name="lastname" placeholder="Enter Last Name" value="">
                </div>
                <div >
                    <label for="name" >Contact</label>
                    <input type="text"  name="contact" placeholder="09xx-xxx-xxxx" value="">
                </div>
                <div >
                    <label for="name" >Email</label>
                    <input type="text"  name="email" placeholder="[email protected]" value="">
                </div>
            </div>
        </div>
        <div >
            <button  type="submit" name="details">Save</button>
            <button ><span >Close</span></button>
        </div>
    </form>

Here's my modal in view.php

<!-- Modal content -->

<div >
    <div >
        <h3><?php echo $packages->title; ?></h3>
    </div>
    <div >
        <?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?

CodePudding user response:

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.

  • Related