jquery ajax example,jQuery Ajax Post Data Example

jQuery $.post() method is used to request data from a webpage and to display the returned result (sent from requested page) on to that webpage from where the request has been sent without page refresh.

$.post() method sends request along with some data using an HTTP POST request.

Under this, a request is send to a webpage (here it is jquery_post.php) from another page (say jquery_send.php) using syntax :

Syntax:

$.post( URL, data, callback);

Parameters:

URL

The URL parameter is defined for the URL of requested page which may communicate with database to return results.

$.post("jquery_post.php",data,callback);

data

The data parameter is defined to send some data along with the request.

,{ // Data Sending With Request To Server

name:vname,

email:vemail

}

callback

The callback parameter is defined for a function to be executed if the request gets succeeded. This contains two sub parameters , the first one holds the returned data from the requested page and  second one holds the status of the request.

,function(response,status){ // Required Callback Function

//"response" receives - whatever written in echo of above PHP script.

alert("*----Received Data----*

Response : " + response+"

Status : " + status);

}

Note : Both ‘ data ‘ and  ‘ callback ‘ parameters are optional parameters, whereas URL is mandatory for $.post() method.

Below is our complete code with download and live demo option

e1519a7b1b6e99bff3ed6dce59f89072.png

Example:

The following example uses the $.post() method to send some data along with the request.

This is jquery_send.php page that contains jQuery $.post() method which can be implemented as given below:

$(document).ready(function(){

$("#btn").click(function(){

var vname = $("#name").val();

var vemail = $("#email").val();

if(vname=='' && vemail=='')

{

alert("Please fill out the form");

}

else if(vname=='' && vemail!==''){alert('Name field is required')}

else if(vemail=='' && vname!==''){alert('Email field is required')}

else{

$.post("jquery_post.php", //Required URL of the page on server

{ // Data Sending With Request To Server

name:vname,

email:vemail

},

function(response,status){ // Required Callback Function

alert("*----Received Data----*

Response : " + response+"

Status : " + status);//"response" receives - whatever written in echo of above PHP script.

$("#form")[0].reset();

});

}

});

});

jQuery Ajax $.post() Method


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值