Comboboxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
krism
Forum Newbie
Posts: 1
Joined: Mon Apr 14, 2003 6:09 am

Comboboxes

Post by krism »

Hi everyone

I have a problem that I have been trying to solve for a long time with no joy.

I am trying to create an online sports hall booking form. There are 3 comboxes, one for the activity, day and time. The data that I want to populate these comboboxes is stored in a mysql database.

I am trying to get this working so that when the user selects the activity they want it updates the day combobox with the days of the week, then then a day is selected I want to populate the time combo with the avalible times.

The tables that I have in the mysql database are:
activity
monday,tuesday,wednesday,thursday, friday - these have a list of time between 08:00-18:00
saturday, sunday - these have a list of time between 12:00-16:00

The problem that I cant sole is how to get each combobox to update when a selection is made,

Any help would be much apreciated


Thakns in advance
User avatar
Guy
Forum Commoner
Posts: 53
Joined: Sun Jan 12, 2003 3:34 am

Post by Guy »

put them in form.
make only the activity available for scrolling at first, and populate it with the data you want (from the DB).
when selecting the activity, refresh the page with the activity id, and
populate the day and time combos by the data you have in your database.
I'd consider using javascript to control the date and time combos.
Guy
User avatar
oQEDo
Forum Commoner
Posts: 43
Joined: Thu Feb 20, 2003 11:08 am
Location: UK

some code

Post by oQEDo »

Populate your dropdown using code like this:

Code: Select all

<select name="site" size="1" onChange="javascript:formHandler()">
<?php
  $Query = "SELECT * from sports";
  $Result = mysql_db_query($DBName,$Query,$Link);
  while ($Row = mysql_fetch_array($Result))
    &#123;
?>
      <option value="Default.asp?sport=<?php print("$Row&#1111;sport]")?>"><?php print("$Row&#1111;sport]")?></option>
<?php
    &#125;
?>
</select>
You will notice that the dropdown code uses a small amount of javascript, this eliminates the need for a submit button, you'll need this between the <head> tags:

Code: Select all

<script LANGUAGE="JavaScript">
function formHandler(form)
&#123;
var URL = document.form.site.options&#1111;document.form.site.selectedIndex].value;
window.location.href = URL;
&#125;
</script>
The code checks for sports which are chosen and the sport name is passed in the address bar as a querystring, if a sport is chosen and passed then a SQL statement is built accordingly:

Code: Select all

<?php
if(isset($_GET&#1111;'sport']) && $_GET&#1111;'sport'] !== '')
&#123;
   $sport = $_GET&#1111;'sport'];
&#125;   
else
&#123;
   $sport = "None";
&#125;

switch($sport)
&#123;
  case "football":
    &#123;
      $Query = "SELECT * from daysavailable";
    &#125;
  break;
 
  case.....etc
&#125;
 
?>
Then just repeat the code above to populate your next dropdown with the times available and so on.

I hope this has given you an idea however I am pretty new to php so there are probably better ways of doing this :roll:

RM
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

You can eliminate the Javascript function in the <head> by simulating the submit button with:

Code: Select all

<?php
     print "<SELECT NAME=Inum ONCHANGE='this.form.submit();'>\n";
?>
Post Reply