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.
– The target of the request url.
– The type of HTTP request either: "GET"
(Default) or "POST"
.
– 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 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 "
.
– The function that is fired when the AJAX
call has completed successfully.
– The function that is fired when the AJAX call encountered errors.
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.
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
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
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More