Current location: Hot Scripts Forums » General Web Coding » JavaScript » AJAX - coding question

AJAX - coding question

Reply
  #1 (permalink)  
Old 02-09-07, 04:29 PM
mcrob mcrob is offline
Coding Addict
 
Join Date: Jul 2004
Posts: 266
Thanks: 0
Thanked 0 Times in 0 Posts
AJAX - coding question

Hey there how is it going I got a question

Go on this site: http://www.w3schools.com/php/php_ajax_database.asp

This is a great tutorial but I am concerned with somethings I see.

Code:
 var xmlHttp

function showUser(str)
{ 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 } 
var url="getuser.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
 } 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
1. In this javascript code is it possible to have it display a loading image when the user presses submit, then right after it loads then the loading image disappears?

Code:
<html>
<head>
<script src="selectuser.js"></script>
</head>
<body>

<form> 
Select a User:
<select name="users" onchange="showUser(this.value)">
<option value="Peter">Peter Griffin</option>
<option value="Lois">Lois Griffin</option>
<option value="Glenn">Glenn Quagmire</option>
<option value="Joseph">Joseph Swanson</option>
</select>
</form>

<p>
<div id="txtHint"><b>User info will be listed here.</b></div>
</p>

</body>
</html>
2. In the HTML Form, you'll notice that there is only one List/Menu field. Is it possible to have multiple text fields and retrieve the entered data in the PHP page?? And how can I include a Submit button so if the user clicks on submit then the AJAX event takes place. I know there maybe some changes I may need to do on the javascript code.

I really hope somebody can reply back to this topic, I am in need to know this desperately so I can create some stunning web applications.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 02-09-07, 07:06 PM
TwoD TwoD is offline
Community Liaison
 
Join Date: Sep 2003
Location: 404
Posts: 1,814
Thanks: 0
Thanked 0 Times in 0 Posts
The code for showing an image,
for example:
Code:
document.getElementById('imageId').style.display="";
needs to be placed right after
Code:
xmlHttp.send(null)
and the code to hide it again,
for example
Code:
document.getElementById('imageId').style.display="none"
needs to be placed after
Code:
document.getElementById("txtHint").innerHTML=xmlHttp.responseText
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 02-09-07, 08:15 PM
mcrob mcrob is offline
Coding Addict
 
Join Date: Jul 2004
Posts: 266
Thanks: 0
Thanked 0 Times in 0 Posts
Code:
var xmlHttp

function showUser(str)
{ 
xmlHttp=GetXmlHttpObject()

if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 } 
var url="getusers.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
document.getElementById('imageId').style.display="";
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("txtHint").innerHTML=xmlHttp.responseText
 document.getElementById('imageId').style.display="none"

 } 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
Thats cool...... but

I do not know where to put the image link

EG: <img src='images/ajax-animation.gif' />

Last edited by mcrob; 02-09-07 at 08:27 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 02-09-07, 11:54 PM
TwoD TwoD is offline
Community Liaison
 
Join Date: Sep 2003
Location: 404
Posts: 1,814
Thanks: 0
Thanked 0 Times in 0 Posts
The link can be placed anywhere on the page since getElementById will find it for you.

Just make sure you give it an id="myImgOrSomehting" attribute.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 02-12-07, 07:09 PM
Contrid Contrid is offline
Newbie Coder
 
Join Date: Dec 2006
Location: Online
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
For the image, you can do something like this :

HTML Code:
<img id="myImage" src="images/filename.gif" style="visibility:hidden;" />
and then use JavaScript to make it visible when the submit button is clicked :

HTML Code:
document.getElementById('myImage').style.visibility = "visible";
Good luck
If you have any questions, post them here.
__________________
Free, useful scripts - www.tribulant.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
AJAX, Simple Question (I think) nova912 JavaScript 10 09-04-06 03:27 PM
printDiv coding question... caboose96z JavaScript 4 06-25-05 10:17 AM
Coding Web Site Question. EMCEEBATTLE HTML/XHTML/XML 4 09-30-04 10:37 PM
coding question nitefire PHP 1 07-24-04 03:28 AM
question and answer software jaydifox C/C++ 0 02-21-04 10:26 AM


All times are GMT -5. The time now is 07:25 AM.
vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.