0

I need to echo result of submit within same page of input form

This is My controller

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

This is My model

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

I need a success message to appear within a form of submit so as to continue being in the same page.

Also i have another problem i need to solve.When i submit data from my view i receive a "success" but with error above it says

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

there in a code of unset($data['submit']); in my controller

Here is the my view

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

1 回答 1

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);
}
于 2018-08-01T10:47:33.493 回答