Jump to content


AJAX Help: Using Two responseText in one file


2 replies to this topic

#1 .CJ

    Young Padawan

  • Members
  • Pip
  • 114 posts
  • Gender:Male
  • Location:Leeds, UK

Posted 10 January 2007 - 12:41 AM

Hello,

I never post here at the P2L forums, but am a regular visitor to the site and enjoy many of the tutorials that get put up, I learn a lot from them! Today I was looking around the forum for AJAX related matters, and saw that their was some quite helpful resources and people willing to help, so I hope my little problem can be resolved and hope you're not the type who laugh in the face of people who aren't regulars, lol.

So here's my query:

I am using AJAX to edit a field in one of my database tables via a input box and using the onBlur attribute. This will sound odd, but that part I have managed to do and it works great!! to give you an insight, here is my code:

AJAX:
function editForm() {
	ajax.open('GET', 'edit_form.php?name='+document.getElementById('name').value);
	ajax.onreadystatechange = function() {
		if(ajax.readyState == 4 && ajax.status == 200) {
			document.getElementById("editInput").innerHTML = ajax.responseText;
		}
	};
	ajax.send(null);
}

My index.php:
<?php
include('../include/functions.php');
connect();
$query = mysql_query("SELECT * FROM applications");
$row = mysql_fetch_array($query);
echo('
<div id="editInput">
<input type="text" id="name" value="'.$row['display_name'].'" onblur="editForm(); return false" /><br /><br />
</div>
');
mysql_close();
?>

Edit_form.php:
<?php
error_reporting(E_ALL ^ E_NOTICE);

include('../include/functions.php');
connect();

if($_GET['name']) {
mysql_query("UPDATE applications SET display_name = '".$_GET['name']."'");
echo($_GET['name']);
}

mysql_close();
?>

So all that works, after I re-type into the input and cause an onBlue event to happen, the input will change from an input box to the value of that database row.

What I want is to be able to do this more than once, with multiple inputs on the same page, I had a go, but whenever the onBlue event occurred, nothing happened, it stayed as a input box and didn't update the database.

Here is the code I used:

AJAX:
function editForm() {
	ajax.open('GET', 'edit_form.php?name='+document.getElementById('name').value+'&username='+document.getElementById('user').value);
	ajax.onreadystatechange = function() {
		if(ajax.readyState == 4 && ajax.status == 200) {
			document.getElementById("editInput").innerHTML = ajax.responseText;
		}
	};
	ajax.send(null);
}

The difference is here that I added +'&username='+document.getElementById('user').value to the open() function

Index.php:
<?php
include('../include/functions.php');
connect();
$query = mysql_query("SELECT * FROM applications");
$row = mysql_fetch_array($query);
echo('
<div id="editInput">
<input type="text" id="name" value="'.$row['display_name'].'" onblur="editForm(); return false" /><br /><br />
</div>
<div id="editInput">
<input type="text" id="user" value="'.$row['username'].'" onblur="editForm(); return false" />
</div>
');
mysql_close();
?>

The difference here is I added another 'editInput' handler and another input box with a different ID and value from the DB.

Edit_form.php:
<?php
error_reporting(E_ALL ^ E_NOTICE);

include('../include/functions.php');
connect();

if($_GET['name']) {
if(mysql_query("UPDATE applications SET display_name = '".$_GET['name']."'")) {
echo(''.$_GET['name'].'<br />');
}
}
else {
if($_GET['username']) {
if(mysql_query("UPDATE applications SET username = '".$_GET['username']."'")) {
echo($_GET['username']);
}
}
}

mysql_close();
?>

The difference here is I added an 'else' statement and another 'if' function but with the $_GET as 'username' and setting the MySQL query to set another field in the table.

So I hope I have given enough info about what my problem is and I hope somebody can help me out, I would appreciate it so much.

Thanks for taking the time to read and look forward to hearing from you!

Chris.

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 13 January 2007 - 01:51 AM

var object='';
function editForm(obj) {
	object = obj.id;
	ajax.open('GET', 'edit_form.php?'+obj.name+'='+obj.value);
	ajax.onreadystatechange = function() {
			if(ajax.readyState == 4 && ajax.status == 200) {
				object.parentNode.innerHTML = ajax.responseText;
			}
		}
	ajax.send(null);
}
<?php
include('../include/functions.php');
connect();
$query = mysql_query("SELECT * FROM applications");
$row = mysql_fetch_array($query);
echo('
<div>
	<input type="text" name="name" value="'.$row['display_name'].'" onBlur="editForm(this);" /><br /><br />
</div>
<div>
	<input type="text" name="user" value="'.$row['username'].'" onBlur="editForm(this);" />
</div>
');
mysql_close();
?>
<?php
error_reporting(E_ALL ^ E_NOTICE);

include('../include/functions.php');
connect();

if($_GET['name']) {
	mysql_query("UPDATE applications SET display_name = '".mysql_real_escape_string($_GET['name'])."'") or die(mysql_error());
	die($_GET['name']);
}

if($_GET['username']) {
	mysql_query("UPDATE applications SET username = '".mysql_real_escape_string($_GET['username'])."'") or die(mysql_error());
	die($_GET['username']);
}

die('Nothing updated.<br><pre>'.print_r($_GET,1)); /* <pre>'.print_r() used for debuging, should be removed when the script goes live. */
?>
This should theoretically work. It's going on the principle that a variable set out side of any functions/object/loops will have global scope and therefore be accessable inside of any functions. I've used it before in several functions that i've wrote, but i can't remember if it will work in this situation (best thing to do is try, worst thing that can happen is nothing will happen).

The reason for using a global variable is so that each input area can blur individually and the script will automatically know which one did it and send the right info to /edit_info.php via the input's names. Then the readystatechange function will know which div to change thanks to a little DOM and that global variable (i see problems with pressing the tab key really fast, but hopefully nothing will happen, you might want to test it though).

Then for the php, we check each get var individually as i can't think of a nice way to use a switch for this. Inside the if-statements i added mysql_real_escape_string() to all of the queries because you should NEVER trust user input, ESPECIALLY when that input is via AJAX. I also added error checking to the queries which, if all goes well, will be what the input divs update to should a problem arrise (thus informing the user something went wrong). Then rather than echo'ing the update info, i set it to simply die() so that it will end the script (only one field will be updated at a time, no sense in checking for a username/etc once we found that it was the name being updated). Again, i added a die() at the end for more error handling.

Unfortunately for you i always forget a quote or something, so i'm not guarantying this thing first time, but it's a start.

#3 .CJ

    Young Padawan

  • Members
  • Pip
  • 114 posts
  • Gender:Male
  • Location:Leeds, UK

Posted 18 January 2007 - 02:22 PM

Hey,

Sorry for the late reply, I thought I wasn't gonna get a response so I stopped checking, lol, and I haven't tried your method yet, but I will do soon and get back to you on it.

Thanks for your help, appreciate it!

- Chris





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users