I have working codeigniter controller and view which sucessfully delete record. I would like to do it with ajax – jquery, to avoid reloading the page, and sure enough I will need to prompt the user if he is sure about deleting the record.
Any code snippet would be deeply appreciated, since i don’t know where to start:
Here is the code from the controller:
public function deleteUser()
{
$this->load->model('backOfficeUsersModel');
$deleteWhat = array(
'dpage' => $this->input->post('dpage'),
'rid' => $this->input->post('rid')
);
$this->backOfficeUsersModel->delete($deleteWhat['rid']);
redirect($deleteWhat['dpage']);
} // end of function deleteUser
Here is the view that I have now: (just the php code is presented for the sake of simplicity)
foreach ($users as $key => $user) {
echo form_open('backOfficeUsers/deleteUser');
echo form_hidden('dpage', 'backOfficeUsers/displayAllUsers');
echo form_hidden('rid', $user['userid']);
echo "<tr>";
echo "<td>";
$data = array(
'name' => 'row_sel',
'id' => $user['userid'],
'value' => $user['userid'],
'class' => 'select_row'
);
echo form_checkbox($data);
//echo "<input type='checkbox' class='select_row' name='row_sel'>";
echo "</td>";
echo "<td>" . $user['userid'] . "</td>";
echo "<td>" . $user['firstname'] . " " . $user['lastname'] . "</td> ";
echo "<td>" . $user['username'] . "</td> ";
echo "<td><a href=mailto:" . $user['email'] . ">" . $user['email'] . "</td> ";
echo "<td style='text-align: center;'>";
echo anchor("backOfficeUsers/displayEditUserForm/$user[userid]/", "<i class=splashy-pencil></i>", "class=ext_disabled");
echo "</td>";
echo"<td style='text-align: center;'>";
$confirm = "class='splashy-remove' style='border:none; width:16px; height:16px;'";
echo form_submit('submit', '', $confirm);
echo"</td></tr>";
echo form_close();
}
Any help (code snippet, or link to some example) will be deeply appreciated
Regards,Zoran
Please, go easy on the
echostatements. If you find yourself using them excessively it probably because you should be writing HTML with embedded PHP and not PHP with embedded HTML. Also, when posting questions on SO, remove the extra fluff that only serves to confuse answerers. There is a lot of crap in your code that does not get to the heart of your problem.From scratch, here’s the HTML scaffolding I’d use:
Then the jQuery:
And the controller:
I used Datamapper’s syntax here for record deletion, please adapt to Active Record. The code is not tested, it was written here in the SO editor – but it is how I would go about things.