我有这个删除并在CodeIgniter中添加管理员帐户功能。但我希望它能够共同发挥作用。有什么办法可以解决这个问题吗?删除和保存工作正确分开。
控制器:
public function index()
{
$this->load->library('form_validation');
// model aanroepen
$this->load->model('Admin_model', '', TRUE);
// see if there is post
if ($this->input->server('REQUEST_METHOD') == 'POST') {
$username = $this->input->post("username");
$password = $this->input->post("password");
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() != FALSE) {
$new_admin = new Admin_model();
$new_admin->Admin_name = $username;
$new_admin->Admin_password = $password;
$this->Admin_model->save($new_admin);
}
}
if ($this->input->server('REQUEST_METHOD') == 'POST') {
$userid = $this->input->post("id_admin");
$this->Admin_model->delete($userid);
}
$tweets = $this->Admin_model->get_all();
$viewdata = [
"tweets" => $tweets
];
$this->load->view('admin/admin', $viewdata);
}
}
查看:
<?php echo validation_errors() ?>
<h1>Create new admin</h1>
<form action="" method="post">
<label for="username">Username</label>
<input type="text" id="username" name="username"/>
<br/>
<label for="password">Password</label>
<input type="password" id="password" name="password"/>
<button type="submit" id="admin_submit">Submit</button>
</form>
<h1>Delete admin</h1>
<?php foreach($tweets as $t): ?>
<?php echo "<p>" . $t['admin_name']."</p> "; ?>
<form action="" method="post">
<input type="text" name="id_admin" value="<?php echo $t['admin_id'] ?>" />
<button type="submit" name="delete">Delete</button>
</form>
<?php endforeach; ?>
答案 0 :(得分:1)
是的,你可以在视图中的保存和删除按钮上添加名称标签,如
<button type="submit" name="save" id="admin_submit">Submit</button>
现在将此转到控制器,如
if(isset($_POST['save']))
{
//here your code of save only its work for when you click save button
}
与删除相同,并在同一功能上正常工作
答案 1 :(得分:1)
如果你想要两者一起工作那么请在两种形式中放入一些隐藏值并获得该值并根据该隐藏值进行验证或按下按钮按下该按钮..通过isset function喜欢
if(isset($_POST['save'])) // or check hidden field value here
{
/* here put save button validation Or validation according for hidden value */
}else if(isset($_POST['delete']))
{
/* Delete validation OR other validation */
}
在您的控制器中
答案 2 :(得分:0)
您的两个方法检查都是发布的,因此当您发布删除表单时,它实际上会进入您的第一个方法。
执行此检查,检查您是否单击了删除按钮:
$is_delete = $this->input->post('delete');
并改变:
if ($this->input->server('REQUEST_METHOD') == 'POST')
为:
if (isset($is_delete))
对保存操作执行相同的操作:
<button type="submit" name="save_admin" id="admin_submit">Submit</button>
执行此检查,检查您是否单击了删除按钮:
$is_save = $this->input->post('save_admin');
并改变:
if ($this->input->server('REQUEST_METHOD') == 'POST')
为:
if (isset($is_save))
这样,每次收到POST请求时都不会调用该方法。