Deleting records through PHP without the page being reloaded (using AJAX)

delete row from table using ajax in php
delete record without a page refresh with ajax in php
insert delete update data using ajax in php
laravel ajax delete
ajax delete in php
delete using ajax in codeigniter
ajax delete request
how to delete record in php using javascript

I am trying to delete some records from a database table through PHP without the page being reloaded (using AJAX). First I am printing all the information in a table and then the user can click a delete button, which will delete the record. However, I am having troubles with passing the record id to the AJAX method.

ListProducts.php

create connection
...
$result = mysqli_query($conn, $sql);

echo "<table border = '1' style = 'border-collapse:collapse;'>";
echo "<tr><th>UserID</th><th>Username</th><th>Action</th></tr>";

if (mysqli_num_rows($result)>0){
    while ($row = mysqli_fetch_assoc($result)){
        echo "<tr><td>".$row['id']."</td><td>".$row['username']."</td><td><input type = 'submit' onclick = 'delete_record()' value = 'Delete' id = '".$row['id']."'></td></tr>";
    }
}

Then, within the same page, there is the AJAX function. I have given each input field the corresponding id and I am trying to pick it with jQuery

function delete_rekord(){
    var id = "Default";
    $(document).ready(function(){
        $("input").click(function(){
            id = $(this).attr('id');
        });
    });
    alert(id); //Test; will print DEFAULT
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function (){
        if (this.readyState == 4 && this.status == 200){
            alert("Deleted user with ID: " + this.responseText);
        }
    }
    xhttp.open("GET", "delete.php?id="+id, true);
    xhttp.send();
}

Over at delete.php I just delete the record and print the id of the deleted record which I collect in this function as a responseText and print out a message. I have put a test alert(id) and it seems that the value of var id is staying default, just as I initialize it. So the mistake must be in the way I am retrieving the id through jQuery. Any ideas how to resolve this? Thanks


pass the $row['id'] into your javascript function like below. In your html change the code as below

<input type = 'submit' onclick = 'delete_record(".$row['id'].")' value = 'Delete' id = '".$row['id']."'>

In your javascript, change the function like this

function delete_rekord(rowid){

   var id = rowid;

Hope this helps you

AJAX CRUD, AJAX CRUD - Laravel: Delete Data without Page Reload using AJAX jQuery with Bootstrap Duration: 12:17 Posted: Dec 30, 2018 How to Delete Record/Data from Database without Page Refresh/Reload (Using AJAX) Live Table Add Edit Delete using Ajax Jquery in PHP 20:40. PHP - Delete Records From MYSQL Database Using


You should pas the id variable into the delete function

echo "<tr><td>".$row['id']."</td><td>".$row['username']."</td><td><input type='submit' onclick='delete_record(".$row['id']."); return false;' value = 'Delete' id = '".$row['id']."'></td></tr>";`

in your js

function delete_record(id){ // also rename it to delete_record your fn name was delete_rekord
  alert(id); //Test; will print the correct ID
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function (){
    if (this.readyState == 4 && this.status == 200){
        alert("Deleted user with ID: " + this.responseText);
    }
  }
  xhttp.open("GET", "delete.php?id="+id, true);
  xhttp.send();
}

How to Delete Record from MySQL Table with AJAX, If you want to delete records from MySQL database table without refreshing the whole page then you can use jQuery AJAX for this. edit or delete the record and update layout without reloading the page. With only PHP you can easily delete records but require to submit the page or send value by URL and  Without having to reload the page, the Ajax technique is a very neat way to add or delete / database records, also it looks very cool. In this tutorial, I have created three files index.php, response.php and config.php. Front page (index.php) is the only file where user directly interacts with your application.


Pass the value of Id as function parameter

<input type ='submit' onclick = 'delete_record(".$row['id'].")' value = 'Delete' id = '".$row['id']."'>

function delete_record(id){
    alert(id);
}

Prevent page from Submit on Refresh in PHP, In this case, if the user refreshes the page then the data is reinserted on the to another page; Check entry in MySQL Database Table; With AJAX; Conclusion Check the submit value in this field if it's not found then insert the record. You can use AJAX to save data and you can either reload the page or  Steps to Creating Submit Form Without Page Refresh Using Ajax, jQuery and PHP. To create a user friendly contact form we will need to follow the below steps: Create a HTML Form Page. Include Bootstrap Library. Include jQuery Library. jQuery Form Validation & AJAX Method. Create an AJAX Action Page.


jQuery, Add / Delete elements without page refresh, Chat with fellow EECMS users in the 'jQuery, Add / Delete elements without page this now for days, and I'm getting nowhere, so please bear with me… I need to delete an input field from a form without a page refresh AND ajax but i wonder how many back-up the ajax functionality with php only code. In this tutorial you can learn how to upload image without page refresh in php by using ajax with jquery and I have also show you how to remove that uploaded image from server using php without


How to make the delete button functional in PHP without reloading , The basics of an AJAX call (a call that doesn't reload the page) follows. You need a button (or page with AJAX · Delete record without refresh page using php Please click add new button to add new product, click edit button to update record, and click delete button to delete record. Congratulations! You did it. Now, you can create a crud application without reloading the page with codeigniter and ajax.


AJAX Introduction, AJAX is about updating parts of a web page, without reloading the whole page. AJAX allows web pages to be updated asynchronously by exchanging small  using this tutorial you can easily understand that how you can table add edit delete using ajax jquery in php mysql without refreshing the page,insert delete update data using ajax in php,insert