I have simple code
File name:newEmptyPHP.php
<form method="post" action="newEmptyPHP.php">Title:
<select name="title">
<option value="select">Select</option>;
<option value="Dr" selected=<?php if(isset($_POST[ 'title'])=="Dr" ){ echo "selected"; } ?>>Dr</option>';
<option value="Prof">Prof</option>';
<option value="Mr">Mr</option>';
<option value="Ms">Ms</option>';
<option value="Miss">Miss</option>';
<option value="Mrs">Mrs</option>';</select>
<input type="submit" value="submit">
</form>
I want to keep selected particular value which was selected before the form submission.
But i did not get the desirable output.
any help appreciated.
change this
<option value="Dr" selected=<?php if(isset($_POST['title']) == "Dr")
{ echo "selected"; } ?> >Dr</option>';
to
<option value="Dr" <?php if(isset($_POST['title']) && $_POST['title'] == "Dr")
{ echo "selected='selected'"; } ?> >Dr</option>';
<?php if(isset($_POST['title']) == "Dr"){ echo "selected"; } ?>
Change to
<?php if(isset($_POST['title']) && $_POST['title'] == "Dr"){ echo "selected"; } ?>
But better to use JS / jQuery. Example for jQuery:
<?php if(isset($_POST['title']) && $_POST['title'] == "Dr"):?>
<script type="text/javascript">
$(function(){
$('select[name=title]').val('<?php echo html_special_chars($_POST[''title'])?>');
});
</script>
<?php endif?>
<?php
if($_POST['title'] == "Dr")
{ ?>
<option value="Dr" selected>Dr</option>
<?php
}
else
{
?>
<?php
<option value="Dr">Dr</option>
<?php
}
?>
Change
<option value="Dr" selected=<?php if(isset($_POST['title']) == "Dr"){ echo "selected"; } ?> >Dr</option>';
To :
<option value="Dr" <?php if(isset($_POST['title']) && $_POST['title'] == "Dr"){ echo 'selected="selected"'; } ?> >Dr</option>';
1.Remove isset where you are checking it for value, isset will return either true or false
2.Make selected="selected inside if condition
There is a mistake in your code. isset(Something) will return either true or false and will not return Dr you are looking for.
Retry with this
<form method="post" action="newEmptyPHP.php">
Title:<select name="title" >
<option value="" >Select</option>
<option value="Dr" selected=<?php if(isset($_POST['title']) && $_POST['title'] == "Dr"){ echo "selected"; } ?> >Dr</option>';
<option value="Prof" <?php if(isset($_POST['title']) && $_POST['title'] == "Prof"){ echo "selected"; } ?>>Prof</option>';
<option value="Mr" <?php if(isset($_POST['title']) && $_POST['title'] == "Mr"){ echo "selected"; } ?>>Mr</option>';
<option value="Ms" <?php if(isset($_POST['title']) && $_POST['title'] == "Ms"){ echo "selected"; } ?>>Ms</option>';
<option value="Miss" <?php if(isset($_POST['title']) && $_POST['title'] == "Miss"){ echo "selected"; } ?>>Miss</option>';
<option value="Mrs" <?php if(isset($_POST['title']) && $_POST['title'] == "Mrs"){ echo "selected"; } ?>>Mrs</option>';
</select>
<input type="submit" value="submit">
I thin you have static option values.If so the you have to check for every option like as below for one.
<form method="post" action="newEmptyPHP.php">
Title:<select name="title" >
<option value="select" >Select</option>;
<?php
$selected = "";
if(isset($_POST['title']) && $_POST['title'] == "Dr")
{
$selected = 'selected="selected"';
}
?>
<option value="Dr" <?php echo $selected; ?>>Dr</option>
<option value="Prof">Prof</option>
<option value="Mr">Mr</option>
<option value="Ms">Ms</option>
<option value="Miss">Miss</option>
<option value="Mrs">Mrs</option>
</select>
<input type="submit" value="submit">
</form>
I would rather prefer this code for the purpose
$option_array = array('select'=>'select','mr'=>'mr'... other values);
<?php
$option_string = '';
foreach($option_array as $key=>$value)
{
if($key == $_POST['title'])
{
$selected = 'selected';
}
else
{
$selected = '';
}
$option_string .= "<option value='$key' $selected>$value</option>";
}
?>
<form method="post" action="newEmptyPHP.php">Title:
<select name="title">
<?php echo $option_string; ?>
</select>
<input type="submit" value="submit">
Related
How do i keep my select item selected via PHP? i did it the same way with radiobuttons and it worked. the only thing i changed was changed "checked" to "selected" as was recommended to me.
Here is my PHP:
if (empty($_POST["favFruit"])) {
$favFruitErr = "You must select 1 or more";
}
else {
$favFruit = $_POST["favFruit"];
}
And here is my form:
<select class=favfruitwindow name="favFruit[]" size="4" multiple>
<option <?php if (isset($favFruit) && $favFruit == "apple") echo "selected"; ?> value="apple">Apple</option>
<option <?php if (isset($favFruit) && $favFruit == "banana") echo "selected"; ?> value="banana">Banana</option>
<option <?php if (isset($favFruit) && $favFruit == "plum") echo "selected"; ?> value="plum">Plum</option>
<option <?php if (isset($favFruit) && $favFruit == "pomegranate") echo "selected"; ?> value="pomegranate">Pomegranate</option>
<option <?php if (isset($favFruit) && $favFruit == "strawberry") echo "selected"; ?> value="strawberry">Strawberry</option>
<option <?php if (isset($favFruit) && $favFruit == "watermelon") echo "selected"; ?> value="watermelon">Watermelon</option>
</select>
$favFruit will be an array even when selecting a single option, and null when nothing is selected.
print_r($_POST["favFruit"]); will return Array ( [0] => banana [1] => plum )
You need for see if the item exists in the array and select based on that check.
<select class=favfruitwindow name="favFruit[]" size="4" multiple>
<option <?php if (isset($favFruit) && in_array('apple', $favFruit)) echo "selected"; ?> value="apple">Apple</option>
<option <?php if (isset($favFruit) && in_array('banana', $favFruit)) echo "selected"; ?> value="banana">Banana</option>
<option <?php if (isset($favFruit) && in_array('plum', $favFruit)) echo "selected"; ?> value="plum">Plum</option>
<option <?php if (isset($favFruit) && in_array('pomegranate', $favFruit)) echo "selected"; ?> value="pomegranate">Pomegranate</option>
<option <?php if (isset($favFruit) && in_array('strawberry', $favFruit)) echo "selected"; ?> value="strawberry">Strawberry</option>
<option <?php if (isset($favFruit) && in_array('watermelon', $favFruit)) echo "selected"; ?> value="watermelon">Watermelon</option>
</select>
Iam using PHP codeigniter framework. Value is set to select dropdown as below
<select class="form-control";>
<option <?php if($d1 == 'native') { echo 'selected'; }?> value='native' >native</option>
<option <?php if($d1 == 'migrated') { echo 'selected'; }?> value='migrated' >migrated</option>
</select>
for each row in table there is select option like above. I want to highlight option with color if option is set to migrated on page load.
I tried like below
<select class="form-control";>
<option <?php if($d1 == 'native') { echo 'selected'; }?> value='native' >native</option>
<option <?php if($d1 == 'migrated') { echo 'selected'; echo 'style="<color:red>"' }?> value='migrated' >migrated</option>
</select>
Your are not using proper syntax for style; you are missing a semicolon; and selected and style will be together making it useless. Try:
<select class="form-control">
<option <?php if($d1 == 'native') { echo 'selected'; }?> value='native' >native</option>
<option <?php if($d1 == 'migrated') { echo 'selected style="background-color:red"' }?> value='migrated' >migrated</option>
</select>
Although to me it is easier to read inline conditions as the following:
<select class="form-control">
<option <?php echo $d1 == 'native' ? 'selected' : ''; }?> value='native' >native</option>
<option <?php echo $d1 == 'migrated' ? 'selected style="background-color:red"' : ''; }?> value='migrated' >migrated</option>
</select>
your styling is wrong
<select class="form-control";>
<option <?php if($d1 == 'native') { echo 'selected'; }?> value='native' >native</option>
<option <?php if($d1 == 'migrated') { echo 'selected'; echo 'style="color:red"'; }?> value='migrated' >migrated</option>
</select>
you don't need the brackets <> inside the inline style and don't forget semicolon ;
Here is the code that i am using for drop down to select.
<select name="formreg" value="">
<option value="Registered" <?php if($f_reg == 'Registered') { ?> selected <?php echo $f_reg; } ?> >Registered</option>
<option value="NonReg" <?php if($f_reg == 'NonReg') { ?> selected <?php echo $f_reg; } ?> >NonReg</option>
</select>
<select name="regform">
<option value="Registered" <?php if (isset($register) && $register=='Registered') {?> selected='selected' <?php echo $register; }?>>Registered</option>
<option value="NonReg" <?php if (isset($nonreg) && $nonreg == 'NonReg') {?> selected='selected' <?php echo $nonreg;} ?>>NonReg</option>
</select>
At the top of script:
$f_reg = $_POST["f_reg"]; // or $f_reg = $_GET["f_reg"] if you pass parameters as the url part
then your < select > tag:
<select name="formreg">
<option value="Registered" <?php if ($f_reg == "Registered") print "selected"?>>Registered</option>
<option value="NonReg" <?php if ($f_reg == "NonReg") print "selected"?>>NonReg</option>
</select>
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
Once submitted selected option, the data is not stored.
just want to know how to post back the data if validation fails
The following line doesnt really work for me.
<select id="numbers" name="numbers" value="<?php echo (isset($_POST['numbers'])) ? $_POST['numbers'] : " "; ?>"/>
if someone could give me a hand?
Many thanks, here is my code
<?php
if(isset($_POST['numbers']) &&($_POST['fruits']) && $_POST['numbers'] != "null" && $_POST['fruits'] !== "null")
{
echo "Thank you!";
} elseif (isset($_POST['numbers']) && $_POST['numbers'] = "null") {
echo "you forgot to choose a number";
}
elseif(isset($_POST['fruits']) && $_POST['fruits'] = "null")
{
echo "you forgot to choose fruit name";
}
?>
<form id="form" name="form" method="post" action="">
<label for="expiry">Select</label>
<select id="numbers" name="numbers" value="<?php echo (isset($_POST['numbers'])) ? $_POST['numbers'] : " "; ?>"/>
<option value="null" selected="selected">-</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
</select>
<select id="fruits" name="fruits" value="<?php echo (isset($_POST['fruits']))? $_POST['fruits'] : ''; ?>"/>
<option value="null" selected="selected">-</option>
<option value="Apple">Apple</option>
<option value="Banana">Banana</option>
<option value="Pear">Pear</option>
</select>
<input type="submit" value="Send" />
</form>
Solved it, Maybe not a best way, but at least got it sorted:
<?php
$item = null; #
$itemyear = null;
if(isset($_POST['numbers'])){
$item = $_POST['numbers'];
}
if(isset($_POST['fruits'])){
$itemyear = $_POST['fruits'];
}
if(isset($item) && isset($itemyear) && $item != "null" && $itemyear !== "null")
{
echo "Thank you!";
} elseif ($item == "null") {
echo "you forgot to choose a number";
}
elseif($itemyear == "null")
{
echo "you forgot to choose fruit name";
}
?>
<form id="form" name="form" method="post" action="">
<label for="expiry">Select</label>
<select id="numbers" name="numbers" />
<option value="null" selected="selected">-</option>
<option value="01" <?php if($item == '01'): echo "selected='selected'"; endif; ?>>01</option>
<option value="02" <?php if($item == '02'): echo "selected='selected'"; endif; ?>>02</option>
<option value="03" <?php if($item == '03'): echo "selected='selected'"; endif; ?>>03</option>
</select>
<select id="fruits" name="fruits" />
<option value="null" selected="selected">-</option>
<option value="Apple"<?php if($itemyear == 'Apple'): echo "selected='selected'"; endif; ?> >Apple</option>
<option value="Banana"<?php if($itemyear == 'Banana'): echo "selected='selected'"; endif; ?>>Banana</option>
<option value="Pear"<?php if($itemyear == 'Pear'): echo "selected='selected'"; endif; ?>>Pear</option>
</select>
<input type="submit" value="Send" />
</form>
<?php
echo $item ."-". $itemyear;
?>
the PHP isset() function returns either true or false (depending on whether the input is.. well... set.
You would want to use this:
value='<?php echo (isset($_POST['numbers'])) ? $_POST['numbers'] : ""; ?>
You can't set a value attribute on a <select>. You have to find the correct <option> and set its selected attribute. Personally, I put a data-default attribute on the <select> and then use JavaScript to loop through the options and find the right one.
Oh now I see.
I don't know what the usual thing to do is but one way is to put all the data as a query string when you redirect the user back. The reason why it doesn't stay in the $_POST global is because it's only kept on the page you post to then it's gone.
So when you redirect the user back
if(validationFailed)
{
header("Location: page.php?data=example");
}
The data can then be retrieved in page.php by using
$data = $_GET['data']; // contains "example"