Using ISSET with checkboxes - php

I am working on a wordpress search form to refine the current search and what Im trying to do is have the search results page with the search from and it's values set based on the query.
So far I've been successful in doing so with single select drop downs and single checkboxes like so --
<!-- SINGLE SELECT -->
<select name="baths" class="form-control">
<?php if (isset($_GET['baths'])) {
$bths = $_GET['baths']; ?>
<option value="<?php echo $bths; ?>"><?php echo $bths; ?></option>
<?php } else { ?>
<option value="Any">Any</option>
<?php } ?>
<option value="Any">Any</option>
<option value="1">1+</option>
<option value="2">2+</option>
<option value="3">3+</option>
<option value="4">4+</option>
<option value="5">5+</option>
<option value="6">6+</option>
<option value="7">7+</option>
<option value="8">8+</option>
<option value="9">9+</option>
<option value="10">10+</option>
</select>
<!-- SINGLE CHECKBOX -->
<input type="checkbox" name="dogs" class="styled" value ="yes" <?php if (isset($_GET['dogs'])) { ?>checked<?php } ?>>
That works, but for the multiple values it doesn't. This is my function to generate a set of checkboxes to select amenities -
<?php
$amenity_array = array();
$id = get_query_var('site');
if (!empty($id)) {
$property_amenities = get_post_meta($id, 'imic_property_amenities', true);
global $imic_options;
foreach ($property_amenities as $properties_amenities_temp) {
if ($properties_amenities_temp != 'Not Selected') {
array_push($amenity_array, $properties_amenities_temp);
}
}
}
global $imic_options;
if (isset($imic_options['properties_amenities']) && count($imic_options['properties_amenities']) > 1) {
foreach ($imic_options['properties_amenities'] as $properties_amenities) {
$am_name = strtolower(str_replace(' ', '', $properties_amenities));
$check = '';
if (in_array($properties_amenities, $amenity_array)) {
$check = 'checked="checked"';
}
<!-- HERE I TRY TO FIND THE SELECTED CHECKBOXES AND CHECK THEM OFF -->
if (isset($_GET['p_am'])) {
$ams = $_GET['p_am'];
echo '<div class="checkbox"><input type="checkbox" name="p_am" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
} else {
echo '<div class="checkbox"><input type="checkbox" name="p_am" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
}
<!-- END ISSET -->
}
} else {
_e('There is no Properties Amenities', 'framework');
}
?>
For the multi select drop down I am using bootstrap multiselect, so on my template the code looks like this --
<select name="property_type[]" id="pt-multi" class="form-control multi-select2" multiple="multiple">
<?php
$terms = get_terms( "property-type", array( 'hide_empty' => 0 ) );
$count = count($terms);
if ( $count > 0 ){
echo "<option value='Any'>All</option>";
foreach ( $terms as $term ) {
echo "<option value='" . $term->slug . "'>" . $term->name . "</option>";
}
}
?>
</select>
On the page it renders out as ---
<select name="property_type[]" id="pt-multi" class="form-control multi-select2 iOSselect" multiple="multiple" style="display: none;">
<option value="Any">All</option>
<option value="co-op">Co-Op</option>
<option value="condo">Condo</option>
</select>
<div class="btn-group" style="width: 100%;">
<button type="button" class="multiselect dropdown-toggle btn btn-default form-control multi-select2" data-toggle="dropdown" title="Property Type" style="width: 100%; overflow: hidden; text-overflow: ellipsis;">
<span class="multiselect-selected-text">Property Type</span>
<b class="caret"></b></button>
<ul class="multiselect-container dropdown-menu pull-right">
<li class="multiselect-item multiselect-all">
<a tabindex="0" class="multiselect-all">
<label class="checkbox"><input type="checkbox" value="multiselect-all"> Select all</label>
</a>
</li>
<li>
<a tabindex="0"><label class="checkbox">
<input type="checkbox" value="Any"> All</label>
</a>
</li>
<li>
<a tabindex="0"><label class="checkbox"><input type="checkbox" value="co-op"> Co-Op</label>
</a>
</li>
<li>
<a tabindex="0"><label class="checkbox"><input type="checkbox" value="condo"> Condo</label>
</a>
</li>
</ul>
</div>
Any ideas?
UPDATE
Using my code from above my amenities checkbox renders out on the page like so ---
<div>
<label>amenities</label>
<div class="checkbox">
<input type="checkbox" name="p_am" class="styled" value="Doorman (Full Time)">
<label for="doorman(fulltime)">Doorman (Full Time)</label>
</div>
<div class="checkbox">
<input type="checkbox" name="p_am" class="styled" value="Doorman (Part Time)"><label for="doorman(parttime)">Doorman (Part Time)</label>
</div>
<div class="checkbox">
<input type="checkbox" name="p_am" class="styled" value="Laundry (In-Unit)"><label for="laundry(in-unit)">Laundry (In-Unit)</label>
</div>
<div class="checkbox">
<input type="checkbox" name="p_am" class="styled" value="Fitness Center"><label for="fitnesscenter">Fitness Center</label>
</div>
</div>
This works with my search function allowing users to check off which amenities to include in the query string --
p_am=Doorman+%28Full+Time%29&p_am=Indoor+Pool
My search function is extensive but as far the relevant parts to the amenities -
$amenities = isset($_GET['p_am'])?$_GET['p_am']:'';
$amenities = ($amenities == __('Any', 'framework')) ? '' : $amenities;
// .....
if (!empty($amenities)) {
array_push($meta_query,array(
'key' => 'imic_property_amenities',
'value' => $amenities,
'compare'=>'LIKE'
));
}
UPDATE
In regards to setting the dropdown options for the bootstrap multiselect, I'm trying to set the options using the following code -
<?php
$taxonomyName = "city-type";
$parent_terms = get_terms( $taxonomyName, array( 'parent' => 0, 'orderby' => 'slug', 'hide_empty' => false ) );
echo "<select name='property_nhb[]' class='form-control multi-select' id='p-nhb' multiple>";
foreach ( $parent_terms as $pterm ) {
// echo "<option data-val='" . $pterm->slug . "' value='Any' " . $selected . ">Any</option>";
//Get the Child terms
$terms = get_terms( $taxonomyName, array( 'parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => false ) );
foreach ( $terms as $term ) {
if(isset($_GET['property_nhb[]'])) {
$pnhb = $_GET['property_nhb'];
$selected = 'selected';
}
echo "<option data-val='" . $pterm->slug . "' value='" . $term->slug . "' " . $selected . " >" . $term->name . "</option>";
}
}
echo "</select>";
?>
My current search function code

This seems a little backwards for creating a search form - it seems more like you're generating a form for each individual result? Annotated code and comments below. I've tried to keep to your coding style in my answer.
$amenity_array = array();
$id = get_query_var('site');
// this first section gets the selected meta properties for the queried property (if available)
// [skipping for brevity ]
global $imic_options;
if (isset($imic_options['properties_amenities']) && count($imic_options['properties_amenities']) > 1) {
// this loops over all of the globally defined properties
foreach ($imic_options['properties_amenities'] as $properties_amenities) {
// gets the name of the property
$am_name = strtolower(str_replace(' ', '', $properties_amenities));
// and sets up to check the box if the property has the defined amenity
// if this is for a search form, why check boxes based on a result?
$check = '';
if (in_array($properties_amenities, $amenity_array)) {
// note: this only really needs to be 'checked'
$check = 'checked="checked"';
}
Here's where things go a little cross-purposes, though.
if (isset($_GET['p_am'])) {
$ams = $_GET['p_am'];
echo '<div class="checkbox"><input type="checkbox" name="p_am" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
} else {
echo '<div class="checkbox"><input type="checkbox" name="p_am" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
}
This says that all checkboxes have the same name ("p_am") instead of using the loop name for the amenity ($am_name) or some form combination if you want all amenities in the same search input array (e.g. "p_am[$am_name]").
Each checkbox would also change the value of $check if supplied to the $_GET array.
$ams = $_GET['p_am'];
if (isset($ams[$am_name])) {
$check = 'checked';
}
Each checkbox would have name="p_am['.$am_name.']" as the name.
echo '<div class="checkbox"><input type="checkbox" name="p_am[' . $am_name . ']" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
If you want each of the amenities to have unique names (looking at the original checkbox which isn't called p_am at all) and not be in an array in PHP, then you would just use the loop name for the amenity ($am_name), like so:
if (isset($_GET[$am_name])) {
$check = 'checked';
}
Each checkbox would have name="'.$am_name.'" as the name.
UPDATE: after the OP was updated, it looks like every checkbox needs to have the same name, but not be keyed values. For this situation, your checkboxes should be called p_am[] (on both search page an original page), and you need to use something like in_array() instead of isset() to check the result, like so:
if (in_array($properties_amenities, $_GET['p_am'])) {
$check = 'checked';
}
Additional note - you're also using $am_name for the label, without actually setting the id attribute on the checkbox to match to, as well as not stripping out non-id characters (like parentheses), so the label association won't work.
echo '<div class="checkbox"><input type="checkbox" name="' . $am_name . '" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
For the bootstrap select, you're just missing a check for which options are selected:
<select name="property_type[]" id="pt-multi" class="form-control multi-select2" multiple="multiple">
<?php
$terms = get_terms( "property-type", array( 'hide_empty' => 0 ) );
$count = count($terms);
if ( $count > 0 ){
echo "<option value='Any'>All</option>";
foreach ( $terms as $term ) {
// if the option is 'checked', you need to add the 'selected' atttibute here
echo "<option value='" . $term->slug . "'>" . $term->name . "</option>";
}
}
?>
</select>
So the inside of the loop would look something like this instead:
// for efficiency's sake should live outside the loop as a one-off, but here for illustrative purposes
$types = $_GET['property_type'];
$selected = isset($types[$term->slug]) ? 'selected' : '';
echo "<option value='" . $term->slug . "'" . $selected . ">" . $term->name . "</option>";
If you're using an older version of bootstrap select, you may need to have the selected string be 'selected="selected" or data-selected="true", depending on the version you're using. For a current version, the above string should be fine.
If the <select> isn't meant to be an array of options either (i.e. not multiple), then the [] should be removed from the name, and would operate similarly to the checkbox code:
<select name="property_type" id="pt-multi" class="form-control multi-select2">
<?php
$terms = get_terms( "property-type", array( 'hide_empty' => 0 ) );
$count = count($terms);
if ( $count > 0 ){
echo "<option value='Any'>All</option>";
foreach ( $terms as $term ) {
$type = $_GET['property_type'];
$selected = isset($type) && $type == $term-slug ? 'selected' : '';
echo "<option value='" . $term->slug . "'" . $selected . ">" . $term->name . "</option>";
}
}
?>
</select>
The original HTML for the <select> doesn't actually 'work' as such either - all it's doing is adding a duplicate <option> to the top of the options list, and because nothing is selected, the form will treat the duplicate created at the top as 'selected' for the <select>.
UPDATE 2: the reason that the search function is breaking with an array of checkbox inputs is because you are using the LIKE comparison for your meta query. From the documentation on WordPress Meta Query:
It can be an array only when compare is 'IN', 'NOT IN', 'BETWEEN', or 'NOT BETWEEN'.
The documentation also shows examples of how to join these together. Since you haven't supplied how you're actually making the search query this is guessing a little, but it looks like for an array of checkboxes your code should be something similar to:
if (!empty($amenities)) {
foreach ($amenities as $amenity) {
array_push($meta_query, array(
'key' => 'imic_property_amenities',
'value' => $amenity,
'compare' => 'LIKE'
));
}
}
As with the examples in the documentation, you'll need to make sure there's a relation => 'OR' or relation => 'AND' to define how you want your search to operate.

what Fred's meant in the comment is something like this:
<input type="checkbox" value="condo" name="var_name[]">
the [] at the end of the name attribute is to tell PHP that it should treat the variable as an array.
and then in the PHP script, you can just check for the array length using somthing like:
$arr = $_POST['var_name']; echo count($arr);
based on leith's comment, if it is auto generated, then you should not be bothered to check the checkboxes manually.
instead, just make the selected options be selected, for example:
<?php
$selections = $_GET['property_type']; // or something similar
?>
<select name="property_type[]" id="pt-multi" class="form-control multi-select2" multiple="multiple">
<?php
$terms = get_terms( "property-type", array( 'hide_empty' => 0 ) );
$count = count($terms);
if ( $count > 0 ){
echo "<option value='Any'>All</option>";
foreach ( $terms as $term ) {
$selected = in_array($term->slug, $selections);
echo "<option value='" . $term->slug . "' " . ($selected?' selected':'') . ">" . $term->name . "</option>";
}
}
?>
</select>
see this fiddle https://jsfiddle.net/u1w158tp/
in it you just selected the options and it will automatically checked the checkboxes

Your checkboxes has the same name. Try to add different name or use "p_am[]" to treat them as array.

Related

How to Use Dynamic Dropdown Selected value in Php

Php/Html:
Here I add The all Countries list to add/edit page.But Edit page Want Value Selected in dropdown.please help
<select class="form-control" id="country_list" name="country">
<?php
global $wpdb;
$network_lists = $wpdb->get_results("SELECT * FROM media_countries");
foreach($network_lists as $network_list){
?>
<option value="<?php echo $network_list->country_name ?>" selected="<?php
echo $network_list->country_name ?>"><?php echo $network_list>country_name ?></option>
<?php }?>
</select>
This is how I would do it. This will check the value against the value the user selects with the value from the database and properly populate your dropdown menu.
global $wpdb;
$network_lists = $wpdb->get_results("SELECT * FROM media_countries");
echo
'<select class="form-control" id="country_list" name="country">';
foreach($network_lists as $network_list) {
if($_POST['country'] == $network_list->country_name) {
echo '<option value="' . $network_list->country_name . '"' . ' selected="selected"' . '>' . $network_list->country_name . '</option>';
}else {
echo '<option value="' . $network_list->country_name . '">' . $network_list->country_name . '</option>';
}
}
echo
'</select>';
Selected value is in $_POST['country'];
Try:
<select class="form-control" id="country_list" name="country">
<?php
global $wpdb;
$network_lists = $wpdb->get_results("SELECT * FROM media_countries");
foreach($network_lists as $network_list){
?>
<option value="<?php echo $network_list->country_name; ?>" <?php echo ( $_POST['country_name'] == $network_list->country_name ? 'selected' : '' ); ?>><?php echo $network_list>country_name; ?></option>
<?php }?>
</select>
The above code automatically selects the option that has value equals to the submitted $_POST['country'] value.

Pre-populating and compare select field with 'selected' option

I am pulling form data from home.php into results.php and using $_GET['xxxxx'] to pre-populate fields of another form. I can populate input fields okay, but how would you compare an option field and $sale_type to selected if equal?
home.php
<form action="results.php" method="GET">
<input id="address" name="address" type="text" class="form-control1"/>
<select type="text" name="sale_type" placeholder="Sale Type">
<option value="Sale">For Sale</option>
<option value="Rent">To Rent</option>
</select>
<input name="submit" type="SUBMIT" value="Next" class="form-control1">
</form>
results.php
Option fields are already populated to ensure the form will work if it's used without results from home.php. I need to compare $sale_type value with the option of name="sale_type and if equal change that option value to selected.
$address = $_GET['address'];
$sale_type = $_GET['sale_type']; ?>
<form method="POST">
<input id="address" name="address" type="text" value='<?php echo $address; ?>'>
<select type="text" name="sale_type" placeholder="Sale Type">
<option value="Sale">For Sale</option>
<option value="Rent">To Rent</option>
</select>
<button type="submit" id="filter">Search</button>
<input type="hidden" name="action" value="ek_search">
</form>
What I'd like results to do
$address = $_GET['address'];
$sale_type = $_GET['sale_type']; ?> //If value is Sale
<form method="POST">
<input id="address" name="address" type="text" value='<?php echo $address; ?>'>
<select type="text" name="sale_type" placeholder="Sale Type">
<option value="Sale" selected>For Sale</option> //Change to selected if equal
<option value="Rent">To Rent</option>
</select>
<button type="submit" id="filter">Search</button>
<input type="hidden" name="action" value="ek_search">
</form>
You could check and then marked as selected when condition satisfy. One of the example is :
<option value="Sale" <?php if(condition) echo "selected" ?> >For Sale</option>
You'd compare the variable to value in the option tag like so:
<option value="Sale" <?= ($sale_type === 'Sale') ? 'selected' : ''; ?>>For Sale</option>
Another, option, if you will, is to set an array of sale_types. Instead of checking each individual option value, you could loop through the options and check there. Like this:
$sale_types = [
'Sale' => 'For Sale',
'Rent' => 'To Rent',
'SomeOtherType' => 'Something Else'
];
Then in select element:
<? foreach ($sale_types as $sale_value => $sale_option): ?>
<option value="<?= $sale_option; ?>" <?= ($sale_type === $sale_option) ? 'selected' : ''; ?>><?= $sale_value; ?></option>
<? endforeach; ?>
If you're going to do very much of this at all, my recommendation is to build a PHP function. Otherwise, you end up with a bunch of mixed HTML / PHP to check conditions.
This allows you to set up and display a dropdown quickly any time you need to.
Something like this would do what you want and would be re-usable:
// Function to generate the HTML for a select
function dropdown_array( $name, $value, $array, $placeholder = '' ) {
$temp_input = '<select name="' . $name . '"';
$temp_input .= ( $placeholder ) ? ' placeholder="' . $placeholder . '"' : '';
$temp_input .= '>' . PHP_EOL;
if ( is_array( $array ) ) {
foreach ( $array as $val => $text ) {
$temp_input .= '<option value="' . $val . '"';
if ( $value === $val ) {
$temp_input .= ' selected';
}
$temp_input .= '>' . $text . '</option>' . PHP_EOL;
}
}
$temp_input .= '</select>' . PHP_EOL;
return $temp_input;
}
Then, in your situation, usage would look like so:
$array = array( 'Sale' => 'For Sale', 'Rent' => 'To Rent' );
echo dropdown_array( 'sale_type', $sale_type, $array, 'Sale Type' );

Post dropbox selected values numerical id -codeigniter

I'm new to codeigniter and its developing. I have used html dropbox inside the form. when I press the submit button I want to retrive and post id of selected drop box index value. How do I do that? please find the code I used.
<?php
echo form_open('homepage/askquestionview');
?><div class="form-group">
<select name="cat" id="cato" onchange="activate_match()">
<?php
foreach ($catogories as $cat) {
echo'<option value="' . $cat . '" id="cato" >' . $cat . '</option>';
}
?>
</select>
<input type="submit" class="btn btn-success btn-block" value="Post Your Question" id="postQuestion">
</p>
<?php echo form_close(); ?>
homepage/askquestionview function
public function askquestionview() {
$data = array(
'Student_Email' => $this->input->post('cato'),
);
var_dump($data);
}
output
array(1) { ["Student_Email"]=> bool(false) }
Your select input name is 'cat' not 'cato'
<select name="cat"
Try a var_dump on $this->input->post() :
public function askquestionview() {
var_dump(this->input->post());
}
Tips, use form_helper to create your input : form_dropdown()
https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html
I don't know much about the data structure of codeigniter, but you'll probably have to modifiy for loop like this:
foreach ($catogories as $id => $cat) {
echo'<option value="' . $id . '" id="cato" >' . $cat . '</option>';
}

Retain dynamically selected value from drop down list, but still be able to see list after submit

I have got the following form:
<div id="Quick_find_container">
<form action="" method="post" target="_self">
<div id="qcategory_1">Product</div>
<div id="qcategory">
<select name="Category" class="dropmenu" id="Category"onchange="this.form.submit()">
<option value="">Any</option>
<option value="Keyboard"<?php if ($_POST['Category']=="Keyboard") {echo "selected='selected'"; } ?>>Keyboard</option>
<option value="Piano"<?php if ($_POST['Category']=="Piano") {echo "selected='selected'"; } ?>>Piano</option>
</select>
</div>
<div id="qbrand_1">Brand</div>
<div id="qbrand"><select name='Manufacturer' onchange="this.form.submit()">
<?php
echo '<option value="">Any</option>';
$value = $_POST['Manufacturer'];
while ($row = mysql_fetch_array($RS_Search)) {
echo "<option value=" . $row['Manufacturer'] . " ' . (selected == $value ? ' selected' : '') . '>" . $row['Manufacturer'] . "</option>";
}
?>
</select>
</div>
<div id="qsubmit">
<input name="Search2" type="submit" id="Search2" value="Submit">
</div>
</form>
</div>
<?php echo $_POST['Category']; ?>
<?php echo $_POST['Manufacturer']; ?>
Echo post Category and Manufacturer are purely to see what it is submitting.
My problem is the second drop down menu. I would like to display after something is selected what the selected value was. At the moment it just jumps back to the default value Any even though the out put of POST_[Manufacturer'] is correct.
Is there a way to display the selected value like in the first drop down menu? I still would like to keep the values to choose from the database as well. But just display the selected value.
Any help welcome
Your line that echos the option has a lot of problems with quotes. This will fix it:
while ($row = mysql_fetch_array($RS_Search)) {
$selected = $_POST['Manufacturer'] == $row['Manufacturer'] ? 'selected' : '';
echo '<option ' . $selected . ' value="' . htmlspecialchars($row['Manufacturer']) . '">' . htmlspecialchars($row['Manufacturer']) . '</option>';
}
Side note: remember to use htmlspecialchars() or similar when outputting variables into HTML to prevent XSS.
In theory the code you have there might work altough i'd had made it like this
while ($row = mysql_fetch_array($RS_Search)) {
$selected = ($_POST['Manufacturer'] == $row['Manufacturer']) ? 'selected="selected":'';
echo '<option '.$selected.'>' . $row['Manufacturer'] . '</option>";
}?>
You do not need value if you have the same thing within the tag. Also you were missing some quote marks in your tag. Learn to simplify your code so it's easier to understand.

How to make form elements "remember" selection?

I am using php to build a "change classifieds" page right now.
I use Mysql as a db.
Currently I use PHP to fetch all mysql information about the classified, and then I output it like this:
$table.="
<select name='year' id='year'>
<option id='2000' value='2000'>2000</option>
<option id='2001' value='2001'>2001</option>
<option id='2002' value='2002'>2002</option>
</select>";
echo $table;
I have a picture upload tool which submits the page to itself, and at the same time uploads the picture, and shows it at the bottom of the page. The problem is, whenever this is done, the user must fill in all information again, because they are "forgotten".
I know about input type="text" where you simply can do something like this:
<input type="text" name="text" value="<?php echo $_POST['text'];?>">
but what about selects? Radios? etc?
What should I do here?
Thanks
You can set the selected attribute to "selected" to make it the default/current selection:
<option id="2000" value="2000" selected="selected">2000</option>
php example:
$options = array( '2000' => '2000', '2001' => '2001', '2002' => '2002' );
$selected = '2001';
$select = '<select name="year" id="year">';
foreach ( $options as $key => $value )
{
$select .= ' <option value="' . $key . ( $key == $selected ? '" selected="selected">' : '">' ) . $value . '</option>';
}
$select .= '</select>';
<option
id='2000'
value='2000'
<?php if(isset($year) && $year === '2000') echo 'selected="selected"'?>
>2000</option>
Where $year contains the year from wherever. This assumes $year is a string. If $year is an integer change the condition to:
if(isset($year) && $year === 2000)
For radio buttons and checkboxes just replace selected="selected" with checked="checked".
For selects, it is a bit more intense you need to loop through the data and check if the value equals the selected value (at least that is the easiest to do). But you could apply a similar technique as seen with the checkbox / radio.
<input type="radio" name="radio1" value="1" <?php echo (!empty($_POST['radio1']))?'checked':''?>>
<input type="checkbox" name="chkbox1" value="1" <?php echo (!empty($_POST['chkbox1']))?'checked':''?>>
Since you are creating the table PHP side here is the code:
$years = array('2000', '2001', '2002');
$table .= "<select name='year' id='year'>";
foreach ($years as $year) {
$table .= "<option id='" . $year . "' value='" . $year . "' " .
(($_POST['year'] == $year)?'selected':'') . ">" . $year . "</option>\n";
}
$table.="</select>";
Should get you on the right track for select statements. The ? and : make up the ternary operator which is a shortened if / else statement.
You can conditionnally echo selected in PHP, depending on the value to select.
<?php
$selected[$_GET['year']] = "selected='selected'";
$table.="
<select name='year' id='year'>
<option id='2000' value='2000' " . $selected['2000'] . ">2000</option>
<option id='2001' value='2001' " . $selected['2001'] . ">2001</option>
<option id='2002' value='2002' " . $selected['2002'] . ">2002</option>
</select>";
echo $table;
?>

Categories