Select input is empty - php

People can select their graduation year from a selection input. I email their selection to myself. However, based on the fact that nothing is printing to the console, the $_POST["year"] must be empty, which is why no year is actually being sent to me as shown in the pic below. How do I fix this?
<div class = "mailInput"> Year:
<div class = "select" name = "year"><select>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
</div>
</div>
// Assign year input to year variable
if (!empty($_POST["year"])) {
$year = $_POST["year"];
echo("<script>console.log('Year after: ".$year."');</script>");
}

You have written wrong html code,
<div class="mailInput"> Year:
<div ><select class="select" name="year"> // this line
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
</div>
</div>
You have given name to div element which was parent of select,
now above I gave name to select element.
Give it a try. It will work.

<div class = "mailInput"> Year:
<div class = "select" >
//give name for select tag not for div
//
<select name = "year">
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
</div>
</div>
// Assign year input to year variable
if (!empty($_POST["year"])) {
$year = $_POST["year"];
echo("<script>console.log('Year after: ".$year."');</script>");
}

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>

How do I handle two dropdown post values?

I have three dropdown by default. The first dropdown value will affect the visibility of the 2nd and 3rd dropdown. For example, choose option one, hide 3rd dropdown, show 2nd dropdown. 2nd and 3rd dropdown will hold the value of the same column in database.
<select class="ui fluid selection dropdown dd" name="category1" id="category1">
<i class="dropdown icon"></i>
<option value="">Which category does this fall into?</option>
<option value="1">Gestures</option>
<option value="2">Etiquette</option>
</select>
<?php if(isset($_POST['category1'])) { $category1 = $_POST['category1']; }?>
<div class="category2" >
<select class="ui fluid selection dropdown dd" name="category2" id="category2" >
<i class="dropdown icon"></i>
<option value="">Be specific...</option>
<option value="1">Friendly gestures</option>
<option value="2">Gestures of respect</option>
<option value="3">Salutes</option>
<option value="4">Celebratory gestures</option>
<option value="5">Finger-counting</option>
<option value="6">Obscene gestures</option>
<option value="7">Taunts</option>
<option value="8">Head motions</option>
<option value="9">Other gestures</option>
</select>
</div>
<?php if(isset($_POST['category2'])) { $category2 = $_POST['category2']; }?>
<div class="category3" style="display:none!important;">
<select class="ui fluid selection dropdown dd" name="category3" id="category3">
<i class="dropdown icon"></i>
<option value="">Be specific...</option>
<option value="10">Greetings</option>
<option value="11">Making payment</option>
<option value="12">Visiting someone's house</option>
<option value="13">Gifts and gift-giving</option>
<option value="14">Table manners</option>
<option value="15">Eating and drinking</option>
<option value="16">Funerals</option>
<option value="17">Bars and restaurants</option>
<option value="18">Driving</option>
<option value="19">Business etiquette</option>
<option value="20">Hierarchy and honoring the elder</option>
</select>
</div>
<?php if(isset($_POST['category3'])) { $category3 = $_POST['category3']; }?>
Now, $category2 and $category3 represent the same column in database. If I were to make an insert query, how do I make sure either dropdown value is posted and will be stored?
mysqli_query($link,"INSERT INTO practices(category1,category2) VALUES('$category1','$category2 or $category3')")
I tried this, and
<?php if(isset($_POST['category2'])) { $category2 = $_POST['category2']; }?>
<?php if(isset($_POST['category3'])) { $category2 = $_POST['category3']; }?>
This one worked for one of the dropdown only, and error inserting for another. Any help would be appreciated
Save previously posted value in a hidden field and then post again with the second dropdown list.
<div class="category3" style="display:none!important;">
<?php if(isset($_POST['category2']))
{
echo'<select name="pre_category" >
<option selected="selected" value="'.$_POST['category2'].'">'.$_POST['category2'].' </option>
</select>';
} ?>
<select class="ui fluid selection dropdown dd" name="category3" id="category3">
<i class="dropdown icon"></i>
<option value="">Be specific...</option>
<option value="10">Greetings</option>
<option value="11">Making payment</option>
<option value="12">Visiting someone's house</option>
<option value="13">Gifts and gift-giving</option>
<option value="14">Table manners</option>
<option value="15">Eating and drinking</option>
<option value="16">Funerals</option>
<option value="17">Bars and restaurants</option>
<option value="18">Driving</option>
<option value="19">Business etiquette</option>
<option value="20">Hierarchy and honoring the elder</option>
</select>
</div>
Now you can find the value from pre_category

How to disable all option second select box that can be less than to first select box selected option?

I have requirement to get date between particular years so I have put two dropdown “from year” and “to Year” but I want to make validation for that “to dropdown” value must be great than “form dropdown”
Below is my html code
<div class="graph-filter">
<label>From</label>
<select onchange="change_data_year();" id="from_data_year" name="from_data_year" class="graph-select">
<option selected="" value="2016">2016</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
</select>
<label>To</label>
<select id="to_data_year" name="to_data_year" class="graph-select">
<option selected="" value="2016">2016</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
</select>
<input type="submit" value="Submit">
Try like this
function change_data_year(){
$('#to_data_year option').prop('disabled',false);
var from_data_year=$('#from_data_year').val();
var to_data_year=$('#to_data_year').val();
$('#to_data_year option').filter(function() {
return $(this).val() < from_data_year;
}).prop('disabled', true);
}
I hope that is like you need it.

how to create a comma delimited array from an ordered select box in jquery

How can I create an array of items from a generated list using a select box to order the items and deliminate them by a comma.
Example...
I have a list of items generated by the id in the database with a select box associated with the item.
<?php
$sql = mysql_query("SELECT id, title FROM songs WHERE id='$id' ORDER BY title ASC");
while($row = mysql_fetch_array($sql)){
$id = $row['id'];
$title = $row['title'];
$song_chart .= '
<div id="$id">
<select>
<option val="1">1</option>
<option val="2">2</option>
<option val="3">3</option>
<option val="4">4</option>
//etc...
</select>
'.$title.'
</div>';
}
$song_chart .= '<button></button>';
echo $song_chart;
?>
I have multiple items that I would like to select a value for.
If the value is greater than 0 than I want to grab the 'id' from the item the select is associated with and order all of the items by the selection (not the id).
Then I would like to post the information and place it into a comma delimited array to insert into my database.
I currently do not have any code that I have actually tried, I am really not sure how to go about this. Any help would be greatly appreciated. Thanks in advance!
EDIT
The output of the above would look like this...
<div id="1">
<select>
<option val="1">1</option>
<option val="2">2</option>
<option val="3">3</option>
<option val="4">4</option>
//etc...
</select>
one
</div>
<div id="2">
<select>
<option val="1">1</option>
<option val="2">2</option>
<option val="3">3</option>
<option val="4">4</option>
//etc...
</select>
two
</div>
<div id="3">
<select>
<option val="1">1</option>
<option val="2">2</option>
<option val="3">3</option>
<option val="4">4</option>
//etc...
</select>
three
</div>
<div id="4">
<select>
<option val="1">1</option>
<option val="2">2</option>
<option val="3">3</option>
<option val="4">4</option>
//etc...
</select>
four
</div>
<button></button>
Then on submit I would like to reorganize the generated id's accourding to the selected value and place then in an array/string like so...
var arr = '2, 3, 1, 4';
and post them to the database.
OK, in this situation, it's easier to encode the data in the value of the OPTION
<div id="3">
<select>
<option val="1">1</option>
<option val="2">2</option>
<option val="3">3</option>
<option val="4">4</option>
//etc...
</select>
three
</div>
Becomes:
<div id="3">
<select>
<option val="3,1">1</option>
<option val="3,2">2</option>
<option val="3,3">3</option>
<option val="3,4">4</option>
//etc...
</select>
three
</div>
Embed the ID in the data. Comma separated.
I came with a solution with the help from Diodeus (Thanks).
$(document).ready(function(){
$('.selection').value(""); // clears the values
$('selection option').click(function(){ // when option is clicked
s=new Array(); // create array
$("select option:selected").each(function(){
// check to see which values are selected
var v = $(this).val();
if(v != ""){ // if the value is not empty.
s.push(v);
}
});
});
$("#submitList").click(function(){
if(s != ""){
alert(s);//or submit the list
}
});
});
I also changed a few things in the html output.
<div id="3">
<select class='selection'>
<option></option>
<option val="1,id">1</option>
<option val="2,id">2</option>
<option val="3,id">3</option>
<option val="4,id">4</option>
//etc...
</select>
three
</div>

Birthday form using HTML/PHP

I'm making a registration form which will have the user put in his birthday.
The problem is when it comes to February and months who do not end in 31. The user is able to put something like 31 February or 31 April. Is there any practical way to display just 28 when February is selected? (aside from using Ajax)
<html>
<body>
<form action="registration.php" method="post">
<p><u>Select table</u></p>
<p>Select date:</p>
<select name="day">
<?php
for($i=1;$i<=31;$i++)
{
echo '<option value='.$i.'>'.$i.'</option>';
}
<select name="month">
<option value="January">January</option>
<option value="February">February</option>
<option value="Mars">Mars</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<select name="year">
<?php
for($i=2011;$i<=2015;$i++)
{
echo '<option value='.$i.'>'.$i.'</option>';
}
?>
</select>
<br/><br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
Don't do that.
Most people are most comfortable typing their birthday out in full. The <option>/<select> bit is really unnecessary.
If you don't have access to a good date guesser server side (odds are you do), break it into three separate fields, but usually, that's not even necessary.
Just give them a single field that can comfortably fit 10 digits, and if the date is ambiguous when you receive it, ask the user if you guessed right.
http://jqueryui.com/demos/datepicker/
Use this and all your date problems are solved forever.
<html>
<head>
<title>Test</title>
</head>
<body>
Date Of Birth:
<select name="month" onChange="changeDate(this.options[selectedIndex].value);">
<option value="na">Month</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="day" id="day">
<option value="na">Day</option>
</select>
<select name="year" id="year">
<option value="na">Year</option>
</select>
<script language="JavaScript" type="text/javascript">
function changeDate(i){
var e = document.getElementById('day');
while(e.length>0)
e.remove(e.length-1);
var j=-1;
if(i=="na")
k=0;
else if(i==2)
k=28;
else if(i==4||i==6||i==9||i==11)
k=30;
else
k=31;
while(j++<k){
var s=document.createElement('option');
var e=document.getElementById('day');
if(j==0){
s.text="Day";
s.value="na";
try{
e.add(s,null);}
catch(ex){
e.add(s);}}
else{
s.text=j;
s.value=j;
try{
e.add(s,null);}
catch(ex){
e.add(s);}}}}
y = 1993;
while (y-->1940){
var s = document.createElement('option');
var e = document.getElementById('year');
s.text=y;
s.value=y;
try{
e.add(s,null);}
catch(ex){
e.add(s);}}
</script>
</body>
</html>
You could use JavaScript to restrict the dropdown to 28, 30, or 31 days based on the month. This does not require you to use AJAX.

Categories