I am trying to create two drop downs with HTML and PHP. The first is an auto submit that sends a month to a session variable:
<form action="" method="post">
<select name="month" onchange="this.form.submit()">
<option value="">-- Select Month --</option>
<?php $month = date('n'); // current month
for ($x = 0; $x < 12; $x++) { ?>
<option value="<?php echo date('m', mktime(0,0,0,$month + $x,1)); ?>"
<?php echo date('m', mktime(0,0,0,$month + $x,1)) == $_SESSION['month'] ? "selected='selected'":""; ?> >
<?php echo date('F Y', mktime(0,0,0,$month + $x,1)); ?>
<?php } ?>
</select>
</form>
This works great. But I then need a second drop down with the day name and number:
<form action="" method="post">
<select name="day" onchange="this.form.submit()">
<option value="">-- Select Day --</option>
<?php for ($i = $current_day; $i < 31; $i++) {
$tmp_date = $_SESSION['year']."/".$_SESSION['month']."/".$i;
$weekday = date('D', strtotime($tmp_date)); { ?>
<option value="<?php echo $i; ?>" <?php echo $i == $_SESSION['day'] ? "selected='selected'":""; ?> >
<?php echo $weekday." ".$i; ?>
<?php } ?>
<?php } ?>
</option>
<?php } ?>
</select>
</form>
This uses a temp date with a session variable as '2014' which I set before. I need to remove the session year variable and get the first drop down to know which year the month selected is and then pass this to the temp date so that it populates the day name and number correctly for the next year (2015) if you choose January onwards. At the moment it only shows the day name and number from the current year (2014) in this case.
<?php
if(isset($_POST)) {
$date = explode('-', $_POST['month']);
$year = $date[0];
$month = $date[1];
echo "Year: ".$year." Month: ".$month;
}
?>
<form action="" method="post">
<select name="month" onchange="this.form.submit()">
<?php
for ($i = 0; $i <= 12; ++$i) {
$time = strtotime(sprintf('+%d months', $i));
$value = date('Y-m', $time);
$label = date('F Y', $time);
printf('<option value="%s">%s</option>', $value, $label);
}
?>
</select>
This gives me the year and month as separate values.
I had to check the print out of the first script, because it didn't make much sense...
<form action="" method="post">
<select name="month" onchange="this.form.submit()">
<option value="">-- Select Month --</option>
<option value="11"
>
November 2014 <option value="12"
>
December 2014 <option value="01"
>
January 2015 <option value="02"
>
February 2015 <option value="03"
>
March 2015 <option value="04"
>
April 2015 <option value="05"
>
May 2015 <option value="06"
>
June 2015 <option value="07"
>
July 2015 <option value="08"
>
August 2015 <option value="09"
>
September 2015 <option value="10"
>
October 2015 </select>
</form>
Might be I'm missing something, because it seemed to work, but it doesn't seem like you're closing the option tags. You are doing this:
<option value="11">November 2014
<option value="12">December 2014
Instead of this:
<option value="11">November 2014</option>
<option value="12">November 2014</option>
This doesn't seem to be the issue though, at least not in chrome, because when I checked the head data being sent, it was just fine:
month: 11
What happens when you send in the variable is that whatever page you send this form data with the method POST against will have to handle it further on. In PHP you do that with the global variable $_POST, in this case $_POST['month'].
if(!empty($_POST['month']))
{
echo $_POST['month'];
}
If you want to make it a session variable, the page you send the form data to will have to communicate with your browser to set this (through the reply HTTP header field). To do anything related to session in PHP you must first start a session using the session_start() before any HTML/data is sent against the browser, and the same goes for if you want to set a session variable using for example session_set_cookie_params. You also got setcookie, which also must be used before any data/HTML is sent to the browser and the variables can be accessed through $_COOKIE.
See if you can make some sense out of this, I've just used $_POST in this case, you can swap it with session or cookie and remember to send it before any data/html to the browser.
<?php
$selmonth = 0;
if(!empty($_POST['month']))
{
$selmonth = $_POST['month'];
}
$month = date('n'); // current month
echo '<form action="" method="post">
<select name="month" onchange="this.form.submit()">
<option value="">-- Select Month --</option>';
for($i=0; $i<12; $i++)
{
$monthtime = mktime(0,0,0,$month + $i,1);
$monthnum = date('m', $monthtime);
echo '
<option value="'.$monthnum.'"'.
($monthnum == $selmonth ? ' selected="selected"' : '').
'>'.date('F Y', $monthtime).'</option>';
}
echo '
</select>
</form>';
?>
<select required="" class="btn btn-warning btn-raised" onchange="loadMonthYear(this)">
<?php
$fdt = date('Y-m-1');
$tdt = date('Y-m-1', strtotime("-12 months"));
while (date('Ymd', strtotime($fdt)) >= date('Ymd', strtotime($tdt))) {
echo '<option value="'.date('M, Y', strtotime($fdt)).'"><a data-tag="' . date('M, Y', strtotime($fdt)) . '" href="javascript: void(0);">' . date('M, Y', strtotime($fdt)) . '</option>';
$fdt = date('Y-m-1', strtotime("-1 months", strtotime($fdt)));
}
?>
</select>
Related
We all know that <input type="date"> is not ideal for forms that require a future date to be selected.
My idea was to create a drop-down that only lists off the current date and every date for the rest of the year.
<select>
<option value="Not Selected" disabled selected>Please select a date.</option>
<optgroup label="September">
<option value="1st">Sunday 1st</option>
<option value="etc">Rest of month</option>
<optgroup label="October">
<option value="1st">Tuesday 1st</option>
<option value="etc">Rest of month</option>
<optgroup label="November">
<option value="1st">Friday 1st</option>
<option value="etc">Rest of month</option>
<optgroup label="December">
<option value="1st">Sunday 1st</option>
<option value="etc">Rest of month</option>
</select>
This would allow me to have more control over the user input without needing to validate for a future date. Of course I could use something like php echo date for the current one but the requirements would be;
1) Get current month and add to <optgroup label="[MONTH]">
2) Get current date
3) Calculate dates remaining and then echo them into their own 'DD/MM/YYYY`
I don't want to have to remove the previous day's date on a daily basis and I'm not too sure how to go about generating this but I've seen date drop-downs on sites before.
The DD/MM/YYYY format would be best, they don't need to read the day of the week too, I know that's more work.
I don't have any working code because I'm not sure on the best approach. Any comments, pointers or code would be really appreciated.
This should more or less do it for you:
<?php
$now = time();
$month = date('m', $now);
$day = date('j', $now);
$year = date('y', $now);
echo '<select>';
echo '<option value="Not Selected" disabled selected>Please select a date.</option>';
for ($m = $month; $m <= 12; $m++)
{
echo '<optgroup label="' . date("F", strtotime("$year-$m-01")) . '">';
$startDay = $m == $month ? $day : 1;
for ($d = $startDay; $d <= cal_days_in_month(CAL_GREGORIAN, $m, $year); $d++)
{
echo '<option value="' . $d . '">' . $d . '/' . $m . '/' . $year . '</option>' . "\n";
}
}
echo '</select>';
?>
I need to display 3 month from the previous month in php
Here is what I have tried so far
<select style='width: 112px;' name='PayMonth'>
<option name="PayMonth" value=''>Select Month</option>
<?php
for($i = 1 ; $i <= 12; $i++)
{
$allmonth = date("F",mktime(0,0,0,$i,1,date("Y")))
?>
<option value="<?php echo $i; ?>" ><?php echo date("F",mktime(0,0,0,$i,1,date("Y")));?>
</option>
<?php
}?>
This will display all months
But how can I display on 3 months from current month
i.e.,
If current month is april it should show Feb, Mar, Apr
If current month is jan it should show nov, dec, jan
I tried..
<?php
$month = date("m");
?>
But in the loop I am confused with
for($i = 1 ; $i <= $month; $i++)
If you are getting $month = date("m"); Then your forloop should be
for($i = $month-3 ; $i <= $month; $i++)
Sidenote :
As you are using
<option value="<?php echo $i; ?>" ><?php echo date("F",mktime(0,0,0,$i,1,date("Y")));?>
You may get -1, -2 inside the values. So you should change
<option value="<?php echo date("m",mktime(0,0,0,$i,1,date("Y"))); ?>" ><?php echo date("F",mktime(0,0,0,$i,1,date("Y")));?>
To get
<select style='width: 112px;' name='PayMonth'>
<option name="PayMonth" value=''>Select Month</option>
<option value="11" >November </option>
<option value="12" >December </option>
<option value="01" >January </option>
<option value="02" >February </option>
</select>
Easy as cake:
echo date('Y-m-d', strtotime("first day of -1 month"));
And you can substitute -1 with the number of your choice:
<?php
foreach(range(-1,-3,-1) as $value) {
echo date('Y-m-d', strtotime(sprintf("first day of %d month", $value)));
}
Just do:
date('m', strtotime('-1 month'));
to get last month.
You can put -2 or -3 to get 2 months ago or 3 months ago also.
Substitute 'm' with the format of your choice.
Try this..
echo date('M', strtotime('0 month'));
echo date('M', strtotime('-1 month'));
echo date('M', strtotime('-2 month'));
echo date('M', strtotime('-3 month'));
Take into consideration that you will get wrong results when using months back using the current day - on every 31st day the month.
Try this instead:
<?
$monthsBack = 3;
// we need to do set day to middle of the month,
// otherwise "-x month" will return wrong results on the 31st of each month:
$now = mktime(0, 0, 0, date("m"), 15, date("Y"));
for ($i1=0; $i1<$monthsBack; $i1++) {
$displayDate = strtotime("-{$monthsBack} month", $now);
echo '<option value="'.date("m", $displayDate).'" >'.date("F", $displayDate).'</option>';
}
?>
// Today is 2015-02-30
echo date('Y-m-d', strtotime('last month'));
I select the dropdowns and nothing populates. I am not sure why? I am new to php so any help is much appreciated.
here is my code:
<section id="content" class="planner">
<select id="month" name="month">
<option value="0">--Select Month--</option>
<option value="January">January</option>
<option value="Februrary">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<select id="year" name="year">
<option value="0">--Select Year--</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2012">2013</option>
<option value="2012">2014</option>
<option value="2012">2015</option>
<option value="2012">2016</option>
</select>
<table class="month">
<tr class="days">
<td>Mon</td>
<td>Tues</td>
<td>Wed</td>
<td>Thurs</td>
<td>Fri</td>
<td>Sat</td>
<td>Sun</td>
</tr>
<?php
if($_POST['month'] && $_POST['year'] != 0)
{
$today = date("d"); // Current day
$month=$_POST['month'];
$year=$_POST['year'];
GenerateCalendar($today,$month,$year);
}
function GenerateCalendar($today,$month,$year)
{
$today = date("d"); // Current day
$days = cal_days_in_month(CAL_GREGORIAN,$month,$year); // Days in current month
$lastmonth = date("t", mktime(0,0,0,$month-1,1,$year)); // Days in previous month
$start = date("N", mktime(0,0,0,$month,1,$year)); // Starting day of current month
$finish = date("N", mktime(0,0,0,$month,$days,$year)); // Finishing day of current month
$laststart = $start - 1; // Days of previous month in calander
$counter = 1;
$nextMonthCounter = 1;
if($start > 5){ $rows = 6; }else {$rows = 5; }
for($i = 1; $i <= $rows; $i++){
echo '<tr class="week">';
for($x = 1; $x <= 7; $x++){
if(($counter - $start) < 0){
$date = (($lastmonth - $laststart) + $counter);
$class = 'class="blur"';
}else if(($counter - $start) >= $days){
$date = ($nextMonthCounter);
$nextMonthCounter++;
$class = 'class="blur"';
}else {
$date = ($counter - $start + 1);
if($today == $counter - $start + 1){
$class = 'class="today"';
}
}
echo '<td '.$class.'><a class="date">'. $date . '</a></td>';
$counter++;
$class = '';
}
echo '</tr>';
}
}
?>
</table>
</section>
You are missing the html <form> tags around your select statements. You are also going to need an input type submit to send the form
<form action="" method="POST" name="myForm">
<!--YOUR SELECTS-->
<input type="submit" value="SEND THIS FORM"/>
</form>
the form action"" sends the post data to your current page. The method is either POST or GET. You are using POST.
That's all. You should consider learning HTML basics before continuing onto php.
Im writing code practicing PHP if and else statements and form validation.
the basic idea is this:
After entering name, DOB , and email and clicking the submit button, Based on the DOB they enter the button leads them to leads to:
-a page telling them they are too young to drink (notwelcome.php)
OR
-a page telling them they can order a drink (welcome.php)
The 2 pages (notwelcome.php & welcome.php) are pulled from separate file called action.php saved like this:
<?php
include('welcome.php');
include('notwelcome.php');
?>
This is what i have been trying ..but its not working. nothing happens.. Its as if the if else code isnt even there :(
<?php
if ($_POST['submit']) {
$dob = $_POST['dob'];
if (isset ($_POST['dob'] )> 12/31/1992) {
header("Location: notwelcome.php");
} else {
header("Location: welcome.php");}
}
?>
Help. Im a beginner and i have hit a small bump in the road in my code.
additional infor:
HTML Code is like this:
<div style="text-align: center;">
<h2>FORM & PHP</h2>
<h3>WHINE & DINE</h3>
<form action="action.php" method="post">
Full Name: <input type="text" name="name"><br>
Date of Birth: <input type="date" name="dob"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit" data-inline="true" value="Submit">
</form>
</div>
</div>
</form>
Try this. Also, you don't need to include those files unless you want them showing up on the page before you process the form. I would check to make sure you have the relative path correct. You would also want to make it so users enter the DOB in the right format.
<?php
if (isset($_POST['dob'])) {
$dob = $_POST['dob'];
if ($dob > date("m/d/Y", mktime(0, 0, 0, date('m'), date('d'), date('Y') - 21))) {
header("Location: notwelcome.php");
} else {
header("Location: welcome.php");}
}
?>
You can try this. In php code you can add additional if conditions to check validity of ranges.
<?php
if (isset($_POST['date']) && isset($_POST['month']) && isset($_POST['year']) )
{
$dob = date_format (date_create ($_POST['year']."-".$_POST['month']."-".$_POST['date']), "Y-m-d");
if ($dob > date("Y-m-d", mktime(0, 0, 0, date('m'), date('d'), date('Y') - 21)))
{
header("Location: notwelcome.php");
}
else
{
header("Location: welcome.php");
}
}
?>
<html>
<head></head>
<body>
<div style="text-align: center;">
<h2>FORM & PHP</h2>
<h3>WHINE & DINE</h3>
<form action="r.php" method="post">
Full Name: <input type="text" name="name"><br>
Date of Birth: <select name="month">
<option value="01">January</option><option value="02">February</option><option value="03">March</option>
<option value="04">April</option><option value="05">May</option><option value="06">June</option>
<option value="07">July</option><option value="08">August</option><option value="09">September</option>
<option value="10">October</option><option value="11">November</option><option value="12">December</option></select> <select name="date" >
<option value="1">01</option><option value="2">02</option><option value="3">03</option>
<option value="4">04</option><option value="5">05</option><option value="6">06</option>
<option value="7">07</option><option value="8">08</option><option value="9">09</option>
<option value="10">10</option><option value="11">11</option><option value="12">12</option>
<option value="13">13</option><option value="14">14</option><option value="15">15</option>
<option value="16">16</option><option value="17">17</option><option value="18">18</option>
<option value="19">19</option><option value="20">20</option><option value="21">21</option>
<option value="22">22</option><option value="23">23</option><option value="24">24</option>
<option value="25">25</option><option value="26">26</option><option value="27">27</option>
<option value="28">28</option><option value="29">29</option><option value="30">30</option><option value="31">31</option>
</select> <input name="year" type="text" id="year" size="4" maxlength="4"> <span>(YYYY)</span>
<br>
E-mail: <input type="text" name="email"><br>
<input type="submit" data-inline="true" value="Submit">
</form>
</div>
</body>
</html>
There's probably a more elegant way to do it, but this worked for me:
if(isset($_POST['dob'])) { $dob = $_POST['dob'];
list($date,$time) = explode(" ", $dob);
list($year,$month,$day) = explode("-",$date);
$years = date("Y") - $year;
$months = date("m") - $month;
$days = date("d") - $day;
if (($years > 21) || ($years == 21 && $months > 0) || ($years == 21 && $months == 0 && $days >= 0)) {
header("Location: welcome.php");
} else {
header("Location: notwelcome.php");
}
}
Subtracting the birth year from the current year, the birth month from the current month and the birth day from the current day, this basically runs up to three tests. First, if the current year is more than 21 years after the person's birth year, they're legal. Second, if it's 21 years after the person's birth year but it's after their birth month, they're legal. Finally, if it's 21 years after the person's birth year and it happens to be their birth month and the day of the month is greater than or equal to the person's birth day, they're legal. If none of those things are true, they're under 21.
I have an HTML select tag like this:
<select name="since_date">
<option value="<?php $sixmonths = date('Y-m-d', strtotime('-6 months')); echo $sixmonths; ?>">6 months</option>
<option value="<?php $fivemonths = date('Y-m-d', strtotime('-5 months')); echo $fivemonths; ?>">5 months</option>
<option value="<?php $fourmonths = date('Y-m-d', strtotime('-4 months')); echo $fourmonths; ?>">4 months</option>
<option value="<?php $threemonths = date('Y-m-d', strtotime('-3 months')); echo $threemonths; ?>">3 months</option>
<option value="<?php $twomonths = date('Y-m-d', strtotime('-2 months')); echo $twomonths; ?>">2 months</option>
<option value="<?php $onemonth = date('Y-m-d', strtotime('-1 month')); echo $onemonth; ?>">1 month</option>
</select>
And I want when for example 4 months is selected a new select tag to appear like this one:
<select name="until_date_4months">
<option value="<?php $threemonths = date('Y-m-d', strtotime('-3 months')); echo $threemonths; ?>">3 months</option>
<option value="<?php $twomonths = date('Y-m-d', strtotime('-2 months')); echo $twomonths; ?>">2 months</option>
<option value="<?php $onemonth = date('Y-m-d', strtotime('-1 month')); echo $onemonth; ?>">1 month</option>
<option value="<?php $now = date('Y-m-d'); echo $now; ?>">Now</option>
</select>
Every time a period is chosen (Ex. 6 months, 5 months) a new select must appear with options lower than the selected one plus a new option "Now" like above. How is this possible? What is the approach? I want to use these information with a submit button. (Send with JQuery a GET request to a specific PHP file <- this I know how to handle).
I can for example use JavaScript like this:
function sinceDateValue(selection) {
if (selection.value == "<?php $fourmonths = date('Y-m-d', strtotime('-4 months')); echo $fourmonths; ?>") {
document.getElementbyId(until_date_4months).style.display = "block"
}
}
and use for the second submit id="until_date_4months" and style="display: none;". Also for the first submit onchange="testValue(this); but this will just show the submit and I want to send only two information with the form: since_date and until_date...
You can create the options dynamically using a loop:
<?php for ($i = 6; $i >= 0; $i--) { ?>
<option value="<?php echo date('Y-m-d', strtotime('-' . $i . ' months')); ?>">
<?php echo ($i == 0) ? 'Now' : $i . ' months'; ?>
</option>
<?php } ?>
As for the jQuery, you can grab the elements based on the one selected, and populate your select box with those:
$('select[name="since_date"]').on('change', function() {
var newOpts = $('option:selected', this).nextAll().clone();
$(newOpts).show().appendTo('#example');
});
Here's a complete example fiddle
You can do smomething like this, all you need to do is set the right values of the options.
$("#one").change(function(){
var htmlOf2 = "", d;
for(var i=1;i<parseInt($(this).find("option:selected").text()) + 1;i++)
{
d = new Date();
d = d.setMonth(d.getMonth() + (i * -1));
// This sets the right month, but is not set as a value yet. I don't know what format you want
htmlOf2 += "<option value=''>" + (i + " months") + "</option>";
}
htmlOf2 += "<option>NOW</option>";
$("#two").html(htmlOf2);
}).trigger("change");
Live fiddle: http://jsfiddle.net/5qaw7/1/