Add delete record using AJAX and PHP

This tutorials about AJAX without refreshing the page is displaying. What we do first making database table, connecting the database. Final output looks like below image.

Final Output

add delete record using ajax and php

Here is code to make copy and paste inside the phpmyadmin in SQL tab.

Database

CREATE TABLE IF NOT EXISTS add_delete_record ( id int(11) NOT NULL AUTO_INCREMENT, content text NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Now let’s create the config file for connecting the database table.

config.php

Now we are going to create the index.php file to get the data from the database and displaying according to our needs. jQuery provide the great animation effects like fade in and fade out we are use in this. Logic is simple we are implement this

  • Call the ajax events when click on add button
  • Call the ajax events when click on remove button

This two types of things we are going to implements in this file. Check below code:

index.php

Now create the response.php file that used to fill the data on specific id in html that call when add and delete button is press that all things is happens without refreshing the page. If you wants to view this file click on network inside the browser on firebug.

response.php

‘; echo $contentToSave.’

‘; mysql_close($connecDB); //close db connection }else{ //header(‘HTTP/1.1 500 ‘.mysql_error()); //display sql errors.. must not output sql errors in live mode. header(‘HTTP/1.1 500 Looks like mysql error, could not insert record!’); exit(); } } elseif(isset($_POST[“recordToDelete”])&& strlen($_POST[“recordToDelete”])>0 && is_numeric($_POST[“recordToDelete”])) { //do we have a delete request? $_POST[“recordToDelete”] //sanitize post value, PHP filter FILTER_SANITIZE_NUMBER_INT removes all characters except digits, plus and minus sign. $idToDelete = filter_var($_POST[“recordToDelete”],FILTER_SANITIZE_NUMBER_INT); //try deleting record using the record ID we received from POST if(!mysql_query(“DELETE FROM add_delete_record WHERE id=”.$idToDelete)) { //If mysql delete query was unsuccessful, output error header(‘HTTP/1.1 500 Could not delete record!’); exit(); } mysql_close($connecDB); //close db connection } else { //Output error header(‘HTTP/1.1 500 Error occurred, Could not process request!’); exit(); } ?>