简体   繁体   中英

How to keep text input after submit

I have created a web search on me. but I want when pressed the submit button, input text in the text box is not lost when the results come out. This is my code : controller :

public function index(){
    $data = $this->mymodel->GetArtikel();
    $this->load->view('tabel',array('data' => $data));
    }

models :

public function GetArtikel(){
    $this->db->select('a.id_artikel, a.judul, a.tanggal_buat, a.tanggal_update, b.nama_kategori, c.nama_lengkap, c.id_user');
    if(!empty($_POST['cari_judul'])){
        $this->db->like('a.judul',$_POST['cari_judul']);
    }
    if(!empty($_POST['cari_penulis'])){
        $this->db->like('c.nama_lengkap',$_POST['cari_penulis']);
    }
    if(!empty($_POST['cari_kategori'])){
        $this->db->where('b.nama_kategori',$_POST['cari_kategori']);
    }
    $this->db->from('artikel as a');
    $this->db->join('kategori as b', 'a.id_kategori=b.id_kategori','LEFT');
    $this->db->join('user as c','a.id_user=c.id_user','LEFT');
    $data=$this->db->get();

    return $data->result_array();
}

view :

<form method="POST" action="<?php echo base_url()."index.php/crud/"; ?>">
    <table widht="200" border="1">
        <tr>
            <td>Judul</td>
            <td><input type="text" name="cari_judul" value="<?php echo $_POST['cari_judul']; ?>"></td>
        </tr>
        <tr>
            <td>Penulis</td>
            <td><input type="text" name="cari_penulis"></td>
        </tr>
        <tr>
            <td>Kategori</td>
            <td><input type="radio" name="cari_kategori" value="Fiksi" >Fiksi
           <input type="radio" name="cari_kategori" value="Non Fiksi" >Non Fiksi</td>
        </tr>
        <td colspan="2"><input type="submit" value="Cari"></td>
        </tr>
    </table>

If you are trying to repopulate the posted values -

You can either set session in your controller -

$this->load->library('session');
if ($this->input->post('name')) {
    $this->session->set_userdata('name' => $this->input->post('name'));
}

Or

set_value()

if you want to keep input value after submit form than use set_value form helper method it will be like this

<input type="text" name="cari_judul" value="<?php echo set_value('cari_judul') ?>">

to read more about form helper check the documentation

http://www.codeigniter.com/user_guide/helpers/form_helper.html

You can use flashdata to store value for one redirect. But to use flashdata you have to use redirect() after creating it.

$this->session->set_flashdata('name' => $this->input->post('name'));
redirect("controller/method");

Hope this helps.

You can change you code like this

public function index(){
    $data = $this->mymodel->GetArtikel();
    $this->load->view('tabel',array(
        'data' => $data,
        'judul' => $_POST['cari_judul'],
        'penulis' => $_POST['cari_penulis'],
        'kategori' => $_POST['cari_kategori']
    ));
}

Send back every data that you've entered

and in your view

<td><input type="text" name="judul" value="<?php echo $cari_judul; ?>"></td>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM