Ajax sending to PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
manRay
Forum Commoner
Posts: 78
Joined: Mon Feb 09, 2009 1:57 pm

Ajax sending to PHP

Post 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";
}
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: Ajax sending to PHP

Post 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.
Kurby
Forum Commoner
Posts: 63
Joined: Tue Feb 23, 2010 10:51 am

Re: Ajax sending to PHP

Post 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.
manRay
Forum Commoner
Posts: 78
Joined: Mon Feb 09, 2009 1:57 pm

Re: Ajax sending to PHP

Post by manRay »

Wow. The littlest things. Lol Up and running. Thanks!
Post Reply