jquery ajax php post,javascript - jQuery Ajax POST example with PHP - Stack Overflow

To make an Ajax request using jQuery you can do this by the following code.

HTML:

A bar

JavaScript:

Method 1

/* Get from elements values */

var values = $(this).serialize();

$.ajax({

url: "test.php",

type: "post",

data: values ,

success: function (response) {

// You will get response from your PHP page (what you echo or print)

},

error: function(jqXHR, textStatus, errorThrown) {

console.log(textStatus, errorThrown);

}

});

Method 2

/* Attach a submit handler to the form */

$("#foo").submit(function(event) {

var ajaxRequest;

/* Stop form from submitting normally */

event.preventDefault();

/* Clear result div*/

$("#result").html('');

/* Get from elements values */

var values = $(this).serialize();

/* Send the data using post and put the results in a div. */

/* I am not aborting the previous request, because it's an

asynchronous request, meaning once it's sent it's out

there. But in case you want to abort it you can do it

by abort(). jQuery Ajax methods return an XMLHttpRequest

object, so you can just use abort(). */

ajaxRequest= $.ajax({

url: "test.php",

type: "post",

data: values

});

/* Request can be aborted by ajaxRequest.abort() */

ajaxRequest.done(function (response, textStatus, jqXHR){

// Show successfully for submit message

$("#result").html('Submitted successfully');

});

/* On failure of request this function will be called */

ajaxRequest.fail(function (){

// Show error

$("#result").html('There is error while submit');

});

The .success(), .error(), and .complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use .done(), .fail(), and .always() instead.

MDN: abort() . If the request has been sent already, this method will abort the request.

So we have successfully send an Ajax request, and now it's time to grab data to server.

PHP

As we make a POST request in an Ajax call (type: "post"), we can now grab data using either $_REQUEST or $_POST:

$bar = $_POST['bar']

You can also see what you get in the POST request by simply either. BTW, make sure that $_POST is set. Otherwise you will get an error.

var_dump($_POST);

// Or

print_r($_POST);

And you are inserting a value into the database. Make sure you are sensitizing or escaping All requests (whether you made a GET or POST) properly before making the query. The best would be using prepared statements.

And if you want to return any data back to the page, you can do it by just echoing that data like below.

// 1. Without JSON

echo "Hello, this is one"

// 2. By JSON. Then here is where I want to send a value back to the success of the Ajax below

echo json_encode(array('returned_val' => 'yoho'));

And then you can get it like:

ajaxRequest.done(function (response){

alert(response);

});

There are a couple of shorthand methods. You can use the below code. It does the same work.

var ajaxRequest= $.post("test.php", values, function(data) {

alert(data);

})

.fail(function() {

alert("error");

})

.always(function() {

alert("finished");

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值