Browse By

What is AJAX and its use with database in PHP?

Spread the love

What is AJAX?

AJAX = Asynchronous JavaScript and XML.

AJAX is a technique to load data without refreshing page, it will refresh div only to give as Asynchronous Result.

//this is index.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<div id="ajaxdiv"></div>

<script> 
    $.ajax({
      url : 'https://www.manojkarn.com/ajax.php',
      type : 'POST',
      data : 'str=' + arg,
      dataType : 'html',
      success : function(htmldata){
           $('#ajaxdiv').html(htmldata); //refresh div of id ajaxdiv.
       }
    });
</script>

in the above code you will find the method, we can use GET or Method depends upon requirement.

The above code will give you value from ajax.php depending upon the data we have sent in the string name str.

<?php
//ajax.php
$serverName = "localhost";  //this remain as it is for the host.
$userName = "root";  // default name for local server
$password = "";   // Blank for local server
$dbName = "Database Name";
$conn = new mysqli($serverName, $userName, $password, $dbName);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
//This is All about select query which is capable for extracting all information from table.
$query = "delete from table_name where id='".$_POST['str']."';
$conn->query($sql);
echo "Recode has been deleted successfully!";
?>
//the result will show the message on the div "Recode has been deleted successfully!" upon execution of ajax.

Leave a Reply

Your email address will not be published. Required fields are marked *