2 questions involving scripts, select tag, and loading pages

Reply

Join Date: Jan 2005
Posts: 70
Reputation: Diode is on a distinguished road 
Solved Threads: 0
Diode's Avatar
Diode Diode is offline Offline
Junior Poster in Training

2 questions involving scripts, select tag, and loading pages

 
0
  #1
May 30th, 2008
Hi,

I'm making a website for a friend who sells sports memorabilia and I'm using a LAMP setup. I am making a menu that utilizes AJAX. The menu consists of a SELECT tag, we'll call this SELECT1, where the user clicks it, it drops down a list of leagues such as MLB, NCAA, NFL, etc., which are pulled out from a league MySQL table.

After the league is selected, a SELECT2 appears, where the user selects the conference such as National League/American League, NFC/AFC, etc.

When that is chosen, a list of that conference's teams shows up below.

It works great. Except when the page is refreshed. In that case, the page is re-loaded and the league selection stays there, but it doesn't show the conference SELECT2 tag. And then even if I click on it, nothing happens unless I change the selection, because I have the script that takes care of the conference to be executed during the onchange event in SELECT1.

How do I make it so that even if the page is refreshed, it still knows the value in SELECT1? There isn't any onload event that I know of for it; I tried it.

Here is my code so far:
  1. <?php
  2.  
  3. include('connect.php');
  4. include('htmlheader.php');
  5.  
  6. $menuleagueno = $_SESSION['mleagueno'];
  7. $menuteamno = $_SESSION['mteamno'];
  8.  
  9. //
  10. // echo "<html><body background='bricks2.jpg'>";
  11.  
  12. echo "
  13. <table border=0 cellspacing=0 cellpadding=0>
  14. <tr>
  15. <td background='bricks6.jpg' height=700 valign=top font style: color=white>
  16. <form>
  17. <font color=white>
  18. Select a sports league:<br />";
  19. echo "<select name='league' onload='leagues(this.value)'>";
  20.  
  21. echo "
  22. <option value=0>Select a sports league:
  23. <option value=0>--------------------------";
  24.  
  25. $sql = "select * from leagues";
  26. $result = mysql_query($sql);
  27.  
  28. while($row = mysql_fetch_array($result))
  29. {
  30. $leagueno = $row['leagueno'];
  31. $leaguedesc = $row['leaguedesc'];
  32.  
  33. echo "<option value=$leagueno>$leaguedesc";
  34. }
  35.  
  36. echo " </select></p>
  37. </font>
  38. </form>
  39.  
  40. <span id='results'></span>
  41. </td>
  42. </tr>
  43. </table> ";
  44.  
  45. echo "</body></html>";
  46.  
  47. // include('htmlfooter.php');
  48. ?>

This file is called menu.php. I include this in the main pages, and position it on the left.

My next question is how do I (without using frames) retain the value of what's in the SELECT tags in menu.php? I tried doing
  1. $menuleagueno = $_GET['leagueno'];
  2. $menuteamno = $_GET['teamno'];
  3.  
  4. include('menu.php?leagueno=$menuleagueno');

to test to see if this method would work, and it didn't. It gave me a syntax error, and treated the whole line as a filename, it said it couldn't find the file. So without putting menu.php in its own frame, how would I keep the menu intact, going from page to page? Constantly having to go through it and select your team would get very annoying to a user.

I hope I described this in sufficient detail, if not, I apologize in advance, and will do my best to do so if you need further clarification.

Thanks,
Diode
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: 2 questions involving scripts, select tag, and loading pages

 
0
  #2
May 30th, 2008
I use a session to remember the value. It has worked perfectly for me everytime i have done it that way.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 70
Reputation: Diode is on a distinguished road 
Solved Threads: 0
Diode's Avatar
Diode Diode is offline Offline
Junior Poster in Training

Re: 2 questions involving scripts, select tag, and loading pages

 
0
  #3
May 31st, 2008
Thanks for your reply, but how do you do it? Do you use If-then-elses and use the onload event for the page to execute the javascript?

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 70
Reputation: Diode is on a distinguished road 
Solved Threads: 0
Diode's Avatar
Diode Diode is offline Offline
Junior Poster in Training

Re: 2 questions involving scripts, select tag, and loading pages

 
0
  #4
Jun 1st, 2008
I'm pretty new to AJAX. I think I have found that you can't use php functions on events such as onchange. Is this correct? Do you have to use a javascript function to set the session variables in this case? If so, how do you use javascript to set php session variables? This is very confusing to me.

Thanks,
Diode
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 70
Reputation: Diode is on a distinguished road 
Solved Threads: 0
Diode's Avatar
Diode Diode is offline Offline
Junior Poster in Training

Re: 2 questions involving scripts, select tag, and loading pages

 
0
  #5
Jun 1st, 2008
I wasn't quite sure if this belonged in php since I'm using php as the main scripting language, or if it should go in the AJAX board. If this is off-topic here, please accept my apologies.

Diode
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: 2 questions involving scripts, select tag, and loading pages

 
0
  #6
Jun 1st, 2008
you are right that you can't use php functions with onchange events. this is because php is server side, meaning that its processed by the server before being sent to the browser. javascript is client side. what you need to do is get ajax(pretty much javascript) to send info to the php script where the session will be set.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 70
Reputation: Diode is on a distinguished road 
Solved Threads: 0
Diode's Avatar
Diode Diode is offline Offline
Junior Poster in Training

Re: 2 questions involving scripts, select tag, and loading pages

 
0
  #7
Jun 8th, 2008
I implemented the session variables, but I have found that the only events SELECT tags respond to are onfocus, onblur, and onchange.

I put the javascript function inside an onload event in the body, which did nothing. I can access the session variables, but if the page is loaded, how do I make the elements of the menu appear without the user having to re-enter all the team data?

  1. leagueno = $mleagueno
  2.  
  3. <onload = 'leagues($mleagueno)'>

this function works if it is placed within the onchange event in the select tag, and there is a value in $mleagueno because it is displayed on the page. Any idea what's going on here?

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 70
Reputation: Diode is on a distinguished road 
Solved Threads: 0
Diode's Avatar
Diode Diode is offline Offline
Junior Poster in Training

Re: 2 questions involving scripts, select tag, and loading pages

 
0
  #8
Jun 9th, 2008
Wait. I think I might have figured it out. Do I put the javascript function in the onload event in the body tag?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC