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

How to echo result of submit within same form in codeigniter

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

这是我的控制器

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;
}

我需要一条成功消息出现在提交表单中,以便继续在同一页面中。

我还有另一个问题需要 solve.When 我从我的视图提交数据 我收到 "success" 但上面有错误

A PHP Error was encountered Severity: Notice Message: Undefined variable: data Filename: controllers/Timetable.php Line Number: 26 Backtrace: File: /home/n17ljw1lcuti/public_html/ticketing/core/admin/application/controllers/Timetable.php Line: 26 Function: _error_handler File: /home/n17ljw1lcuti/public_html/ticketing/core/admin/index.php Line: 315 Function: require_once In line number 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>

应该是这样的:

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);
}