AJAX = Asynchronous JavaScript and XML. AJAX is a technique for creating fast and dynamic web pages. AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.

Ajax with Angular and PHP | PHPenthusiast

You can use Ajax to POST, GET and ect. But POST and GET is basic use of AJAX. POST is to send data request into database system Like Save and Update Request while Get is Send data request to Get or select data from Database.

syntax:

             $.ajax({
                    type:"POST",   // in the line "POST" you can use either POST ot GET
                    url: "function.php",   // this is the php file api or function
                    data: {
                        data : "Jave", // this is the data if you need data to request
                    },
                    success:function(res) // this part ; the return of the API will be store to "res" either success or not 
                    {
                                                
                    },
                    error: function(res) // in this part catch of the ajax syntax or api error. return to res
                    {
                        
                    }
                });

next is example of POST request in PHP and AJAX;

PHP file : function.php

<?php

if (isset($_POST["data"]))
{
	echo "POST Success";
}

?>

Ajax file: function.js

function buttonClickPOST()
{
	            $.ajax({
                    type:"POST",   
                    url: "function.php",  
                    data: {
                        data : "Jave",
                    },
                    success:function(res) 
                    {
                         alert(res);         	              
                    },
                    error: function(res)
                    {
                         alert(res);    
                    }
                });
}

next is example of GET request in PHP and AJAX;

PHP file : function.php

<?php

if (isset($_GET["data"]))
{
	echo "GET Success";
}

?>

Ajax file: function.js

function buttonClickGET()
{
	            $.ajax({
                    type:"GET",   
                    url: "function.php",  
                    data: {
                        data : "Jave",
                    },
                    success:function(res) 
                    {
                         alert(res);         	              
                    },
                    error: function(res)
                    {
                         alert(res);    
                    }
                });
}

file of Index.php

<!DOCTYPE html>
<html>
<head>
	<title>AJAX In PHP</title>
</head>
<body>
<button onclick="buttonClickPOST()">POST</button>
<button onclick="buttonClickGET()">GET</button>
<script src="function.js"></script>

</body>
</html>

but if you catch some error like this,

Add this on your code:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

this is how look like;

<!DOCTYPE html>
<html>
<head>
	<title>AJAX In PHP</title>
</head>
<body>
<button onclick="buttonClickPOST()">POST</button>
<button onclick="buttonClickGET()">GET</button>
<script src="function.js"></script>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

</body>
</html>

Related Topics

One Reply to “Using Ajax in PHP”

  1. Hello! Do you know if they make any plugins to assist with Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good gains. If you know of any please share. Thank you!

Leave a Reply

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