|
Author |
Message |
domuk
WebHelper
Joined: 15 Jan 2004
Posts: 91
|
Posted:
Wed May 19, 2004 4:21 pm (19 years, 11 months ago) |
|
I have two combo boxes on the same page and am using a PHP function to load the values into them.
To make the script more workable I would like to select the first one which lists “crags” and when that is selected I want the other combo box to only display climbs from the crag selected in the first box.
I have thought about using JavaScript (on change), but I was wondering if I could just use PHP to find out the first value and reload the second combo box.
I am not sure how to go about this and would like to look at some working code that does something similar.
Can anyone point me in the correct direction?
I have enclosed my code thus far for your information
The function to load the combo boxCode: |
function enhanced_list_box($options){
require_once ('../../mysql_connect.php'); // connects to the database
$sql = "select " . $options['id_field'];
$sql .= ", " . $options['value_field'];
$sql .= " from " . $options['table'];
//append any where criteria to the sql
if(isset($options['where_statement'])) {
$sql .= " where " . $options['where_statement'] ;
}
// set the sort order of the list
$sql .= " order by " . $options['value_field'];
$result = mysql_query($sql)
or die("error in SQL");
echo '<select name="', $options['id_field'], '" size="1">';
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
if($row[0] == $options['highlight_id']) {
echo '<option value="', $row[0], '" SELECTED>',
$row[1], '</option>';
} else {
echo '<option value="', $row[0], '">',
$row[1], '</option>';
}
}
echo '</select>';
}
|
Check the data and query the database (ignore escape_data fn)
Code: |
$page_title = 'climbers_climbes';
include ('templates/header.inc');
if (isset($_POST['submit']))
{
require_once ('../../mysql_connect.php'); // Connect to the db.
$message = NULL; // Create an empty new variable.
//_________________________________________________________________________
if ($_POST['climb_id'] == 1) // the form initial set condition and the first record in the database
{
$cid = FALSE;
$message .= '<p>You did not enter the climb </p>';
}
else
{
$cid = escape_data($_POST['climb_date']);
}
// if ($_POST['crag_id'] == 1) // the form initial set condition and the first record in the database
// {
// $crag = FALSE;
// $message .= '<p>You did not enter the crag</p>';
// }
// else
// {
// $crag = escape_data($_POST['crag_id']);
// }
//_________________________________________________________________________ if form data is ok
if ( $cid) //if $fn and $cid etc is not false continue
{ //start of if
// process form
require_once ('../../mysql_connect.php'); // http dir
$query = “blar blar blar";
$result = @mysql_query($query);
if ($result)
{
echo 'your data was passed to the database';
include ('templates/footer.inc'); // Include the HTML footer.
exit();
}
else
{
$message = 'your data was not passed due to a system error';
}
mysql_close();
}// end of if
else
{
$message .='please try again';
}
} //end of submit
if (isset($message))
{
echo '<font color="red">', $message, '</font>';
}
?>
|
Part of the form to build the combo boxes
Code: |
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>Crags
<?php
enhanced_list_box_crags(array(
'table' => 'crags',
'id_field' => 'crag_id',
'value_field' => 'crag_name',
'highlight_id' => 1));
?>
<p>Climb
<?php
enhanced_list_box(array(
'table' => 'climb_details',
'id_field' => 'climb_id',
'value_field' => 'climb_name',
'highlight_id' => 1));
?>
<input type="Submit" name="submit" value="Enter information" />
</form>
</font>
| [/code] |
|
|
|
|
Daniel
Team Member
Joined: 06 Jan 2002
Posts: 2564
|
Posted:
Wed May 19, 2004 4:30 pm (19 years, 11 months ago) |
|
I would actually do it with Javascript, since I find having forms reloaded while I'm filling them in is annoying. Also you'll have to use Javascript anyway to reload the page
http://forums.devshed.com/archive/t-128656 talks about ways of doing this with Javascript. |
________________________________
|
|
|
|
domuk
WebHelper
Joined: 15 Jan 2004
Posts: 91
|
Posted:
Thu May 20, 2004 8:37 am (19 years, 11 months ago) |
|
|
|
|
|
|
You cannot post new topics in this forum. You cannot reply to topics in this forum. You cannot edit your posts in this forum. You cannot delete your posts in this forum. You cannot vote in polls in this forum.
|
Page generation time: 0.125961 seconds :: 18 queries executed :: All Times are GMT
Powered by phpBB 2.0
© 2001, 2002 phpBB Group :: Based on an FI Theme
| |