PHP Display label and selected value - php

:::EDIT:::
Ok so apparently I should have put in the larger picture here, and that is my fault. Ok so let's say there are 2 sets of dropdowns,
Consider the following HTML:
<form name="myform" action="process.php" method="POST">
<input type="hidden" name="check_submit" value="1" />
<select name="month">
<option selected="--">--</option>
<option value="January">01</option>
<option value="February">02</option>
<option value="March">03</option>
<option value="April">04</option>
<option value="May">05</option>
<option value="June">06</option>
<option value="July">07</option>
<option value="August">08</option>
<option value="September">09</option>
<option value="October">10</option>
<option value="November">11</option>
<option value="December">12</option>
</select>
<select name="year">
<option selected="--">--</option>
<option value="1965">65</option>
<option value="1966">66</option>
<option value="1967">67</option>
<option value="1968">68</option>
</select>
<input type="submit" />
</form>
I am using $_POST in 'process.php' to display the year selected by the user like this:
<?php
echo "{$_POST['month']} ";
echo "{$_POST['year']} ";
?>
If "67" is selected "1967" displays AND when "09" is selected, "September" is displayed so the question still stands...
Is there a way that I can display the option and the label on the same page? This is simple HTML with a PHP processor, nothing more.
Thanks!

you can't achieve it by php alone you can use jquery with this like follow
<select name="month" onchange="document.getElementById('month_text').value=this.options[this.selectedIndex].text">
<option selected="--">--</option>
<option value="January">01</option>
<option value="February">02</option>
<option value="March">03</option>
............
<option value="December">12</option>
</select>
<select name="year" onchange="document.getElementById('year_text').value=this.options[this.selectedIndex].text">
<option selected="--">--</option>
<option value="1965">65</option>
<option value="1966">66</option>
<option value="1967">67</option>
<option value="1968">68</option>
</select>
<input type="hidden" name="year_text" id="year_text" value="" />
<input type="hidden" name="month_text" id="month_text" value=""/>
in php
echo $_POST['year']; // will print 1967
echo $_POST['year_text']; // will print 67
echo $_POST['month']; // will print January
echo $_POST['month_text']; // will print 01

You can do like this:
Modify html like:
<option value="1965-65">65</option>
In PHP:
$explode = explode("-",$_POST['year']);
echo $explode[0]; //will be 1965
echo $explode[1]; //will be 65
OR
your html
<option value="1965">65</option>
PHP
echo substr($_POST['year'], 2, 2); // 65

<?php
$monthArray = array('01'=>'January','02'=>'February','03'=>'March','04'=>'April',
'05'=>'May','06'=>'June','07'=>'July','08'=>'August',
'09'=>'September','10'=>'October','11'=>'November',
'12'=>'December');
$month = $_POST['month'];
$year = $_POST['year'];
echo $month;
echo $year;
echo "You have selected ".substr($year, 2)." Year.";
echo "You have selected ".$monthArray[$month]." month.";
?>
I have updated the code for month also.

Related

How can I achieve years on drop down menu

Hello everyone one I want to get this kind of drop down menu which has years need to be selected
For example season 2021/2022, 2020/2021, all these value should be generated from 1990 to 2022
If I understood correctly, you want to populate the select with options with all seasons until now since a specific year?
You could modify a for loop so the years appears in a descending order, and then output the option tag with the "previous year / year" as a value like this:
<div class="season-select-wrapper">
<?php
// Define variables for the select
$currentYear = 2022;
$startYear = 1990;
?>
<label for="season">Select a season</label>
<select name="season" id="season">
<?php for ($year = $currentYear; $year > $startYear; $year--) { $prevYear = $year - 1; ?>
<option value="<?php echo "{$prevYear}/{$year}"; ?>"><?php echo "{$prevYear}/{$year}"; ?></option>
<?php } ?>
</select>
</div>
This would create HTML markup like this:
<div class="season-select-wrapper">
<label for="season">Select a season</label>
<select name="season" id="season">
<option value="2021/2022">2021/2022</option>
<option value="2020/2021">2020/2021</option>
<option value="2019/2020">2019/2020</option>
<option value="2018/2019">2018/2019</option>
<option value="2017/2018">2017/2018</option>
<option value="2016/2017">2016/2017</option>
<option value="2015/2016">2015/2016</option>
<option value="2014/2015">2014/2015</option>
<option value="2013/2014">2013/2014</option>
<option value="2012/2013">2012/2013</option>
<option value="2011/2012">2011/2012</option>
<option value="2010/2011">2010/2011</option>
<option value="2009/2010">2009/2010</option>
<option value="2008/2009">2008/2009</option>
<option value="2007/2008">2007/2008</option>
<option value="2006/2007">2006/2007</option>
<option value="2005/2006">2005/2006</option>
<option value="2004/2005">2004/2005</option>
<option value="2003/2004">2003/2004</option>
<option value="2002/2003">2002/2003</option>
<option value="2001/2002">2001/2002</option>
<option value="2000/2001">2000/2001</option>
<option value="1999/2000">1999/2000</option>
<option value="1998/1999">1998/1999</option>
<option value="1997/1998">1997/1998</option>
<option value="1996/1997">1996/1997</option>
<option value="1995/1996">1995/1996</option>
<option value="1994/1995">1994/1995</option>
<option value="1993/1994">1993/1994</option>
<option value="1992/1993">1992/1993</option>
<option value="1991/1992">1991/1992</option>
<option value="1990/1991">1990/1991</option>
</select>
</div>

Get value of drop down menu and save as variable in PHP

I want to get the selected value of a drop down menu and save it as a variable. I tried the following as documented in another post, but it seems not working: echo $selected = $_POST['<?php $as->my_name(); ?>'];
Drop down:
<select name="<?php $as->my_name(); ?>">
<option value="">Select this</option>
<option value="91"<?php $mb->state('91'); ?>>91</option>
<option value="90"<?php $mb->state('90'); ?>>90</option>
<option value="89"<?php $mb->state('89'); ?>>89</option>
<option value="88"<?php $mb->state('88'); ?>>88</option>
<option value="87"<?php $mb->state('87'); ?>>87</option>
<option value="86"<?php $mb->state('86'); ?>>86</option>
<option value="85"<?php $mb->state('85'); ?>>85</option>
<option value="84"<?php $mb->state('84'); ?>>84</option>
<option value="83"<?php $mb->state('83'); ?>>83</option>
<option value="82"<?php $mb->state('82'); ?>>82</option>
</select>
Post the form:
You have to put the select option within form tags
<form method="post">
<select name="example">
<option></option>
</select>
<input type="submit" />
</form>
Get the posted value:
To get the value of this example you can use:
<?php
$selected = $_POST['example'];
echo $selected;
?>
To get the value in your case:
<select name="<?php echo $as->my_name(); ?>">
<option value="test">test</option>
</select>
<?php
$selected = $_POST[$as->my_name()];
echo $selected;
?>

Set a PHP variable as the value of user selected dropdown form

I'm trying to create a form with text fields and dropdowns. With the text fields, I'm using this code:
<label for="name">Name</label>
<input type="text" id="name" name="name" value="<?php echo $user->name;?>" style="width:95%;"/>
The form then checks if the user has filled out the "name" field with this:
$name = $input->get('name', null);
if($name == null)
return false;
This works fine for the multiple text fields that the form uses, but I don't know how to check for the user input on the dropdowns. How can I send the option selected by the user, similar to the way I did it with the text field, so that I can check to make sure the user selected something? Example:
<label for="department">Department</label>
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<option value="1"><?php echo $params->get('department1');?></option>
<option value="2"><?php echo $params->get('department2');?></option>
<option value="3"><?php echo $params->get('department3');?></option>
<option value="4"><?php echo $params->get('department4');?></option>
<option value="5"><?php echo $params->get('department5');?></option>
<option value="6"><?php echo $params->get('department6');?></option>
<option value="7"><?php echo $params->get('department7');?></option>
<option value="8"><?php echo $params->get('department8');?></option>
</select>
$department1 = $input->get('department1', null);
$department2 = $input->get('department2', null);
Etc........
This is set up the exact same way as the text fields as far as I know, but doesn't work and seems like a bad way to do it anyways.
if you want to verify and pre-select one option you should do something like this:
<option value="6" <?php if($params->get('department6')==true){ echo 'selected'; } ?>><?php echo $params->get('department6');?></option>
First of all, instead of having 8 lines like that for your departments, you could use a for loop.
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<?php for($i=1; $i<=8; $i++): ?>
<option value="<?php echo $i; ?>"><?php echo $params->get('department' . $i);?></option>
<?php endfor; ?>
</select>
Now, inside your for loop, you can add a check if the $input->get is true.
<?php if($input->get('department' . $i, null)) echo 'selected'; ?>
So if you mix both together, the result would be
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<?php for($i=1; $i<=8; $i++): ?>
<option value="<?php echo $i; ?>" <?php if($input->get('department' . $i, null)) echo 'selected'; ?>><?php echo $params->get('department' . $i);?></option>
<?php endfor; ?>
</select>
And if you want to a "pre selected" option use "selected" in the option you want:
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<option value="1"><?php echo $params->get('department1');?></option>
<option value="2"><?php echo $params->get('department2');?></option>
<option value="3"><?php echo $params->get('department3');?></option>
<option value="4" selected><?php echo $params->get('department4');?></option>
<option value="5"><?php echo $params->get('department5');?></option>
<option value="6"><?php echo $params->get('department6');?></option>
<option value="7"><?php echo $params->get('department7');?></option>
<option value="8"><?php echo $params->get('department8');?></option>
</select>
As explained by W3CSchools.

array in php function undefined

For training purposes i need to make a function which tells me the 'travel cost' between 2 cities. The book tells me to type this function:
<?php
function travelcost($start, $destination)
{
$travelcost = array();
$travelcost[1] = array();
$travelcost[2] = array();
$travelcost[3] = array();
$travelcost[4] = array();
$travelcost[1][1] = 0;
$travelcost[1][2] = 30;
$travelcost[1][3] = 60;
$travelcost[1][4] = 90;
echo($travelcost[$start][$destination] . " Euro's");
}
?>
In addition i've created this form to ask for a start and a destination:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Start: <select name="start" value="true">
<option value="start[]">Amsterdam</option>
<option value="start[]">Utrecht</option>
<option value="start[]">Den Haag</option>
<option value="start[]">Rotterdam</option>
</select>
Destination: <select name="destination" value="true">
<option value="destination[]">Amsterdam</option>
<option value="destination[]">Utrecht</option>
<option value="destination[]">Den Haag</option>
<option value="destination[]">Rotterdam</option>
</select>
<p><input type="submit" name="calculate" value="Calculate"</p>
</form>
Followed by:
<?php
if(isset($_POST["start"])&& isset($_POST["destination"]))
{
travelcost($_POST['start'], $_POST['destination']);
}
?>
This gives me Undefined index: start[]
I know im doing it wrong, but i just can't see the logic in the function and the array. I assume the function is correct because it's right out of the book but i'm also not sure about that.
Can someone help me out?
This is wrong,
<option value="start[]">Amsterdam</option>
^ ^
It should be
<option value="start">Amsterdam</option>
or
<option value="Amsterdam">Amsterdam</option>
Same for all options in start and destination.
According to your function `travelcost(), your select should be
Start: <select name="start" value="true">
<option value="1">Amsterdam</option>
<option value="1">Utrecht</option>
<option value="1">Den Haag</option>
<option value="1">Rotterdam</option>
</select>
Destination: <select name="destination" value="true">
<option value="1">Amsterdam</option>
<option value="2">Utrecht</option>
<option value="3">Den Haag</option>
<option value="4">Rotterdam</option>
</select>

value="<?php echo htmlspecialchars($_POST['whatever']); ?>" not working in select tag

my code is written below
<select name="year" required value="<?php echo htmlspecialchars($_POST['year']); ?>" >
<option>----SELECT----</option>
<option value="1">1<sup>st</sup></option>
<option value="2">2<sup>nd</sup></option>
<option value="3">3<sup>rd</sup></option>
<option value="4">4<sup>th</sup></option>
</select>
htmlspecialchars() is not working for select tag.is there any mistake or there is some other way to do it
if you want to set the default value of a select you have to set the option that contains this value as selected like this:
<select name="year" required="required">
<option>----SELECT----</option>
<option <?php if((int)($_POST['year'])==1){?>selected<?php } ?> value="1">1<sup>st</sup></option>
<option <?php if((int)($_POST['year'])==2){?>selected<?php } ?> value="2">2<sup>nd</sup></option>
<option <?php if((int)($_POST['year'])==3){?>selected<?php } ?> value="3">3</sup>rd<sup></b></option>
<option <?php if((int)($_POST['year'])==4){?>selected<?php } ?> value="4">4<sup>rth</sup></option>
</select>
You don't assign a value attribute to a select tag; you assign it to option tags. It's not clear what you are trying to do, but <select ... value="..."> is just invalid HTML. See the specification.
you cannnot give value in <select> tag. Value is given in <option> tag.
like
<select name="year">
<option>----SELECT----</option>
<option value="<?php echo htmlspecialchars($_POST['year']); ?>">
<?php echo htmlspecialchars($_POST['year']); ?></option>
<option value="1">1<sup>st</sup></option>
<option value="2">2<sup>nd</sup></option>
<option value="3">3</sup>rd<sup></b></option>
<option value="4">4<sup>rth</sup></option>
</select>

Categories