How can I achieve years on drop down menu - php

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>

Related

how to show gender using simple html dom parser in php?

i'm unable for success in how to show gender and state from below coading
please solve this.
$gender = $html->getElementById("cpBody_rbtnListGender")->getAttribute("value");
$state = $html->getElementById("cpBody_ddlState")->getAttribute("value");
this is for gender
<span id="cpBody_rbtnListGender" class="form-control pull-left radio-input"><input id="cpBody_rbtnListGender_0" type="radio" name="ctl00$cpBody$rbtnListGender" value="MALE" checked="checked" tabindex="3"><label for="cpBody_rbtnListGender_0">Male</label><input id="cpBody_rbtnListGender_1" type="radio" name="ctl00$cpBody$rbtnListGender" value="FEMALE" tabindex="3"><label for="cpBody_rbtnListGender_1">Female</label><input id="cpBody_rbtnListGender_2" type="radio" name="ctl00$cpBody$rbtnListGender" value="TRANSGENDER" tabindex="3"><label for="cpBody_rbtnListGender_2">Transgender</label><input id="cpBody_rbtnListGender_3" type="radio" name="ctl00$cpBody$rbtnListGender" value="OTHER" tabindex="3"><label for="cpBody_rbtnListGender_3">Other</label></span>
and this is for state
<div class="input-group">
<label class="control-label label-fixed">State</label>
<select name="ctl00$cpBody$ddlState" id="cpBody_ddlState" tabindex="15" class="form-control">
<option value="0">--SELECT STATE--</option>
<option value="1">ANDAMAN & NICOBAR ISLANDS</option>
<option value="2">ANDHRA PRADESH</option>
<option value="3">ARUNACHAL PRADESH</option>
<option value="4">ASSAM</option>
<option value="5">BIHAR</option>
<option value="6">CHANDIGARH</option>
<option value="7">CHHATTISGARH</option>
<option value="8">DADRA & NAGAR HAVELI</option>
<option value="9">DAMAN & DIU</option>
<option value="10">DELHI</option>
<option value="11">GOA</option>
<option value="12">GUJARAT</option>
<option value="13">HARYANA</option>
<option value="14">HIMACHAL PRADESH</option>
<option value="15">JAMMU AND KASHMIR</option>
<option value="16">JHARKHAND</option>
<option value="17">KARNATAKA</option>
<option value="18">KERALA</option>
<option value="19">LAKSHADWEEP</option>
<option value="20">MADHYA PRADESH</option>
<option value="21">MAHARASHTRA</option>
<option value="22">MANIPUR</option>
<option value="23">MEGHALAYA</option>
<option value="24">MIZORAM</option>
<option value="25">NAGALAND</option>
<option value="26">ORISSA</option>
<option value="27">PONDICHERRY</option>
<option value="28">PUNJAB</option>
<option selected="selected" value="29">RAJASTHAN</option>
<option value="30">SIKKIM</option>
<option value="31">TAMIL NADU</option>
<option value="32">TELANGANA</option>
<option value="33">TRIPURA</option>
<option value="34">UTTAR PRADESH</option>
<option value="35">UTTARAKHAND</option>
<option value="36">WEST BENGAL</option>
</select>
</div>
state and gender is not showing using getAttribute("value");
You could make use of find.
One option to get the state could be to use the id and get the selected option #cpBody_ddlState option[selected]
$state = $html->find('#cpBody_ddlState option[selected]', 0)->plaintext;
echo $state; //RAJASTHAN
To get the selected radio button you might use #cpBody_rbtnListGender input and loop though the items until the attribute checked matches:
foreach ($html->find('#cpBody_rbtnListGender input') as $input) {
if ($input->hasAttribute("checked")) {
echo $input->getAttribute("value"); // MALE
}
}
idk why you're doing this with php.'
in javascript u can create a p to pass the value to it:
<body onload="pageloaded()">
<p id="display"></p>
in javascript:
var p = document.getElementById("display");
var value = document.getElementById("cpBody_rbtnListGender").value;
function pageloaded(){
p.innerHTML = value;
}
u can do the same for ur other needs

Dropdown List in an echo format (PHP) [duplicate]

This question already has answers here:
How to echo in PHP, HTML tags
(11 answers)
Closed 5 years ago.
I would like to place a dropdown list inside an echo as i am using it to update data. Here are the codes for php
echo "Staff Name: " . "<input type = 'text' style = 'width: 200px' name ='name' value = '$staff_name' required>";
For HTML, i know that i need to do it this way, but i do not know how to apply to php(echo)
<select name="sbranch_no">
<option value="Intelligence">Intelligence</option>
<option value="Investigation">Investigation</option>
<option value="Manpower">Manpower</option>
<option value="Operations">Operations</option>
<option value="Police National Service">Police National Service</option>
<option value="Planning and Organization Development">Planning and Organization Development</option>
<option value="Service Quality">Service Quality</option>
<option value="Support and Technical">Support and Technical</option>
<option value="Training">Training</option>
</select>
is this what you mean?
<?php
echo '<select name="sbranch_no">
<option value="Intelligence">Intelligence</option>
<option value="Investigation">Investigation</option>
<option value="Manpower">Manpower</option>
<option value="Operations">Operations</option>
<option value="Police National Service">Police National Service</option>
<option value="Planning and Organization Development">Planning and Organization Development</option>
<option value="Service Quality">Service Quality</option>
<option value="Support and Technical">Support and Technical</option>
<option value="Training">Training</option>
</select>';
?>
Or perhaps
<?php
$menu='<select name="sbranch_no">
<option value="Intelligence">Intelligence</option>
<option value="Investigation">Investigation</option>
<option value="Manpower">Manpower</option>
<option value="Operations">Operations</option>
<option value="Police National Service">Police National Service</option>
<option value="Planning and Organization Development">Planning and Organization Development</option>
<option value="Service Quality">Service Quality</option>
<option value="Support and Technical">Support and Technical</option>
<option value="Training">Training</option>
</select>';
echo "some stuff... $menu ...more stuff";
?>

How to use index of loop in Post method PHP

I need your help! I am trying to save a variable in sql table using Php but I have problem. There are two questions in php, the first concern the continent and the second is depended from the continent. I want to use a loop to check which of the continents has been selected in the first question and then save the value of the second question. I hide the option of unchecked continent using some javascript code (I don't have problem).
The HTML code:
<form method="post" action="">
<fieldset><legend>Continents</legend>
<select id="q1" name="q1">
<option value="1">Africa</option>
<option value="2">Asia</option>
<option value="3">Australia</option>
<option value="4">America</option>
<option value="5">Europe</option>
</select>
<select id="q2" name="Africa">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Asia">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Australia">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="America">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Europe">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
</fieldset>
The Php code
$q1 = $_POST['q1'];
$continents = array("Africa","Asia", "Australia","America","Europe");
for ($i = 1; $i <= 5; $i++) {
if($q1 == $i) {
$q2 = $_POST[$continents[$i-1]]
}
}
Your array should be from 1 to 5 instead of 0 to 4 as you have values 1 to 5 in q1.
Alternatively, I suggest that change your HTML structure to get the continent value in a single line without using loop. You need to change the values of continent like,
<select id="q1" name="q1">
<option value="Africa">Africa</option>
<option value="Asia">Asia</option>
<option value="Australia">Australia</option>
<option value="America">America</option>
<option value="Europe">Europe</option>
</select>
And to get the value of selected continent use $_POST[$_POST['q1']]. For egs, $_POST['q1']=Asia, then $_POST['Asia'] will return the Asia's choice,
$q2= $_POST[$_POST['q1']];
change
$continents =
array(1 => "Africa",
2 => "Asia",
3 => "Australia",
4 => "America",
5 => "Europe");
or
if(($q1-1) == $i) {
$q2 = $_POST[$continents[$i]]
}
First, you may change your select continent HTML:
<select id="q1" name="q1">
<option value="Africa">Africa</option>
<option value="Asia">Asia</option>
<option value="Australia">Australia</option>
<option value="America">America</option>
<option value="Europe">Europe</option>
</select>
Then you could loop over your continents and get the answer:
$q1 = $_POST['q1'];
$continents = array("Africa","Asia", "Australia","America","Europe");
foreach($continents as $continent) {
if ($_POST['q1'] == $continent) {
$q2 = $_POST[$continent];
}
}
Now you have your answer to the second question in $q2.
How about this?
// Always try to seperate logic from your view
// Intialize data
$continents = array("Africa", "Asia", "Australia", "America", "Europe");
// Check for $_POST
if(isset($_POST["q1"])) {
foreach($continents as $id => $continent) {
if($_POST["q1"] == $id) {
// Do something special here
}
}
}
// Render HTML
<form method="post" action="">
<fieldset>
<legend>Continents</legend>
<select id="q1" name="q1">
<!-- Notice that I echo only variables not all of the html -->
<?php foreach($continents as $id => $continent) { ?>
<option value="<?php echo $id; ?>"><?php echo $continent; ?></option>
<?php } ?>
</select>
<select id="q2" name="Africa">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Asia">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Australia">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="America">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Europe">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
</fieldset>
Beautiful thing about my solution is that now you can create whatever array you want and your code will always work.
You can for example SELECT data from database and store them in $continents variable.
$query = "SELECT id, name FROM continents";
$result = mysql_query($query);
$continents = array();
while($row = mysql_fetch_assoc($result)) {
$continents[$row["id"]] = $row["name"];
}
Cool right? :)
I found the solution finally!!!
Replace
$q2 = $_POST[$continents[$i-1]]
with
$q2 = $_POST["{$continents[$i-1]}"];
That solution I wanted!!

HTML <select> default option

I know this question has been asked before, but I have an unaddressed concern about it.
How can you have a default value that is generated by serverside script?
Ex:
Weekday: <select name="weekday" class="right" value="<% =rs("Weekday")%>">
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
<option value="Saturday">Saturday</option>
<option value="Sunday">Sunday</option>
</select>
I know you can have the selected attribute with regular elements, but I'm thrown for a loop here.
Any ideas?
You want your HTML to look something like this eventually - notice the selected after "Friday" to select Friday by default:
<select name="weekday" class="right">
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday" selected>Friday</option>
<option value="Saturday">Saturday</option>
<option value="Sunday">Sunday</option>
</select>
A quick and dirty PHP example can be used like so, where each option is asked to verify whether it is the correct option for the current date. If it is, show selected otherwise don't show anything. You can try this code on http://phpfiddle.org/lite.
<html>
<body>
<select name="weekday" class="right">
<option value="Monday" <?php echo setSelected('Monday'); ?>>Monday</option>
<option value="Tuesday" <?php echo setSelected('Tuesday'); ?>>Tuesday</option>
<option value="Wednesday" <?php echo setSelected('Wednesday'); ?>>Wednesday</option>
<option value="Thursday" <?php echo setSelected('Thursday'); ?>>Thursday</option>
<option value="Friday" <?php echo setSelected('Friday'); ?>>Friday</option>
<option value="Saturday" <?php echo setSelected('Saturday'); ?>>Saturday</option>
<option value="Sunday" <?php echo setSelected('Sunday'); ?>>Sunday</option>
</select>
</body>
</html>
<?php
function setSelected($day)
{
date_default_timezone_set('America/Chicago');
return strtolower($day) === strtolower(date('l')) ? ' selected' : '';
}
?>
I'd be more inclined to create the select/option on the server side itself. Again, quick and dirty PHP example:
<html>
<body>
<select name="weekday" class="right">
<?php
date_default_timezone_set('America/Chicago');
$dayOfWeek = strtolower(date('l'));
$days = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
foreach ($days as $day)
{
echo sprintf("<option value=\"%s\" %s>%s</option>\n",
$day,
$dayOfWeek == strtolower($day) ? 'selected' : '',
$day
);
}
?>
</select>
</body>
</html>

PHP Display label and selected value

:::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.

Categories