我需要在输入表单的同一页面内回显提交结果
这是我的控制器
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/ticking/core/admin/application/controllers/Timetable.php线路:26函数:_error_handler文件:/home/n17ljw1lcuti/public_html/ticking/core/admin/index.php线路:315函数:require_one第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);
}