0

these are my code for form validation :

     <form action="<?php echo base_url().'data_laporan/tambah_aksi';?>"method="post" enctype="multipart/form-data">
  

        <div class="form-group">
            <label>Jenis Laporan</label>
            <select class="form-control" name="jenis_laporan">
            <option>Penemuan hewan</option>
            <option>Kehilangan Hewan</option>
            </select>
       
        </div>

        <div class="form-group">
            <label>Jenis Hewan</label>
            <input type="text" name="jenis_hewan" class="form-control">
        </div>

        <div class="form-group">
            <label>Lokasi</label>
            <input type="text" name="lokasi" class="form-control">
        </div>

        <div class="form-group">
            <label>Deskripsi</label>
            <input type="text" name="deskripsi" class="form-control">
        </div>

        <div class="form-group">
            <label>Nama Pelapor</label>
            <input type="text" name="nama_pelapor" class="form-control">
        </div>

        <div class="form-group">
            <label>No Hp</label>
            <input type="text" name="no_hp" class="form-control">
        </div>

        <div class="form-group">
            <label>Gambar Hewan</label>
            <input type="file" name="gambar" class="form-control">
        </div>
  

  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
    <button type="submit" class="btn btn-primary">Save changes</button>
  </div>
  </form>

i want to show a simple warning when i click "Save Changes" as a submit button like this :

image for example warning

and these are my code for tambah_aksi() function :

public function tambah_aksi()
{
    $jenis_laporan = $this->input->post('jenis_laporan');
    $jenis_hewan = $this->input->post('jenis_hewan');
    $deskripsi = $this->input->post('deskripsi');
    $lokasi = $this->input->post('lokasi');
    $nama_pelapor = $this->input->post('nama_pelapor'); 
    $no_hp = $this->input->post('no_hp');
    $gambar = $_FILES['gambar']['name'];
    if ($gambar = '') {} else{
        $config ['upload_path'] = './upload';
        $config ['allowed_types'] = 'jpg|jpeg|png|gif';

        $this->load->library('upload', $config);
        if(!$this->upload->do_upload('gambar')){
            echo "Gambar gagal di Upload";
        }else{
            $gambar=$this->upload->data('file_name');
        }
    }
    $data = array (
        'jenis_laporan' => $jenis_laporan,
        'jenis_hewan' => $jenis_hewan,
        'deskripsi' => $deskripsi,
        'lokasi' => $lokasi,
        'nama_pelapor' => $nama_pelapor,
        'no_hp' => $no_hp,
        'gambar' => $gambar,
    );

    $this->model_laporan->tambah_aksi($data, 'tb_laporan');
    redirect('data_laporan/index');
}

i tried using if-else form validation run checking but it does not goes well, could you please help me to fix this with another way or the efficient way of if-else checking?

  • You have error in this part if ($gambar = ''). You are mising one =, matching yould be done with double == – Ivan Jul 12 '21 at 15:53

2 Answers2

0

You can do this many ways Via codeigniter 3.x then first load form validation library in controller function and set rules and place holder for showing errors in view file. See for reference https://codeigniter.com/userguide3/libraries/form_validation.html#the-controller https://codeigniter.com/userguide3/libraries/form_validation.html#showing-errors-individually

Via HTML You can simply do this by adding required attribute to all html elements which are required, browser will show warning automatically after clicking on submit button for required fields. see example for input html element

<input type="text" name="nama_pelapor" required class="form-control">

See for dropdown

<select class="form-control" required name="jenis_laporan">
        <option>Penemuan hewan</option>
        <option>Kehilangan Hewan</option>
        </select>

Via Jquery If you want to customize error messages then you do this via your own code in jquery or jquery validator library. See this documentation for jquery validation https://jqueryvalidation.org/validate/

Virender Kumar
  • 182
  • 1
  • 8
0

Comparison operator should be double == not single =. Edit your line of code to this.

 if ($gambar =='') {} else{
Lenin
  • 303
  • 2
  • 3