如何在Codeigniter中以相同的形式回显提交结果

时间:2018-08-01 09:14:40

标签: php codeigniter codeigniter-3 codeigniter-query-builder

我需要在输入表单的同一页面内回显提交的结果

这是我的控制者

public function message(){
    $this->form_validation->set_rules('message','Message', 'required');
    $this->form_validation->set_rules('friend_id[]','Recipients', 'required');
    if($this->form_validation->run())
    {
        $friend_ids = $this->input->post("friend_id[]");
        $message = $this->input->post("message");
        unset($data['submit']);
        $this->load->model('Queries');
        $insert = $this->Queries->saveMessage($friend_ids, $message);
        if($insert)
        {
            echo "Success";
        }
        else
        {
            echo "error";
        }
    }
    else
    { 
        echo validation_errors();
    }
}

这是我的模特

public function saveMessage($friend_ids , $message){
    foreach($friend_ids as $friend_id)
    {
        $record = array('friend_id' => $friend_id,'message' => $message);
        $this->db->insert('tbl_messages', $record);
    }
    return true;
}

我需要一条成功消息才能出现在提交形式中,以便继续位于同一页面中。

我还需要解决另一个问题。当我从视图提交数据时,我收到了“成功”,但上面有错误提示

  

遇到PHP错误       严重程度:注意       消息:未定义的变量:数据       文件名:controllers / Timetable.php       行号:26       回溯:       文件:/home/n17ljw1lcuti/public_html/ticketing/core/admin/application/controllers/Timetable.php       行:26       函数:_error_handler       文件:/home/n17ljw1lcuti/public_html/ticketing/core/admin/index.php       线:315       函数:require_once       在第26行

我的控制器中有unset($data['submit']);的代码

这是我的观点

<body>
    <div style="margin-bottom:2% !important;">
        <?php echo form_open('Timetable/message'); ?>
        <fieldset>
            <br/>
            <div>
                <div>
                    <div>
                        <label>Enter Message</label>
                        <textarea id="mytextbox" name="message" placeholder="Enter Message"></textarea>
                    </div>
                    <div>
                        <?php echo form_submit(['name'=>'submit', 'value'=>'Send', 'class'=>'login']); ?>
                    </div>
                </div>
                <div>
                    <?php echo form_error('message', '<div class="text-danger">', '</div>'); ?>
                </div>
            </div>
            <div>
                <?php echo form_error('friend_id', '<div class="text-danger">', '</div>'); ?>
                <table>
                    <thead>
                        <tr>
                            <th>
                                <input type="button" id="toggle" value="select" onClick="do_this()" />
                            </th>
                            <th>Friends Name</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if(count ($friends)):?>
                        <?php foreach ($friends as $friend):?>
                        <tr>
                            <td>
                                <input type="checkbox" name="friend_id[]" value=<?php echo $friend->id;?>></td>
                            <td>
                                <?php echo $friend->friend_name;?>
                            </td>
                        </tr>
                        <?php endforeach;?>
                        <?php else:?>
                        <td>No Records Founds!</td>
                        <?php endif;?>
                    </tbody>
                </table>
            </div>
        </fieldset>
        <?php echo form_close(); ?>
    </div>
</body>

1 个答案:

答案 0 :(得分:1)

应该是这样的:

public function message()
{
    if ($this->input->post('submit') == 'Send')
    {
        $this->form_validation->set_rules('message','Message', 'required');
        $this->form_validation->set_rules('friend_id[]','Recipients', 'required');
        if($this->form_validation->run())
        {
            $friend_ids = $this->input->post("friend_id");
            $message = $this->input->post("message");
            $this->load->model('Queries');
            $insert = $this->Queries->saveMessage($friend_ids, $message);
            if($insert)
            {
                echo "Success";
            }
            else
            {
                echo "error";
            }
        }
        else
        { 
         echo validation_errors();
        }
    }
// here goes your views
$data['friends'] = 'record-from-db';
$this->load->view('this-is-your-view', $data);
}