Scenario:
I have used AJAX to delete the record and PHP as the programming language. The AJAX part is given below:
Here $('#id')
is the id
of the button which contains the value of user's id from the database dynamically with the help of this part: id='.$d['ID'].'
implemented in the front end.
<script>
function deleteRecord() {
var var1 = $('#id').html();
var result = confirm("Do you want to delete this Record?");
if(result){
//call ajax
$.ajax({
url: '<?php echo base_url();?>index.php/Device/delete',
type: 'POST',
data: {
id: var1
},
success: function(result){
$("#id").html(result);
alert('You have successfully deleted the user.');
},
error(e){
alert('Problem while interacting to the server.');
}
});
}
else{
console.log('Operation Cancelled.');
}
}
</script>
This is the front end part where Button is used to delete the records. I have used id='.$d['ID'].'
to get the value of the id dynamically from the database.
<td> <button class="btn" type="submit" onclick="deleteRecord()" id='.$d['ID'].'>Delete
</button> </td>
This is the url as mentioned in the AJAX:
url: '<?php echo base_url();?>index.php/Device/delete'
In the above URL, Device is the name of the Controller and delete is the method of the controller which is given below:
public function delete($id = '') {
$login = $_SESSION['username'];
if (! in_array($login, $this->main_model->get_authorized_users())) {
$redirect = site_url('device');
redirect($redirect, 'refresh');
}
if ($this->input->post('submit')) {
$username = $this->input->post('username');
$deviceid = $this->input->post('deviceid');
if ($deviceid == null || $username == null || $login == null) {
$redirect = site_url('device');
redirect($redirect, 'refresh');
return;
}
} else {
if ($id == '') {
$redirect = site_url('device');
redirect($redirect, 'refresh');
}
$res = $this->device_model->delete();
$data['action'] = 'delete';
$data['id'] = $id;
$data['result'] = $this->device_model->get_device($id);
$data['body'] = $this->load->view('register_view', $data, true);
$this->load->view('template', $data);
}
}
Here is the Model part:
function delete(){
$username = $this->input->get('username');
$deviceid = $this->input->get('deviceid');
$id = $this->input->get('id');
$language= $this->input->get('language');
$deleted_by=$_SESSION['username'];
$sql = "DELETE FROM devices
WHERE id = $id";
$query = $this->db->query ( $sql );
//log update
$this->log_device_delete($id,$username,$deviceid,$deleted_by);
}
Current Output:
Issue Faced:
When user presses the OK button then the message called "You have successfully deleted the user." is displayed but the record is not deleted.
Required Solution:
When the user presses the OK button on the Confirmation Box then the user record should be deleted.
What correction do I need here?
Suggestions are highly appreciated.
You are using $d['ID']
in id
attribute. But it need to be placed as value in some field.
The code that you need to update is:
<td>
<button class="btn" type="submit" onclick="deleteRecord(this)" data-id="<?php echo $d['ID']; ?>">Delete</button>
</td>
And update in JS as:
function deleteRecord(e) {
var var1 = $(e).data("id");
Try data attribute as
<td> <button class="btn" type="submit" onclick="deleteRecord()" data-id=".$d['ID'].">Delete
</button> </td>
And in deleteRecord function get that value as
var var1 = $(this).attr("data-id");
Are you sure in the SQL Query you put the right ID?
You said:
When user presses the OK button then the message called "You have successfully deleted the user." is displayed but the record is not deleted.
You have no errors and AJAX calls the success function. All force me to think about ID problem.