简体   繁体   English

提交后如何保持文字输入

[英]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 如果要在提交表单后保留输入值,而不是使用set_value表单帮助器方法,它将像这样

<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 http://www.codeigniter.com/user_guide/helpers/form_helper.html

You can use flashdata to store value for one redirect. 您可以使用flashdata存储一次重定向的值。 But to use flashdata you have to use redirect() after creating it. 但是要使用flashdata,您必须在创建FlashData后使用redirect()

$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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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