Ajax sending to PHP
Posted: Mon Jun 14, 2010 12:15 pm
I am trying to send some input values to a php script using ajax and than sent to a database. I need the php script to update some fields with the variables sent from the ajax script. The php script does update the fields on the database but only when the fields are updated with string literals. Meaning database does not update with the variable sent from the ajax.When I submit this form, blanks appear in the database.
and ajax code:
Code: Select all
$connection = mysql_connect("*******","*****","*****");
if ($connection) {
mysql_select_db("*******", $connection);
$email = $_SESSION["email"];
$pass = $_GET["password"];
$fname = $_GET["first"];
$lname = $_GET["last"];
$add = $_GET["address"];
$zip = $_GET["zip"];
mysql_query("UPDATE accounts SET password='$pass', first='$first', last='$last', address='$address', zip='$zip' WHERE email='$email'");
}Code: Select all
function SaveAccount() {
password = document.getElementById("edit_pswrd").value;
first = document.getElementById("edit_fname").value;
last = document.getElementById("edit_lname").value;
address = document.getElementById("edit_address").value;
zip = document.getElementById("edit_zipcode").value
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","../php/saveaccount.php?password=" + password + "&first=" + first + "&last=" + last + "&address=" + address + "&zip=" + zip,true);
xmlhttp.send();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("passwordfield").innerHTML= password;
document.getElementById("fnamefield").innerHTML= first;
document.getElementById("lnamefield").innerHTML= last;
document.getElementById("addressfield").innerHTML= address;
document.getElementById("zipcodefield").innerHTML= zip;
} }
editting = false;
document.getElementById("editlink").innerHTML = "Edit";
}