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 };}