Ajax response not exactly correct

Get help on web editors (Frontpage, Dreamweaver) and web languages (HTML, ASP, PHP).
Post Reply
DarkRanger
Registered User
Posts: 8346
Joined: 10 May 2006, 02:00
Processor: Intel i5-3750
Motherboard: Gigabyte
Graphics card: nVidia GTX 550Ti
Memory: 8GB Jetram
Contact:

Ajax response not exactly correct

Post by DarkRanger »

Okay, so in short, our office has an extension list that they want on our intraweb. This extension list must be editable by the receptionist.

Here are my problems:

1. When deleting a record, the AJAX response doesn't come through. It deletes the extension as requested, but still shows the deleting message. The response area is a <tr> with id=rowID (where ID is the database ID, so database id 333 will be <tr id=row333>)

Here is the Javascript and php.

Code: Select all

function deleteID(id)
{
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=deleteItem(id);
	xmlhttp.open("GET","extensionEdit.php?id="+id,true);
	xmlhttp.send(null);
}

function deleteItem(id)
{
	if (xmlhttp.readyState!=4)
	{
		document.getElementById("row"+id).innerHTML="<img src=\"../../images/breadcrumbload.gif\"/>&nbsp;&nbsp;Deleting Extension..."
	}
	if (xmlhttp.readyState==4)
	{
		document.getElementById("row"+id).innerHTML=xmlhttp.responseText;
	}
}

Code: Select all

$id = $_GET['id'];
if(!mysql_query("DELETE FROM extensions WHERE id='$id'"))
	die(mysql_error);
else
	echo "<td colspan='2'>&nbsp;</td>";
2. When adding a record, the AJAX response comes through, but only part of it. It's supposed to add a new row with the new ext box at the top and then add the newest extension to the top of the list (below the new ext box). The response area is a <tr> with id=newOne. What it does is it adds the new extension next to the new ext box.

Code: Select all

function addExt(id)
{
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=stateChanged;
	var name = document.getElementById('name').value;
	var ext = document.getElementById('ext').value;
	var office = document.getElementById('office').value;
	xmlhttp.open("GET","extensionEdit.php?name="+name+"&ext="+ext+"&office="+office,true);
	xmlhttp.send(null);
}

function stateChanged()
{
	if (xmlhttp.readyState!=4)
	{
		document.getElementById("newOne").innerHTML="<img src=\"../../images/breadcrumbload.gif\"/>&nbsp;&nbsp;Adding Extension..."
	}
	if (xmlhttp.readyState==4)
	{
		document.getElementById("newOne").innerHTML=xmlhttp.responseText;
	}
}

Code: Select all

$name = $_GET['name'];
$ext = $_GET['ext'];
$office = $_GET['office'];

if(!mysql_query("INSERT INTO extensions (name, ext, office) VALUES ('$name', '$ext', '$office')"))
	die(mysql_error);
else
{
	echo "<td>
		<input type='hidden' id='office' name='office' value='".$_GET['branch']."'/>
		<input type='text' id='name' name='name'/>
	</td>
	<td>
		<input type='text' id='ext' name='ext'/>
	</td>
	<td>
		<span onclick='addExt()'>+</span>
	</td>
</tr>
<tr id='row$id'>
	<td>$name</td>
	<td>$ext</td>
	<td><span onclick='deleteID(\"$id\")'>X</span></td>";
}
When I look at the code it echoes, it shows this:

Code: Select all

<span onclick="addExt()">+</span></td><tr><td>Dawie</td>
Notice that it throws away the </tr> bracket.

Any help?
Image
-Prometheus-
Resident Drama Llama
Posts: 967
Joined: 05 Mar 2008, 02:00
Contact:

Re: Ajax response not exactly correct

Post by -Prometheus- »

........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
Last edited by -Prometheus- on 04 Apr 2011, 03:37, edited 1 time in total.
BBLounge - Broadband and Technology forum
Please like our facebook page
DarkRanger
Registered User
Posts: 8346
Joined: 10 May 2006, 02:00
Processor: Intel i5-3750
Motherboard: Gigabyte
Graphics card: nVidia GTX 550Ti
Memory: 8GB Jetram
Contact:

Re: Ajax response not exactly correct

Post by DarkRanger »

Okay, I've fixed problem two by just adding a div tag around the table, and then echoing the whole table when the DB has been updated. Sure, not ideal, but the data requested from the DB is very small (200 lines of 3 columns of about 5 characters avg).

Now, with the delete item I have kind of narrowed my problem down. What I did was in my deleteItem(id) function when the finished reply comes through, it should pop up an alert box saying done. It is however not showing this popup. So my guess is that it's not finishing the call. It IS deleting the entry from the DB, but not replying ready.

@-Prometheus-
Please show me where I used the variable in different functions and I'll tell you why or fix it.
Image
DarkRanger
Registered User
Posts: 8346
Joined: 10 May 2006, 02:00
Processor: Intel i5-3750
Motherboard: Gigabyte
Graphics card: nVidia GTX 550Ti
Memory: 8GB Jetram
Contact:

Re: Ajax response not exactly correct

Post by DarkRanger »

Back to problem one:

Firefox adds the entry to the database but shows no feedback on the form. Thought it was perhaps because xmlhttp.onreadystatechange=stateChanged; didn't have a () after it, but when I put that in, FF said document.getElementById('name') can't be found. But it is there.

Ah, i'm loosing hope.
Image
-Prometheus-
Resident Drama Llama
Posts: 967
Joined: 05 Mar 2008, 02:00
Contact:

Re: Ajax response not exactly correct

Post by -Prometheus- »

........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
Last edited by -Prometheus- on 04 Apr 2011, 03:42, edited 1 time in total.
BBLounge - Broadband and Technology forum
Please like our facebook page
DarkRanger
Registered User
Posts: 8346
Joined: 10 May 2006, 02:00
Processor: Intel i5-3750
Motherboard: Gigabyte
Graphics card: nVidia GTX 550Ti
Memory: 8GB Jetram
Contact:

Re: Ajax response not exactly correct

Post by DarkRanger »

Oh damn... Perhaps that is why it doesn't respond properly as it has two to choose from... Thanks for pointing that out. I'm having a look at it now. Something so easy to miss...
Image
Post Reply