Forum Moderators: coopster
The use of $_SERVER['PHP_SELF']; don't use this if you can help it especially if you intend to go live with this code/site as there are vulnerabilities with it, google $_SERVER['PHP_SELF']; to see what I mean. Instead just leave the action attribute blank ie: <form method="post" action=""> this will then keep you compliant with the sgml parser (W3C schools)
Is not, I believe, semantic. Instead, I suggest you do this:action=""
action="<?php echo $_SERVER['REQUEST_URI']; ?>"
As you commented a while back, you can leave the attribute blank, <form action="" method="post">
petty would be pulling me up on grammar too ;-)
New to PHP and thought I take this challenge but I'm losing. I want to create a drop down box based upon directory listing. Once an item is selected, it will bring up another drop down box box with its contents.
if (
(isset($_POST['list1']) and is_numeric($_POST['list1']) and ($_POST['list1'] > 0)) and
(isset($_POST['list2']) and is_numeric($_POST['list2']) and ($_POST['list2'] > 0))
) { $output = do_search(); }
else if (
(isset($_POST['list1']) and is_numeric($_POST['list1']) and ($_POST['list1'] > 0)) and
(! isset($_POST['list2']) or ! is_numeric($_POST['list2']) or ! ($_POST['list2'] > 0))
) {
$sub_option=sub_option_list($_POST['list1']);
$output = output_form($sub_option);
}
else { $output = output_form(); }
header("content-type:text/html");
echo $output;
//
// -------------- Done with program, functions follow
//
// not complete, just showing the logic
//
function output_form($options=null) {
$form = '<form method="post" action="some-script.php">
<label for="list1">Main Cat:</label>
<!-- note the onchange event handler . . . -->
<select name="list1" id="list1" onchange="this.form.submit();">
';
// however you get your list
$query = "select id,title from category order by title";
$result = mysql_query($query);
while ($row=mysql_fetch_array($result)) {
$form .= '<option value="' . $row[0] . '"';
// Or, selected="selected" for XHTML, if you're doing any extending.
if ($_POST['list1']==$row[0]) { $form .= ' selected'; }
$form .= '>' . $row[1] . '</option>';
}
$form .= '</select>';
if ($options) {
$form .= '<label for="list2">Sub Category:</label>' . $options;
}
else { $form .= '<-- Select Main Category'; }
$form .= '<input type="submit" value="next"></form>';
return $form;
}
//
// Do the SAME THING here as above, but use the
// main category to: select * from subcats where category = $category
//
function sub_option_list($category) {
if (! is_numeric($category) or ! ($category>0)) {
die("Oops/ Somehow you are requesting sub cat, but no main category.");
}
// carry on, create select list as above . . .
return $select_list;
}
//
// calling it search, whatever your function is
//
function do_search() {
//
return $search_results;
}