Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Aug 2008
Posts: 66
Reputation: PsychicTide is an unknown quantity at this point 
Solved Threads: 7
PsychicTide's Avatar
PsychicTide PsychicTide is offline Offline
Junior Poster in Training

AJAX td?

 
0
  #1
Aug 5th, 2008
Alright ran into a bit of a problem on the item count in my index... let me give you the layout I'm working with.

I've got my index page with the header logo and navigation bar. This is followed by an Iframe. When I click the navigation buttons it changes to Iframe to show a new webpage.

The problem I'm having is, I have a <td> in the top part of the Index (page never changes) that has a text element inside of it that calls a PHP function to show the number of items I have in the cart. So I have to reload the main page, which reloads everything back to the start in order to see the number of items.

This is the function is case you were wondering
  1. function total_items(){
  2. global $sid;
  3. if($_SESSION["loggedin"]==1){
  4. $q1=mysql_query("SELECT SUM(qty) as total_items FROM cart_items WHERE uid='{$_SESSION["user_id"]}'");
  5. }else{
  6. $q1=mysql_query("SELECT SUM(qty) as total_items FROM cart_items WHERE session='$sid'");
  7. }
  8. $r1=mysql_fetch_array($q1);
  9. return $r1["total_items"];
  10. }

and this is how I call it in just the text area:
  1. <td><font color="#000000">(<?= total_items(); ?> items)</font></td>

my friend had mentioned something about AJAX, but I'm not too familiar with the blending of fields.

I appreciate any info I can get, even if its just a link to something to look up.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 2
Reputation: gralex07 is an unknown quantity at this point 
Solved Threads: 2
gralex07 gralex07 is offline Offline
Newbie Poster

Re: AJAX td?

 
0
  #2
Aug 5th, 2008
I got started with AJAX a couple of months ago and I love it.. There are few websites out there that have good tutorials, however, I have been using the same script for awhile, maybe this will help you get a feel for AJAX and say no more iframes!

There are three elements to my AJAX script

1. The main page, or page you are displaying
2. a javascript page, which is called into the main page
3. the results php page, which will display in a div on the main page right before your eyes

main page (hello_world.php)
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script language="JavaScript" src="world.js"></script>
  2. <?php
  3. //php code here
  4. $variable_from_page = "just an example variable";
  5. ?>
  6.  
  7. <div id="div_world"></div><br>
  8. <a href="#" onclick="world('<? echo "$variable_from_page"; ?>');">Click for AJAX</a>


Javascript page (world.js)
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var xmlHttp
  2. function world(variable_from_form)
  3. {
  4. xmlHttp=GetXmlHttpObject()
  5. if (xmlHttp==null)
  6. {
  7. alert ("Browser does not support HTTP Request")
  8. return
  9. }
  10. var url="world.php"
  11. //this passes the variable to world.php via get variable, you can add as many as you want
  12. url=url+"?variable_from_form="+variable_from_form+"&"
  13. url=url+"&sid="+Math.random()
  14. xmlHttp.onreadystatechange=stateChanged
  15. xmlHttp.open("GET",url,true)
  16. xmlHttp.send(null)
  17. }
  18. function stateChanged()
  19. {
  20. if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  21. {
  22. document.getElementById("div_world").innerHTML=xmlHttp.responseText
  23. }
  24. }
  25. function GetXmlHttpObject()
  26. {
  27. var xmlHttp=null;
  28. try
  29. {
  30. // Firefox, Opera 8.0+, Safari
  31. xmlHttp=new XMLHttpRequest();
  32. }
  33. catch (e)
  34. {
  35. //Internet Explorer
  36. try
  37. {
  38. xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  39. }
  40. catch (e)
  41. {
  42. xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  43. }
  44. }
  45. return xmlHttp;
  46. }

And finally the page that will display in the div (world.php)
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?php
  2. $variable = $_GET['variable_from_form'];
  3. //even though we have the variable from form I will output hello
  4. //I just brought this variable along to show how to get it from the main page to the div
  5. echo "Hello World";
  6. //you can exicute any code here and it will output in the div where world went
  7. ?>
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 66
Reputation: PsychicTide is an unknown quantity at this point 
Solved Threads: 7
PsychicTide's Avatar
PsychicTide PsychicTide is offline Offline
Junior Poster in Training

Re: AJAX td?

 
0
  #3
Aug 5th, 2008
interesting... it seems this may be easier than I tlhought.

Thanks alot for the info, greatly appreciated.

I'll play around with it a bit and see what I can't get working.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 66
Reputation: PsychicTide is an unknown quantity at this point 
Solved Threads: 7
PsychicTide's Avatar
PsychicTide PsychicTide is offline Offline
Junior Poster in Training

Re: AJAX td?

 
0
  #4
Aug 6th, 2008
heh, not as easy as I thought...

After toying with this for some time, what I've managed to do is make the small area change from the original text "(# items)" into a table the same size as the area before (turns it from words to a blanks spot). This at least accomplishs the task of only changing the one area instead of the entire site.

I'm not familiar enough with the syntax for writing javascripts or XML calls, I'm learning as I go. Maybe I messed up my echo, maybe the script itself, I'm not sure. Hopefully you can see the problem?

This is what I use on the main page holding the div (index.php):
I'm calling "total_items()" from my config.php
  1. <script language="JavaScript" src="itemcount_revert.js"></script>
  2. ...
  3. <?
  4. $cartNumbers = total_items();
  5. ?>
  6. ...
  7. <td width="50" height="20"><div id="cart_count"><font color="#000000">(<?= $cartNumbers; ?> items)</font></div></td>
  8. <td><a href="#" onclick="itemcount_revert(<? $cartNumbers; ?>);">Click for AJAX</a></td>

This is the .js (itemcount_revert.js):
  1. var xmlHttp
  2. function itemcount_revert($cartNumbers)
  3. {
  4. xmlHttp=GetXmlHttpObject()
  5. if (xmlHttp==null)
  6. {
  7. alert ("Browser does not support HTTP Request")
  8. return
  9. }
  10. var url="world.php"
  11. url=url+"?$cartNumbers="+$cartNumbers+"&"
  12. url=url+"&sid="+Math.random()
  13. xmlHttp.onreadystatechange=stateChanged
  14. xmlHttp.open("GET",url,true)
  15. xmlHttp.send(null)
  16. }
  17.  
  18. function stateChanged()
  19. {
  20. if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  21. {
  22. document.getElementById("cart_count").innerHTML=xmlHttp.responseText
  23. }
  24. }
  25.  
  26. function GetXmlHttpObject()
  27. {
  28. var xmlHttp=null;
  29. try
  30. {
  31. // Firefox, Opera 8.0+, Safari
  32. xmlHttp=new XMLHttpRequest();
  33. }
  34. catch (e)
  35. {
  36. //Internet Explorer
  37. try
  38. {
  39. xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  40. }
  41. catch (e)
  42. {
  43. xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  44. }
  45. }
  46. return xmlHttp;
  47. }

and this is the site displayed inside the div (world.php)
  1. <?
  2. $cartFinal = $_GET['cartNumbers'];
  3.  
  4. echo ("<table height=\"20\" width=\"40\">\n");
  5. echo ("<tr>\n");
  6. echo ("<td>.$cartFinal\n");
  7. echo ("</td>\n");
  8. echo ("</tr>\n");
  9. echo ("</table>\n");
  10. ?>

If I use "echo ("<td>.$cartFinal\n");" it comes back undefined. If I use "echo ($cartFinal);" it comes up blank.

By the way... is there any way I could possibly find a snippet to make it auto refresh every so often instead of having to submit or hit a link? (meta on the world.php maybe?, java refresh maybe?) could use a bit of help on this one too if you have the time =)
Last edited by PsychicTide; Aug 6th, 2008 at 8:42 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 213
Reputation: nikesh.yadav is an unknown quantity at this point 
Solved Threads: 17
nikesh.yadav's Avatar
nikesh.yadav nikesh.yadav is offline Offline
Posting Whiz in Training

Re: AJAX td?

 
0
  #5
Aug 6th, 2008
is problem solved or not
Help as an alias

I think programming is great................
Tour Travel weblink by me and about Tour ,
Go To My Home Page and I m in Webdevelopment.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 66
Reputation: PsychicTide is an unknown quantity at this point 
Solved Threads: 7
PsychicTide's Avatar
PsychicTide PsychicTide is offline Offline
Junior Poster in Training

Re: AJAX td?

 
0
  #6
Aug 6th, 2008
just posted that... have any ideas?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 381
Reputation: langsor is an unknown quantity at this point 
Solved Threads: 33
langsor langsor is offline Offline
Posting Whiz

Re: AJAX td?

 
0
  #7
Aug 6th, 2008
Using Ajax to update one number is like killing a gnat with a sledgehammer ... unless you plan on using Ajax to update your page (cart) content as well -- in which case you might lose people who have javascript turned off in their browser.

Why don't you pass the item count value in with the iFrame document and use some javascript to update the value in the iFrame's parent document?

Something like this in the iFrame updated document would work
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <head>
  2. <script type="text/javascript">
  3. parent.document.getElementById('item_count').firstChild.nodeValue = '12 items';
  4. </script>
  5. </head>

And you would have your static (parent) document with the menu bar and item count printout look like this
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <body>
  2. <a href="updated.html" target="iframe">update cart</a>
  3. <p id="item_count">0 items</p>
  4. <iframe name="iframe" src="default.html"></iframe>
  5. </body>

So when you load the new document, it automatically updates your item count.

Hope this helps
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 66
Reputation: PsychicTide is an unknown quantity at this point 
Solved Threads: 7
PsychicTide's Avatar
PsychicTide PsychicTide is offline Offline
Junior Poster in Training

Re: AJAX td?

 
0
  #8
Aug 7th, 2008
No luck using the method you posted, but thanks alot for the response Langsor.

With the current setup, I can get the box text change to say "(undefined items)", but not understanding where the variable is getting lost.

This is the world.php as of now:
  1. <?
  2. $cartFinal = $_GET['cartNumbers'];
  3.  
  4. echo ("<font color=\"#000000\">(".$cartFinal." items)</font>");
  5. ?>

Index and itemcount_revert are the same... any ideas?

I kind of like the sledghammer approach... can use this stuff for later projects
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 381
Reputation: langsor is an unknown quantity at this point 
Solved Threads: 33
langsor langsor is offline Offline
Posting Whiz

Re: AJAX td?

 
1
  #9
Aug 7th, 2008
I admit, it's good to learn how to use the asynchronous http objects ... I did test my approach and it does work and is very easy to implement (compared to what you're doing) so if you change your mind let me know.

I noticed in post #4 from you above
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script language="JavaScript" src="itemcount_revert.js"></script>
  2. ...
  3. <?
  4. $cartNumbers = total_items();
  5. ?>
  6. ...
  7. <td width="50" height="20"><div id="cart_count"><font color="#000000">(<?= $cartNumbers; ?> items)</font></div></td>
  8. <td><a href="#" onclick="itemcount_revert(<? $cartNumbers; ?>);">Click for AJAX</a></td>
Is the = total_items() a PHP function you're calling, or is it supposed to be a JavaScript function? Because if it's supposed to be JavaScript, you can't have PHP and JavaScript talk directly like that -- they're from opposite sides of the tracks.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?
  2. $cartFinal = $_GET['cartNumbers'];
  3.  
  4. echo ("<font color=\"#000000\">(".$cartFinal." items)</font>");
  5. ?>
A couple tests you can do to see where you're losing your variable data
1. give $cartFinal a static or string value to see if it echoes in your html
2. enter the page.php?cartNumbers=12 in your browser location bar to see if you're getting the $_GET query to the PHP

Are you thinking that the Ajax response will be returned as a $_GET variable, because it's returned from the xmlHttp.response type object value.

Good luck
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 66
Reputation: PsychicTide is an unknown quantity at this point 
Solved Threads: 7
PsychicTide's Avatar
PsychicTide PsychicTide is offline Offline
Junior Poster in Training

Re: AJAX td?

 
0
  #10
Aug 8th, 2008
Sorry it took me so long to get back to you... after thinking for a while, I decided not to try and pass the variable. Instead I just put an include to my config.php(holds total_items function) on the return page (world.php) and have it refresh on an interval of 5 seconds instead of having to hit a button..

Altho I'll play with what you posted and see if I can't get it to work for me in a later project of course =).

Thanks alot for all the help from you guys. You really saved me alot of stress and confusion reading more bad tutorials and misleading forum posts. I can now say I know the basic of applying AJAX .
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC