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;
?>
Related
Here I have two select box,one for month and other for year,for month it is working but for year in my case its coming based on current year dynamically.In the case of year when I select any option and click submit button,the option is changing and going to previous position again.Can anyone help me please. here is the code
HTML & PHP
<form action="" method="post" id="nameform">
<select name="fetchmonth">
<option <?php if ($_POST['fetchmonth'] == 'January') echo 'selected="selected"'; ?> >January</option>
<option <?php if ($_POST['fetchmonth'] == 'February') echo 'selected="selected"'; ?>>February</option>
<option <?php if ($_POST['fetchmonth'] == 'March') echo 'selected="selected"'; ?>>March</option>
</select>
<select class="boottext" name="fetchyear" required id="month" >
<?php
$expenses_year = date('Y');
// populate the select options with years
for ($yr = date("Y"); $yr >= 1970; $yr--) {
if ($yr == $expenses_year) {
echo '<option value="' . $yr . '" selected="selected">' . $yr .
'</option>';
} else {
echo '<option value="' . $yr . '">' . $yr . '</option>';
}
}
?>
</select>
</form>
<p> </p>
<button type="submit" form="nameform" value="Submit">Submit</button>
I have a dropdown I dynamically populated from mysql
When I save the information it to mysql, it do not save the value of the dropdown list.
How do I set the selected value for the dynamic dropdown
Here is the code use to fill the dropdown.
How do I set the selected value on change in php
echo "<option value=\"" . utf8_encode($row['school_name']) . "\">" . utf8_encode($row['school_name']) . "</option>";
im have problems setting the correct format using this example
<option value="My School"<?php if ($row[school_name] == 'My School') echo ' selected="selected"'; ?>>My School</option>
Where My School should be the value populated from database
Ok
<select id="school_name" "name="school_name" style="width:200px;" name="school_name">
<option selected="">Select Province School</option>
</select>
i then fill the dropdownlist with following.
$sql="SELECT * FROM ppSD_schools where province_code ='".$_POST['c_id']."'";
$res = $conn->query($sql);
$num_rows = $res->num_rows;
if($num_rows > 0) {
echo "<option value=''>------- Select --------</option>";
while($row = $res->fetch_assoc())
{
?>
echo "<option value=\"" . utf8_encode($row['school_name']) . "\">" . utf8_encode($row['school_name']) . "</option>";
}
But when ever i select another school. it dont save the value selected to MySql
My guess is it dont mark the value as selected because it get filled dynamic
I now have to change the code so when i select it it must marked is as selected
I would use a repeated function to compare the value against the row value
<?php
function selected($a, $b) {
return ($a === $b) ? ' selected' : '';
}
?>
<option name="school" value="My School" <?= selected($row["school_name"], "My School"); ?>>My School</option>
<option name="school" value="Your School" <?= selected($row["school_name"], "Your School"); ?>>Your School</option>
I write this code for printing the price values up to 10000 using for loop when I submit the form using get method the value of max_price is showing in the URL.
Now I want to select that value which is in URL when user refresh the page I tried to get it but I didn't get success.Any solution for this?
<select name="max_price" id="max_price">
<option value="">Max Price</option>
<?php for($maximum=50;$maximum<=10000;$maximum=$maximum+500){ $price=($maximum)*(1000);?>
<option value="<?php echo $price;?>" <?php if(isset($_GET['max_price'])){ if ($_GET['max_price']==(($price)){?> selected <?php } }?>><?php echo "$".$maximum."K";?> </option> <?php } ?>
</select>
You can try this:
<select name="max_price" id="max_price">
<option value="">Max Price</option>
<?php
for($maximum=50;$maximum<=10000;$maximum=$maximum+500)
{ $price=($maximum)*(1000);
echo "<option value=\"echo $price;\"";
if(isset($_GET['max_price']))
{ if ($_GET['max_price']==$price)
echo " selected ";
}
echo ">$".$maximum."K</option>";
} ?>
</select>
Moreover in your source,
this line : if(isset($_GET['max_price'])) is missing closing ) parenthesis.
First, considering your Question, it is vital that you (at the least) expand your Code for readability. This is because, if your Code had been a little more readable, you would personally solve the issue.... There is nothing wrong with inlining your Code like you did... it is just only a bit of an extra-work to debug it (like in the current scenario).
<?php
$selOptions = "<select name='max_price' id='max_price'>" .PHP_EOL;
$selOptions .= "<option value=''>Max Price</option>" .PHP_EOL;
for($maximum=50; $maximum<=10000; $maximum+=500) {
$price = $maximum * 1000;
$selOptions .= "<option value='{$price}' ";
if (isset($_GET['max_price'])) {
if ($_GET['max_price'] == $price) {
$selOptions .= " selected=selected ";
}
}
$selOptions .= ">\${$maximum}K</option>" . PHP_EOL;
}
$selOptions .= "</select> " . PHP_EOL;
echo $selOptions;
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.
I'm having trouble with my php form that i query the info from the db which works fine now i just want when the user clicks submit to return the the selected value when the form reloads. I been trying to get this to work all night.
Example to be more clear.
--- Select ---
--- Apple ---
--- Banana ---
If they selected banana and clicked sumbit when the page reloads to have the field banana already selected. Here the code below. After 45 mins of fighting the "Parse error: syntax error, unexpected '.' I'm ready to pull my hair out so i hope you guys can lend a hand.
echo '<option value="'. $row['free'] .'" "'. $free==$row['free'] ? .'" selected=':'>$"'.$row['free'].'"</option>';
Thanks
echo '<option value="'. $row['free'] .'" "'. $free==$row['free'] ? /*.*/ '" selected=':'>$"'.$row['free'].'"</option>';
Just commented it out so you can see it.
let's assume you have a select element within a form
<form action="" method="post">
<select name ="fruits">
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="orange">Oranges</option>
<option value="mango">Mangoes</option>
</select>
<input type="submit" name="submit"/>
</form>
if i understand you correct you want to show the selected value back to the user when a user click on the submit button to do that place this in top of your php code.
<?php
//This will check if form is submitted then fetch the value from select element else assign null
$value = isset($_POST['submit']) ? $_POST['fruits'] : NULL;
?>
and change all the <option> to
<option value="apple" <?php echo ($value == 'apple') ? 'selected' : ''; ?>>Apple</option>
<option value="banana" <?php echo ($value == 'banana') ? 'selected' : ''; ?>>Banana</option>
<option value="orange" <?php echo ($value == 'orange') ? 'selected' : ''; ?>>Oranges</option>
<option value="mango" <?php echo ($value == 'mango') ? 'selected' : ''; ?>>Mangoes</option>
echo '<option value="' . $row['free'] . '"' . ($free == $row['free'] ? ' selected="selected"' : '') . '>' . $row['free'] . '</option>';
That should work :)
I'd advice you simplify your sintax and make it readable first of all.
$selected = '';
if($free==$row['free']){ $selected=' selected'; }
echo "<option value='{$row['free']}'{$selected}>{$row['free']}</option>";
Not sure how that string got into such a mess.... it's clearer (in my opionion) if you drop out of PHP mode if you're rendering HTML ... as follows:
?>
<option value="<?php echo $row['free']; ?>" <?php if($free == $row['free']) echo 'selected="selected"'; ?>><?php echo $row['free']; ?></option>
<?php