I try to make my dropdown selected only if error happen, and this is my script
<select name="usertype" id="usertype" class="form-control">
<option value="">Please choose the user right</option>
<option value="admin"<?php if(isset($error)
&& $_POST['usertype'] == 'admin' ? ' selected="selected"' : '');?>>
Admin
</option>
<option value="author"<?php if(isset($error)
&& $_POST['usertype'] == 'author' ? ' selected="selected"' : '');?>>
Author
</option>
<option value="public"<?php if(isset($error)
&& $_POST['usertype'] == 'public' ? ' selected="selected"' : '');?>>
Public
</option>
</select>
can anyone tell me the right way? because it doesn't work.
You're mixing up your ternary, its (condition) ? true : false. Here's a revised one:
<?php $usertype = array('admin', 'author', 'public'); ?>
<select name="usertype" id="usertype" class="form-control">
<option disabled selected>Please choose the user right</option>
<?php foreach($usertype as $val): ?>
<option
value="<?php echo $val; ?>"
<?php echo (isset($error, $_POST['usertype']) && $_POST['usertype'] == $val) ? 'selected="selected"' : ''; ?>
>
<?php echo ucfirst($val); ?>
</option>
<?php endforeach; ?>
</select>
try out this code:
<select name="usertype" id="usertype" class="form-control">
<option value="">Please choose the user right</option>
<option value="admin" <?php echo ((isset($error) && $_POST['usertype'] == 'admin') ? ' selected="selected"' : '');?>>Admin</option>
<option value="author" <?php echo ((isset($error)&& $_POST['usertype'] == 'author') ? ' selected="selected"' : '');?>>Author</option>
<option value="public" <?php echo ((isset($error) && $_POST['usertype'] == 'public') ? ' selected="selected"' : '');?>>Public</option>
</select>
Related
I have a form which a user fills up and hits "Save & Next" which will take them to another page where the user can upload images and hit "Final Submit". They can also go back to the previous page to edit the data.
At that time, all the data he had previously filled up should be displayed on the text box. I have used session variable to store the data and display it.
I am stuck in the drop down box.
<select name="District">
<option value="East">East</option>
<option value="West">West</option>
<option value="North">North</option>
<option value="South">South</option>
</select>
When the user submits, I am storing the selected value in Session $_SESSION['District'] = $_POST['District']; and when the user clicks back to go the previous page, I need to auto select that option value in the drop down.
How can I achieve this?
Using array would make it easier.
<?php
$options = array(
'East', 'West', 'North', 'South',
);
?>
<select name="District">
<?php foreach($options as $option) { ?>
<option value="<?php echo $option; ?>" <?php echo (isset($_SESSION['District']) && $_SESSION['District'] == $option) ? "selected" : "" ?>><?php echo $option; ?></option>
<?php } ?>
</select>
Check value in session and if matches set the attibute selected
<select name="District">
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'East'){ echo 'selected'; }?> value="East">East</option>
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'West'){ echo 'selected'; }?> value="West">West</option>
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'North'){ echo 'selected'; }?> value="North">North</option>
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'South'){ echo 'selected'; }?> value="South">South</option>
</select>
Just check the selected value against the one stored in session.
Your option will look like this:
<option value="East" <?php echo ($_SESSION['District']=="East" ? "selected" : ""; ?>>East</option>
And the right one will be selected
<select name="District">
<option value="East" <?php if($_SESSION['District'] == "East"):?>selected="selected"<?php endif; ?>>East</option>
... Repeat with all options ...
Use below code:
<select name="District">
<option value="East" <?php echo ($_SESSION['District'] == "East") ? "selected" : "" ?>>East</option>
<option value="West" <?php echo ($_SESSION['District'] == "West") ? "selected" : "" ?>>West</option>
<option value="North" <?php echo ($_SESSION['District'] == "North") ? "selected" : "" ?>>North</option>
<option value="South" <?php echo ($_SESSION['District'] == "South") ? "selected" : "" ?>>South</option>
</select>
<option value="East" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'East' ? 'selected="selected"' :'' ;?> >East</option>
<option value="West" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'West' ? 'selected="selected"' :'' ;?>>West</option>
<option value="North" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'North' ? 'selected="selected"' :'' ;?>>North</option>
<option value="South" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'South' ? 'selected="selected"' :'' ;?>>South</option>
i would like this select filter to show up only if Language count is more than 4
Any ideas?
Here is html select filter code:
<p>
Select date:
</p>
<form method="get" action="">
<select id="training_session" name="date"onchange=this.form.submit()>
<option value=""<?php if($_GET['date'] === '') echo 'selected' ?>>[All dates]</option>
<option value="April"<?php if($_GET['date'] === 'April') echo 'selected' ?>>April</option>
<option value="May"<?php if($_GET['date'] === 'May') echo 'selected' ?>>May</option>
<option value="June"<?php if($_GET['date'] === 'June') echo 'selected' ?>>June</option>
<option value="July"<?php if($_GET['date'] === 'July') echo 'selected' ?>>July</option>
<option value="August"<?php if($_GET['date'] === 'August') echo 'selected' ?>>August</option>
<option value="September"<?php if($_GET['date'] === 'September') echo 'selected' ?>>September</option>
<option value="October"<?php if($_GET['date'] === 'October') echo 'selected' ?>>October</option>
<option value="November"<?php if($_GET['date'] === 'November') echo 'selected' ?>>November</option>
</form>
<noscript><input type="hidden" value="filter"></noscript>
Here is PHP code:
if (isset($_GET['date']) && $_GET['date']) {
foreach ($training_sessions as $key => $session) {
if (date('F', strtotime($session['ZCS_b_date'])) !== $_GET['date']) {
unset($training_sessions[$key]);
So how do i make it to show up only if language count is more than 4. I have language select filter which has to stay there all the time, but i want date filter to show up only if language count is more than 4.
This is the language select filter code:
<p>
Select language:
</p>
<form method="get" action="">
<select id="training_session" name="lang" onchange=this.form.submit()>
<option value=""<?php if($_GET['lang'] === '') echo 'selected' ?>>[All languages]</option>
<option value="English" <?php if($_GET['lang'] === 'English') echo 'selected' ?>>English</option>
<option value="Portuguese"<?php if($_GET['lang'] === 'Portuguese') echo 'selected' ?>>Portuguese</option>
<option value="French"<?php if($_GET['lang'] === 'French') echo 'selected' ?>>French</option>
<option value="Italian"<?php if($_GET['lang'] === 'Italian') echo 'selected' ?>>Italian</option>
<option value="Japanese"<?php if($_GET['lang'] === 'Japanese') echo 'selected' ?>>Japanese</option>
</form>
<noscript><input type="hidden" value="filter"></noscript>
This is the php code for language select filter:
if (isset($_GET['lang']) && $_GET['lang']) {
foreach ($training_sessions as $key => $session) {
if ($session['training_language'] !== $_GET['lang']) {
unset($training_sessions[$key]);
So does anyone knows what im trying to achieve here? What i want to do.
Ok here we go..
This should give you the results you want
<html>
<head></head>
<body>
<form id="submitquery" method="get" action="">
<?php
$month=array('[All Dates]','January','February','March','April',
'May','June','July','August',
'September','October','November','December');
$language=array('[All Languages]','English','Portuguese','French','Italian','Japanese');
$changeLanguagejs='changeForm();';
$changeDatejs='this.form.submit();';
writeSelect($language,'Language',$changeLanguagejs);
if (isset($_GET['Language'])){
if($_GET['Language']!='[All Languages]'){
$training_sessions=filterLanguage($training_sessions);}
if (count($training_sessions)>=4){
writeSelect($month,'Date',$changeDatejs);
if (isset($_GET['Date'])){
if($_GET['Date']!='[All Dates]'){
$training_sessions=filterDate($training_sessions);}
showResults($training_sessions);}}
else{showResults($training_sessions);}}
?>
</form>
</body>
</html>
<?php
function writeSelect($values,$name,$changejs){
echo "\n".'<p>Select '.$name.':</p>';
echo "\n".' <select id="training_sessions_'.$name.'" name="'.$name.'" onchange='.$changejs.'>'."\n";
foreach($values as $value){
echo ' <option value="'.$value.'" ';
if (isset($_GET[$name]) && $_GET[$name]==$value){echo 'selected ';}
echo '>'.$value.'</option>'."\n";}
echo ' </select><br/><br/>'."\n";}
function filterLanguage($training_sessions){
foreach($training_sessions as $key => $session){
if($session['training_language']!=$_GET['Language']){
unset($training_sessions[$key]);}}
return $training_sessions;}
function filterDate($training_sessions){
foreach($training_sessions as $key => $session){
if(date('F', strtotime($session['ZCS_b_date']))!=$_GET['Date']){
unset($training_sessions[$key]);}}
return $training_sessions;}
function showResults($training_sessions){
echo "\n".'Showing '.count($training_sessions).' ';
if(count($training_sessions)==1){echo 'Result';}
else{echo 'Results';}
echo'<br/>'."\n\n";
print_r($training_sessions);}
?>
<script>
function changeForm(){
var selectLang = document.getElementById("training_sessions_Language").value;
document.getElementById("submitquery").method="post";
document.getElementById("submitquery").action="?Language="+selectLang;
document.getElementById("submitquery").submit();}
</script>
I'm going to edit out the comments to make it more readable...
if you want to see the comments go back through the edits of this answer
I've used Selected="Selected" on SELECT that is fed by values I have written into the code, my query is how can I use it with a dynamic query from the MySQL db?
This is my written code
<label>Fuel</label>
<select tabindex="1" id="proptenure" name="proptenure">
<option value=""></option>
<option value="1" <?php echo ($searchfuel == '1' ? 'selected' : '')?>>Mains gas</option>
<option value="2" <?php echo ($searchfuel == '2' ? 'selected' : '')?>>Wood or coal fire</option>
<option value="3" <?php echo ($searchfuel == '3' ? 'selected' : '')?>>Oil</option>
<option value="4" <?php echo ($searchfuel == '4' ? 'selected' : '')?>>Electric storage heaters</option>
<option value="5" <?php echo ($searchfuel == '5' ? 'selected' : '')?>>LPG or bottled gas</option>
<option value="6" <?php echo ($searchfuel == '6' ? 'selected' : '')?>>No central heating system</option>
</select>
How can use the above type of line on my Query based SELECT
<?php echo ($searchtenure == '2' ? 'selected' : '')?>
This is an example of how I'm using SELECT from a query
<label>Fuel Type</label>
<?php $fueltype = db::getInstance()->query('SELECT * FROM lkup_fueltype');
if(!$fueltype->count()) {
echo 'Problem';
} else { ?>
<select tabindex="1" id="propertyfueltype" name="propertyfueltype">
<?php foreach ($fueltype->results() as $fueltype) { ?>
<option value="<?php echo $fueltype->PropertyFuelType; ?>"><?php echo $fueltype->PropertyFuelType; ?></option> <?php } } ?>
</select>
So how can I use the Selected="selected"?
This should do the job:
<label>Fuel Type</label>
<?php $fueltype = db::getInstance()->query('SELECT * FROM lkup_fueltype');
if(!$fueltype->count()) {
echo 'Problem';
} else { ?>
<select tabindex="1" id="propertyfueltype" name="propertyfueltype">
<?php foreach ($fueltype->results() as $fueltype) { ?>
<?php if($fueltype->PropertyFuelType == 2){ $selected = ' selected="selected"'; }else{ $selected = NULL; }?>
<option value="<?php echo $fueltype->PropertyFuelType; ?>"<?php echo $selected; ?>><?php echo $fueltype->PropertyFuelType; ?></option> <?php } } ?>
</select>
please, i'm somewhat new with php bear with me.
I have a selectbox...
<select name="criteria">
<option value="1">Student ID</option>
<option value="2">Firstname </option>
<option value="3">Lastname</option>
<option value="6">All</option>
</select>
<input name="searchvalue" type="text">
<input name="search" type="submit" value="Search">
When i search with a particular option, after the post request, the selected option usually defaults to Student ID. But i want it to still remain the option i searched with. Any suggestion would be appreciated.
To be honest I'd adjust your select code just a little bit to allow for a bit more dynamic editing..
<?php
$options = array('1'=>'Student ID', '2'=>'Firstname', '3'=>'Lastname', '6'=>'All');
?>
<select name="criteria">
<?php
foreach($options as $key=>$value){
$addtItem = '';
if(isset($_POST['criteria']) && $_POST['criteria']==$key){
$addtItem = 'selected="selected"';
}
echo('<option '.addtItem.' value="'.key.'">'.$value.'</option>');
}
?>
</select>
This way you can quickly add more values without a ton of repeat code..
<select name="criteria">
<option value="1" <?php if ($_POST["criteria"] == 1) echo "SELECTED"; ?>>Student ID</option>
<option value="2" <?php if ($_POST["criteria"] == 2) echo "SELECTED"; ?>>Firstname </option>
<option value="3" <?php if ($_POST["criteria"] == 3) echo "SELECTED"; ?>>Lastname</option>
<option value="6" <?php if ($_POST["criteria"] == 6) echo "SELECTED"; ?>>All</option>
</select>
Really ugly, but that works.
You need to check the $_POST array and select the corresponding select item
<?php
$criteria = isset($_POST['criteria']) ? $_POST['criteria']: 0;
?>
<select name="criteria">
<option value="1" <?php echo ($criteria == '1') ? 'selected="selected"': ''; ?>>Student ID</option>
<option value="2" <?php echo ($criteria == '2') ? 'selected="selected"': ''; ?>>Firstname </option>
<option value="3" <?php echo ($criteria == '3') ? 'selected="selected"': ''; ?>>Lastname</option>
<option value="6" <?php echo ($criteria == '6') ? 'selected="selected"': ''; ?>>All</option>
</select>
I'm creating a simple registration page:
<?php
$firstName = $_POST['firstName'];
?>
<form action="registration.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>
<b>First name:</b>
</td>
<td>
<input type="text" name="firstName" size="30" maxlength="400" value="<?php echo $firstName; ?>" />
</td>
</tr>
<tr>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
</form>
If the user enters an invalid first name (ex. too short, or weird symbols), the page reloads with an error message shows up to tell them that they have to re-enter their name HOWEVER, the value of the text field is what they most recently entered. This way whenever a user doesn't pass validation, they don't have to re-enter all the fields in the form.
What's a good way to remember what the user entered for a drop down menu? The problem for me is that the option value is different than the text inside the option tag. So when I use the above approach, if I select "Mar" if in invalid submission occurs, '03' appears in the dropdown menu.
<select name="birthdayMonth">
<option value="-1">Month:</option>
<option value="01">Jan</option>
<option value="02">Feb</option>
<option value="03">Mar</option>
<option value="04">Apr</option>
<option value="05">May</option>
<option value="06">Jun</option>
<option value="07">Jul</option>
<option value="08">Aug</option>
<option value="09">Sep</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>
At the top:
$birthdayMonth = $_POST['birthdayMonth']
In the select:
<select name="birthdayMonth">
<option value="-1">Month:</option>
<option value="01"<?php echo $birthdayMonth == '01' ? 'selected="selected"' : ''; ?>>Jan</option>
<option value="02"<?php echo $birthdayMonth == '02' ? 'selected="selected"' : ''; ?>>Feb</option>
<option value="03"<?php echo $birthdayMonth == '03' ? 'selected="selected"' : ''; ?>>Mar</option>
<option value="04"<?php echo $birthdayMonth == '04' ? 'selected="selected"' : ''; ?>>Apr</option>
<option value="05"<?php echo $birthdayMonth == '05' ? 'selected="selected"' : ''; ?>>May</option>
<option value="06"<?php echo $birthdayMonth == '06' ? 'selected="selected"' : ''; ?>>Jun</option>
<option value="07"<?php echo $birthdayMonth == '07' ? 'selected="selected"' : ''; ?>>Jul</option>
<option value="08"<?php echo $birthdayMonth == '08' ? 'selected="selected"' : ''; ?>>Aug</option>
<option value="09"<?php echo $birthdayMonth == '09' ? 'selected="selected"' : ''; ?>>Sep</option>
<option value="10"<?php echo $birthdayMonth == '10' ? 'selected="selected"' : ''; ?>>Oct</option>
<option value="11"<?php echo $birthdayMonth == '11' ? 'selected="selected"' : ''; ?>>Nov</option>
<option value="12"<?php echo $birthdayMonth == '12' ? 'selected="selected"' : ''; ?>>Dec</option>
</select>
This is a really dirty of what doing what you want, but it will work:
http://www.plus2net.com/php_tutorial/pb-drop.php
It would be better to use loop to build the dropdown and then add the selected="selected" value to the correct option.
<option value="03" <?php echo $_POST['birthdayMonth'] == '03' ?
'selected="selected"' : '' ?>>Mar</option>