TransWikia.com

Submit button for multiple choice without using radio button

Stack Overflow Asked by Rizki Prastyo on December 22, 2021

I had a problem when I want to auto submit my choices using codeigniter. My return values into database are ID (integer), status (enum (‘masuk’,’istirahat’,’kembali’,’pulang’)), waktu (datetime). I want variable ‘waktu’ is my current datetime if user select one from ‘status’ button.

Here is my code in views folder:

<html>
  <head>
    <title>Form Tambah Presensi</title>
  </head>
  <body>
    <h1>Form Tambah Data Presensi</h1>
    <hr>
    <!-- Menampilkan Error jika validasi tidak valid -->
    <div style="color: red;"><?php echo validation_errors(); ?></div>
    <?php echo form_open("presensi/tambah"); ?>
        <tr>
          <td>ID</td>
          <td><input type="number" name="input_id" value="<?php echo set_value('input_id'); ?>"></td>
        </tr><br><br>
        <tr>
          <td>Status:</td><br>
          <input type="submit" id="input_status" name="input_status" value="masuk">
          <label for="masuk"></label><br>
        </tr>
        <tr>
          <input type="submit" id="input_status" name="input_status" value="istirahat">
          <label for="istirahat"></label><br>
        </tr>
        <tr>
          <input type="submit" id="input_status" name="input_status" value="kembali">
          <label for="kembali"></label><br>
        </tr>
        <tr>
          <input type="submit" id="input_status" name="input_status" value="pulang">
          <label for="pulang"></label><br><br>
        </tr>
    <?php echo form_close(); ?>
  </body>
</html>

My code in models folder:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class PresensiModel extends CI_Model {
  
  // Fungsi untuk validasi form tambah dan ubah
  public function validation($mode){
    $this->load->library('form_validation'); // Load library form_validation untuk proses validasinya
    
    // Tambahkan if apakah $mode save atau update
    if($mode == "save")
      $this->form_validation->set_rules('input_id', 'id', 'required|numeric|max_length[11]');
    
    $this->form_validation->set_rules('input_status', 'status');
    $this->form_validation->set_rules('input_waktu', 'waktu', 'required');
      
    if($this->form_validation->run()) // Jika validasi benar
      return TRUE; // Maka kembalikan hasilnya dengan TRUE
    else // Jika ada data yang tidak sesuai validasi
      return FALSE; // Maka kembalikan hasilnya dengan FALSE
  }
  
  // Fungsi untuk melakukan simpan data ke tabel presensi
  public function save(){
    $data = array(
      "id" => $this->input->post('input_id'),
      "status" => $this->input->post('input_status'),
      "waktu" => $this->input->post('input_waktu')
    );
    
    $this->db->insert('presensi', $data); // Untuk mengeksekusi perintah insert data
  }
}

My code in controllers folder:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Presensi extends CI_Controller {
  
  public function __construct(){
    parent::__construct();
    
    $this->load->model('PresensiModel'); // Load PresensiModel ke controller ini
  }
  
  public function index(){
    $data['presensi'] = $this->PresensiModel->view();
    $this->load->view('presensi/form_tambah', $data);
  }
  
  public function tambah(){
    if($this->input->post('submit')){ // Jika user mengklik tombol submit yang ada di form
      if($this->PresensiModel->validation("save")){ // Jika validasi sukses atau hasil validasi adalah TRUE
        $this->PresensiModel->save(); // Panggil fungsi save() yang ada di SiswaModel.php
        redirect('presensi');
      }
    }
    
    $this->load->view('presensi/form_tambah');
  }
}

One Answer

It's simple you need to change at 2 places validation() and save()

In validation() function we want to make it optional if user selected any status because we will generate waktu on server, so change

$this->form_validation->set_rules('input_waktu', 'waktu', 'required');

To

if(empty( $this->input->post('input_status') ) )
$this->form_validation->set_rules('input_waktu', 'waktu', 'required');

Meaning if user haven't selected any status then waktu is mandatory otherwise (by default) it will be optional


Then in save() function, do a check like this

$data = array(
  "id" => $this->input->post('input_id'),
  "status" => $this->input->post('input_status'),
  "waktu" => empty($this->input->post('input_status'))? $this->input->post('input_waktu'): date('Y-m-d H:i:s')
);

It means use use the value of user provided waktu if status is not selected but if user has selected status then use the server current datetime

Answered by Vinay on December 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP