iEntry 10th Anniversary Forum Rules Search
WebProWorld
Register FAQ Calendar Mark Forums Read
Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Share Thread: & Tags

Share Thread:

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-20-2009, 01:27 AM
WebProWorld New Member
 
Join Date: Jul 2009
Posts: 1
shifanyroma RepRank 0
Default Validation of php drop down menu of months,days and years

Cannot get it to validate, for leaps years, and feb,april 31.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>

<?php
$todo=$_POST['todo'];
if(isset($todo) and $todo=="submit"){
$month=$_POST['month'];
$dt=$_POST['dt'];
$year=$_POST['year'];
$date_value="$month/$dt/$year";
echo "mm/dd/yyyy format :$date_value<br>";
$date_value="$year-$month-$dt";
echo "YYYY-mm-dd format :$date_value<br>";
}
?>

<?php


$m="2,4";
$d="31";
$y="";
If(!checkdate($m,$d,$y)){
echo "invalid date";
}else {
echo "Entry date is correct";
}
?>


<form method=post name=f1 action=''><input type=hidden name=todo value=submit>
<table border="0" cellspacing="0" >
<tr><td align=left >
<select name=month value=''>Select 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>

</td><td align=left >
Date<select name=dt >
<option value='01'>01</option>

<option value='02'>02</option>
<option value='03'>03</option>
<option value='04'>04</option>
<option value='05'>05</option>
<option value='06'>06</option>
<option value='07'>07</option>
<option value='08'>08</option>
<option value='09'>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>


<tr><td align=left >
<select name=year value=''>Select Year
<option value='2009'>2009</option>
<option value='2010'>2010</option>
<option value='2011'>2011</option>
<option value='2012'>2012</option>
<option value='2013'>2013</option>
<option value='2014'>2014</option>
<option value='2015'>2015</option>
<option value='2016'>2016</option>
<option value='2017'>2017</option>
<option value='2018'>2018</option>
<option value='2019'>2019</option>
<option value='2020'>2020</option>
</select>

<input type=submit value=Submit>
</table>


</form>



</html>
Reply With Quote
  #2 (permalink)  
Old 07-20-2009, 03:30 PM
Uncle Dog's Avatar
WebProWorld Pro
 
Join Date: Apr 2008
Location: Scotland
Posts: 269
Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5
Default Re: Validation of php drop down menu of months,days and years

For a start, your test values will cause the php checkdate function to return false. So as far as that goes it's working correctly. I guess this is just another case of cut'n'paste'n'hope. You didn't know they were test values did you, otherwise you'd know why the $m="2,4" is really wrong. Or perhaps you did but didn't realise what checkdate does.

<?php


$m="2,4";
$d="31";
$y="";

If(!checkdate($m,$d,$y)){
echo "invalid date";
}else {
echo "Entry date is correct";
}
?>

Last edited by Uncle Dog; 07-20-2009 at 03:49 PM.
Reply With Quote
  #3 (permalink)  
Old 07-20-2009, 06:37 PM
WebProWorld New Member
 
Join Date: Dec 2005
Location: UK
Posts: 12
iowarth RepRank 0
Default Re: Validation of php drop down menu of months,days and years

Or, to put it another way, it doesn't have the right data - it simply needs the date - not the example values for $m, $d, $y which you have set. So, ideally you should have a separate function along the lines of:-
function validatedate($date)

{
If(!checkdate($m,$d,$y))
{
echo "invalid date";
}else {
echo "Entry date is correct";
}
}

Then AFTER you have created the date (your value $date_value) you can call the function.

Do it that way and if you have multiple forms needing the same validation you can simply include/require it in all of them.

Chris
Reply With Quote
  #4 (permalink)  
Old 07-20-2009, 07:06 PM
Uncle Dog's Avatar
WebProWorld Pro
 
Join Date: Apr 2008
Location: Scotland
Posts: 269
Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5
Default Re: Validation of php drop down menu of months,days and years

Quote:
Originally Posted by iowarth View Post
Then AFTER you have created the date (your value $date_value) you can call the function.
Sorry, you're wrong. The month, day & year variables are set by the form and are ready to be passed straight to checkdate() for testing providing they are renamed.

I am assuming it's a self referencing form page. I'm also ignoring a lot of other nonsense. shifanyroma - If you want some more help then please ask, and join in the discussion so that you can learn something. Otherwise I'll just assume you've got some school homework problem and limited time.

Last edited by Uncle Dog; 07-20-2009 at 07:15 PM.
Reply With Quote
  #5 (permalink)  
Old 07-21-2009, 12:18 AM
Steelchord's Avatar
WebProWorld Member
 
Join Date: Nov 2005
Location: Tampa, FL
Posts: 31
Steelchord RepRank 1
Default Re: Validation of php drop down menu of months,days and years

Ok, yeah. Wow.

I think that the biggest misunderstanding here is that a leap year february can have 31 days. The most days Feb will ever see is 29. The checkdate function knows that, which is why your test variables (assuming that the double variable is for our benefit and you didn't actually expect a month of "2,4" to check positive) failed.

This page is so broken-unclosed table cells, no protection against cross site scripting, senseless outputs, I hope this script is never used on a production page.

That said, here's a quick and dirty fix.

<?php
$todo=htmlentities($_POST['todo']);
if(isset($todo) and $todo=="submit"){
$month=htmlentities($_POST['month']);
$dt=htmlentities($_POST['dt']);
$year=htmlentities($_POST['year']);
$date_value="$month/$dt/$year";
echo "mm/dd/yyyy format :$date_value<br>";
$date_value="$year-$month-$dt";
echo "YYYY-mm-dd format :$date_value<br>";
}

$m=$month;
$d=$dt;
$y=$year;
If(!checkdate($m,$d,$y)){
echo "invalid date";
}else {
echo "Entry date is correct";
}
?>
Reply With Quote
  #6 (permalink)  
Old 07-21-2009, 12:19 AM
Steelchord's Avatar
WebProWorld Member
 
Join Date: Nov 2005
Location: Tampa, FL
Posts: 31
Steelchord RepRank 1
Default Re: Validation of php drop down menu of months,days and years

Btw, I'm half asleep so I make no guarantees that the above is bug free code. But it should point you in the right direction.
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Google Refresh - days? weeks? months? joebull Google Discussion Forum 16 05-10-2006 01:25 AM
Hurricane cleanup could take months, years WPW_Feedbot IT Discussion Forum 0 08-31-2005 06:01 PM
Simple Javascript Drop Down List validation jestep Web Programming Discussion Forum 1 05-05-2005 05:25 PM
Google PR up & Drop down after few days!!! rsadik Google Discussion Forum 5 01-05-2005 06:45 AM
Drop down menu bradh888 Search Engine Optimization Forum 1 07-08-2004 10:42 AM


All times are GMT -4. The time now is 10:21 AM.



Search Engine Optimization by vBSEO 3.3.0