How does php receive the ajaxpost form?

Source: Internet
Author: User
How does php receive the ajaxpost form? A.html
Var params = $ ("# form1"). serialize ();
$. Ajax ({
Type: "post ",
ContentType: "application/text ",
Data: params,
DataType: "json ",
// DataType: "binary ",
Async: false,
Timeout: 30000,
Url: '1. php ',
Success: function (data ){
// Process data
Var s = eval (data );
Alert (s. a + ''+ s. B );
},
Error: function (x, e ){
// Alert (x. readyState );
Alert (e );
}
});
-------------------------------
1. php
$ Db = new mysqli ('localhost', 'adb', 'winst', 'test ');
Mysqli_query ($ db, "set character set 'utf8 '");
$ Arr =$ _ REQUEST;
$ Vnameid = $ arr ['username'];
$ Vtele = $ arr ['telphone'];
$ Query = "insert into cardvip (namevip, tel) VALUES ('" + $ vnameid + "', '" + $ vtele + "')";
$ Db-> query ($ query );

$ Data = json_encode (array ('a' => $ vnameid, 'B' => $ vtele ));
Echo $ data;
?>

If $ vnameid = 'username ';
$ Vtele = 'telphone ';
1. html alert (s. a + ''+ s. B); correctly displayed.

The post data is not received.


Reply to discussion (solution)

Comment out dataType: "json"
Alert (s. a + ''+ s. B); changed to alert (data );

1. php
Print_r ($ _ POST );
Exit;

Use this change for debugging

Comment out dataType: "json"
Alert (s. a + ''+ s. B); changed to alert (data );

1. php
Print_r ($ _ POST );
Exit;

Use this change for debugging

No problem with the returned data,
$ Arr =$ _ REQUEST;
$ Vnameid = $ arr ['username'];
$ Vtele = $ arr ['telphone'];
I mean $ vnameid cannot obtain post data

$ Vnameid = $ arr ['username']; the value cannot be obtained?

$ Vnameid = $ arr ['username']; the value cannot be obtained?
Well, the value cannot be obtained.
If a.html uses url: '1. php? '+ Params,
You can get the value. what is the problem?

Your ajax parameter dataType: "json", which transmits serialized data.
Remove dataType: "json ".

Did you do what I said?

Did you do what I said?

A.html
Var params = $ ("# form1"). serialize ();
$. Ajax ({
Type: "post ",
ContentType: "application/text ",
Data: params,
Async: false,
Timeout: 30000,
Url: '1. php ',
Success: function (data ){
// Process data
Alert (data );
},
Error: function (x, e ){
// Alert (x. readyState );
Alert (e );
}
});
-----------------------------------
1. php
$ Arr =$ _ REQUEST;
$ Vnameid = $ arr ['username'];
$ Vtele = $ arr ['telphone'];
Echo $ vnameid;
?>

Still cannot get the post value

ContentType: "application/text ",
Remove this. Is there this MIME?

Delete contentType: "application/text ",

OK. Just remove contentType: "application/text ".

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.