Jquery

jQuery Ajax Example Using PHP

This tutorial help to create AJAX call with PHP and JavaScript. jQuery provide $.ajax() function used to get response from script through asynchronous/synchronous manner.

AJAX stands for Asynchronous JavaScript and XML, and it allows you to fetch content from the back-end server asynchronously, without a page refresh. 

It’s provide dynamic data loading using backend script. In other words , with help of ajax you can dynamically load partial page or portion of page. It will also help to submit form data server side and saved into database.

jQuery ajax function have a lot of properties but I am describing only few common properties.

url

– The target of the request url.

type

– The type of HTTP request either: "GET" (Default) or "POST".

async

– When we set asynchronous to TRUE then it will load data in background and this will allow you to run mutiple AJAX requests at the same time. If you set to FALSE the request will run and wait response from server,this is working only single threading process.

data

– data as a key value pair and send to target request script for process. example "{param1: 'value1'}".
dataType – Specify the type of data that is returned: "xml/html /json ".

Related Post
success

– The function that is fired when the AJAX call has completed successfully.

error

– The function that is fired when the AJAX call encountered errors.

 

Source Code for jQuery AJAX Request Using PHP

that.getDetails = function(params) {
 alert(params['target']);
$.ajax({ 
         url: siteurl +params['url'], //request url
         data: {action : ‘test’},//data which you want send to server side
         type: 'post', //request type
         success: function(response) {
    alert(response);//when ajax request success
                      $(params['target']).html(response);
                  },
         error:function (xhr, ajaxOptions, thrownError){
                    alert(xhr.status);//if ajax request failed
                    alert(thrownError);
                }    
      
});

I have created a common method will take URL as parameter. This method ll send asynchronous request to the server.

The $ sign is used to refer to a jQuery object.

The url is the first parameter that will be called in the background to fetch content from the server side. 

The data hold the information that ll send to the server as a request payloads.

The type is http request method.

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

4 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

10 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

10 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

10 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

11 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

11 months ago

Categories