TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-23-2008, 05:11 AM   #1 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default AJAX Help

My whole 3 hours of ajax experince has hit a brick wall. I am able to get the data i want through the ajax but i cannot get it to update the display of the page. Heres what i am trying to use.

Code:
//strait from w3schools.com
//well almost i changed a little.
<script type="text/javascript">
function ajaxFunction(){
var xmlHttp;
try  {
  xmlHttp=new XMLHttpRequest();
  } catch (e)  {
  try {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
    try{
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function() {
    if(xmlHttp.readyState==4) {
      alert(xmlHttp.responseText);
      document.getElementByName('guide1')=xmlHttp.responseText;
      }
    }
  xmlHttp.open("GET","test.php",true);
  xmlHttp.send(null);
  }
</script>
And my call to the function.

Code:
echo "<h7 name=\"guide1\">8</h7><a href=\"#\" onClick=\"ajaxFunction()\">+ Guide</a>...
All the test.php does is output the word done. and when i click on my link here the alert box says done. but the h7 tag will not update. Any ideas where im screwing up?
CoryMathews is offline  
Reply With Quote
Old 02-23-2008, 05:18 AM   #2 (permalink)
The Contributor
 
DeMo's Avatar
 
Join Date: Jan 2008
Location: Brazil
Posts: 77
Thanks: 14
DeMo is on a distinguished road
Default

As far as I know.. you can't assign a new "text" directly like this:
Code:
document.getElementByName('guide1') = xmlHttp.responseText
You have to use the innerHTML property of the element, like this:
Code:
document.getElementByName('guide1').innerHTML = xmlHttp.responseText
Also, I think it's more common to use document.getElementById instead of getElementByName. I don't know if it has something to do with browser compatibility, standards or something like that.. but all the examples I see all the time are using getElementById. Of course, to use getElementById you should set the id=".." property of the element instead of the name=".." property.
Send a message via ICQ to DeMo Send a message via MSN to DeMo Send a message via Skype™ to DeMo
DeMo is offline  
Reply With Quote
Old 02-23-2008, 06:19 AM   #3 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

There is no 'getElementByName' method. There is 'getElementsByName' and 'getElementsByTagName', both return arrays of element names and tag names, respectively.

Use 'getElementById' and the innerHTML property when dealing with text, as DeMo stated.

There's an excellent JavaScript and DOM reference at javascriptkit.com.
__________________
I reject your reality, and substitute my own.
SOCK is offline  
Reply With Quote
The Following User Says Thank You to SOCK For This Useful Post:
CoryMathews (02-24-2008)
Old 02-23-2008, 10:02 PM   #4 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

thanks for the help so far guys. When i changed it to byId it works. Now i gotta add one last feature. That is to pick which id to update by passing in a var (idKey). Problem is the variable i pass in is always undefined where i alert it. Heres the new javascript.

Code:
<script type="text/javascript">
function ajaxFunction(IdKey){
var xmlHttp;
try  {
  xmlHttp=new XMLHttpRequest();
  } catch (e)  {
  try {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
    try{
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function(IdKey) {
    if(xmlHttp.readyState==4) {
		alert(IdKey)
      document.getElementById('IdKey').innerHTML=xmlHttp.responseText;
      }
    }
  xmlHttp.open("GET","test.php",true);
  xmlHttp.send(null);
  }
</script>
CoryMathews is offline  
Reply With Quote
Old 02-23-2008, 11:23 PM   #5 (permalink)
The Contributor
 
DeMo's Avatar
 
Join Date: Jan 2008
Location: Brazil
Posts: 77
Thanks: 14
DeMo is on a distinguished road
Default

Code:
document.getElementById('IdKey').innerHTML=xmlHttp.responseText;
You have to use IdKey without the single quotes. IdKey is a variable.. you want the value stored inside it then you have to use it without the quotes. The way you're doing the browser is searching for an element with the id property set to IdKey (id="IdKey").

Edit: I forgot the part where you say the alert(IdKey) is outputting "undefined". Did you change the call to ajaxFunction() to pass the id of the element you want to update?

Should be something like this:
Code:
<h7 id="guide1">8</h7><a href="#" onClick="ajaxFunction('guide1')">+ Guide</a>
Send a message via ICQ to DeMo Send a message via MSN to DeMo Send a message via Skype™ to DeMo
DeMo is offline  
Reply With Quote
Old 02-23-2008, 11:42 PM   #6 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

ye i tried that before i added the quotes, but i get either [object HTMLUnknownElement] if i place the alert right after the function call or i get undefined if its where the current alert is.

Edit: in reply to your edit lol i had a div onclick i changed it to your a tag, now the variable gets to the first function. but it doesnt get to the current alert its still undefined.
CoryMathews is offline  
Reply With Quote
Old 02-24-2008, 05:26 AM   #7 (permalink)
The Contributor
 
DeMo's Avatar
 
Join Date: Jan 2008
Location: Brazil
Posts: 77
Thanks: 14
DeMo is on a distinguished road
Default

Found the error
Code:
xmlHttp.onreadystatechange=function(IdKey) {
Just erase that IdKey that's inside the brackets, make it like this:
Code:
xmlHttp.onreadystatechange=function() {
Send a message via ICQ to DeMo Send a message via MSN to DeMo Send a message via Skype™ to DeMo
DeMo is offline  
Reply With Quote
The Following User Says Thank You to DeMo For This Useful Post:
CoryMathews (02-24-2008)
Old 02-24-2008, 02:28 PM   #8 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

ok i got it to work. You were right i needed to remove the idKey in the ready state change. But i also needed to remove the a href link and change it to a

Code:
<h7 id=\"$guideId\">8</h7><h6><div onClick=\"ajaxFunction('$guideId')\">+ Guide</div></h6>
CoryMathews is offline  
Reply With Quote
Old 02-25-2008, 03:47 AM   #9 (permalink)
The Contributor
 
DeMo's Avatar
 
Join Date: Jan 2008
Location: Brazil
Posts: 77
Thanks: 14
DeMo is on a distinguished road
Default

Hmmm the normal link works for me (Opera 9.26).
The href property has to be "#", and you call the function via the onclick="" property.
Code:
<a href="#" onclick="ajaxFunction('element_id')">Link text</a>
Send a message via ICQ to DeMo Send a message via MSN to DeMo Send a message via Skype™ to DeMo
DeMo is offline  
Reply With Quote
Old 02-25-2008, 05:10 AM   #10 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

hum im on the 9.5 beta and it didn't but it might be the beta. thanks though.
CoryMathews is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:15 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design