Page 1 of 1

Ajax sending to PHP

Posted: Mon Jun 14, 2010 12:15 pm
by manRay
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.

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'");	
	
}
and ajax code:

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

Re: Ajax sending to PHP

Posted: Mon Jun 14, 2010 1:23 pm
by internet-solution
Your variable names do not match. For example, you are using $fname while getting $_GET value, but in SQL you are using $first.

Re: Ajax sending to PHP

Posted: Mon Jun 14, 2010 1:27 pm
by Kurby
This could be caused by a variety of problems.

1) Is your javascript completing without errors?

2) Is this part of your javascript being executed?

Code: Select all

 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;
    }
3) Is this part working as expected?

Code: Select all

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
Are you getting the values right before you pass them? Maybe your element Ids are off.

4) You should also thinking about URL encoding your variables before sending them. In your PHP script you should then decode them user mysql_escape_string.


This is a standard debugging procedure. Check each part of your code and identify errors. You should be using a javascript debugger like the one built into chrome and IE or firebug(my favorite) addon for firefox.

Re: Ajax sending to PHP

Posted: Mon Jun 14, 2010 1:33 pm
by manRay
Wow. The littlest things. Lol Up and running. Thanks!