Question about script + ajax data submission

Source: Internet
Author: User
Beginner script + ajax data submission question HTML content
............

Password:


Email:


............


Script
Function submit (I)
{
Var sendData = "";
If (I = "1") {sendData = '{"action": "' + I + '", "pw":' + getElementByName + ', "email ": '+ getElementByName + ':}';}
$. Ajax ({
Timeout: 5000,
Type: "POST ",
Url: "edit_server.php ",
Data: sendData,
Success: function (data)
{
GetData (1); // This is a function used to open a page.
}
});
}
Script



Content in edit_server.php:

Require_once "functions. php ";
ConnectDb ();
If ($ _ GET ['action'] = "1 ")
{
$ Pw = $ _ POST ['pw '];
$ Email = $ _ POST ['email '];
Mysql_query ("UPDATE basic_information SET pw = '$ pw', email = '$ email ');
If (mysql_errno ()){
Echo mysql_error ();
} Else {
GetData (1 );
}
}


Reply to discussion (solution)

Several questions:
1. Check $ _ GET ['action'] = "1" in edit_server.php"
But there is no parameter in the ajax url (url: "edit_server.php ",)
Url: "edit_server.php? Action = "+ I,
2. sendData = '{"action": "' + I + '", "pw":' + getElementByName + ', "email":' + getElementByName + ':}'
After the value is assigned, sendData is a string and will not be parsed into a $ _ POST array by php.
To do this

sendData = {pw : document.getElementByName(‘pw').value, email : document.getElementByName('emal'][0].value }

The post method is used for front-end ajax submission, and the backend PHP uses if ($ _ GET ['action'] = "1 "){....} to verify the problem.

After modification


Script
Function submit (I)
{
Var sendData = "";
If (I = "1") {sendData = {pw: getElementByName ("pw"). value, email: getElementByName ("email"). value };}
$. Ajax ({
Timeout: 5000,
Type: "POST ",
Url: "edit_server.php? Action = "+ I,
Data: sendData,
Success: function (data)
{
Alert (data );
GetData (1 );
}
});
}
Script


Background:

If ($ _ POST ['action'] = "1 ")
{
$ Pw = $ _ POST ['pw '];
$ Email = $ _ POST ['email '];
Mysql_query ("UPDATE basic_information SET pw = '$ pw', email = '$ email ');
If (mysql_errno ()){
Echo mysql_error ();
} Else {
GetData (1 );
}
}

But it still cannot run. the console prompts ReferenceError: getElementByName is not defined

GetElementsByName

After modification, the data is still not passed in

Ajax method. after initialization, the system submits and sends parameters to receive the returned data.

Thank you very much for successfully solving the problem.
If (I = "1") {sendData = {pw: document. getElementByName ("pw "). value, email: document. getElementByName ("email "). value };}

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.