Page 1 of 1

Comboboxes

Posted: Mon Apr 14, 2003 6:09 am
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

Posted: Mon Apr 14, 2003 7:34 am
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

some code

Posted: Mon Apr 14, 2003 8:18 am
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

Posted: Mon Apr 14, 2003 8:44 am
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";
?>