sanderphp Posted May 6, 2008 Share Posted May 6, 2008 I have a form where a user fills out the date when the event occurred. Currently you have to type 2008/5/6 in the box everytime. I would like to simply it. Either I could probably just have them type in the month/day and concatenate it with the year or I could have the year filled in the already. Ideally I would have one of this little popup calendars that so many sites have where you click on a date, but that is probably a stretch for me at this point. If you have other suggestions, I'm open to them. I've very new to php. What do you guys suggest? $date = $_POST['date']; $eventdate = '2008'$date; something like this? I doubt that code is correct. Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/ Share on other sites More sharing options...
conker87 Posted May 6, 2008 Share Posted May 6, 2008 Why don't you use drop down fields? Then you can join them during posting. Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-534269 Share on other sites More sharing options...
sanderphp Posted May 6, 2008 Author Share Posted May 6, 2008 Is there a way to have the drop down default to the current month and day on load or will it always default to 1 or null? Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-534355 Share on other sites More sharing options...
conker87 Posted May 6, 2008 Share Posted May 6, 2008 Add: <select> <option>blah</option> <option>blah2</option> <option selected>blah3</option> </select> Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-534368 Share on other sites More sharing options...
saint959 Posted May 6, 2008 Share Posted May 6, 2008 hey, here is a quick fix for you. i think it should work, if not you can play around with the idea Add: <select> <option value="January" <?php if (date('F') == "January"){ echo "selected";} ?>>January</option> <option value="February" <?php if (date('F') == "February"){ echo "selected";} ?>>February</option> <option value="March" <?php if (date('F') == "March"){ echo "selected";} ?>>March</option> </select> Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-534371 Share on other sites More sharing options...
sanderphp Posted May 6, 2008 Author Share Posted May 6, 2008 Ok, been trying to wrap my head around this. Here is how I'm using this code on the page. Don't I need something to get the current year, month, day and pass that as the default? echo date('Y'); Here is what I have thus far: <label>dateyear <select name="dateyear" id="dateyear"> <option value="2008" <?php if (date('Y') == "2008"){ echo "selected";} ?>>2008</option> <option value="2009" <?php if (date('Y') == "2009"){ echo "selected";} ?>>2009</option> <option value="2010" <?php if (date('Y') == "2010"){ echo "selected";} ?>>2010</option> </select> </label> <label>datemonth <select name="datemonth" id="datemonth"> <option value="January" <?php if (date('F') == "January"){ echo "selected";} ?>>January</option> <option value="February" <?php if (date('F') == "February"){ echo "selected";} ?>>February</option> <option value="March" <?php if (date('F') == "March"){ echo "selected";} ?>>March</option> </select> </label> Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-534560 Share on other sites More sharing options...
sanderphp Posted May 6, 2008 Author Share Posted May 6, 2008 anyone? Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-534741 Share on other sites More sharing options...
realjumper Posted May 6, 2008 Share Posted May 6, 2008 What you have here is fine: <label>dateyear <select name="dateyear" id="dateyear"> <option value="2008" <?php if (date('Y') == "2008"){ echo "selected";} ?>>2008</option> <option value="2009" <?php if (date('Y') == "2009"){ echo "selected";} ?>>2009</option> <option value="2010" <?php if (date('Y') == "2010"){ echo "selected";} ?>>2010</option> </select> </label> <label>datemonth <select name="datemonth" id="datemonth"> <option value="January" <?php if (date('F') == "January"){ echo "selected";} ?>>January</option> <option value="February" <?php if (date('F') == "February"){ echo "selected";} ?>>February</option> <option value="March" <?php if (date('F') == "March"){ echo "selected";} ?>>March</option> </select> </label> You are 'getting' the current year and month in your example, it works. Just extend the months a little to get this month.....try it and see. <label>dateyear <select name="dateyear" id="dateyear"> <option value="2008" <?php if (date('Y') == "2008"){ echo "selected";} ?>>2008</option> <option value="2009" <?php if (date('Y') == "2009"){ echo "selected";} ?>>2009</option> <option value="2010" <?php if (date('Y') == "2010"){ echo "selected";} ?>>2010</option> </select> </label> <label>datemonth <select name="datemonth" id="datemonth"> <option value="January" <?php if (date('M') == "January"){ echo "selected";} ?>>January</option> <option value="February" <?php if (date('M') == "February"){ echo "selected";} ?>>February</option> <option value="March" <?php if (date('M') == "March"){ echo "selected";} ?>>March</option> <option value="April" <?php if (date('M') == "April"){ echo "selected";} ?>>April</option> <option value="May" <?php if (date('M') == "May"){ echo "selected";} ?>>May</option> <option value="June" <?php if (date('M') == "June"){ echo "selected";} ?>>June</option> </select> </label> Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-534751 Share on other sites More sharing options...
sanderphp Posted May 6, 2008 Author Share Posted May 6, 2008 oh jeez. I had no idea it was that simple. Thought for sure I was missing something. Thanks Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-534791 Share on other sites More sharing options...
sanderphp Posted May 6, 2008 Author Share Posted May 6, 2008 Is there a better way to list the days of the month? This seems like it will take a while to type in ... <option value="1" <?php if (date('Y') == "1"){ echo "selected";} ?>>1</option> <option value="2" <?php if (date('Y') == "2"){ echo "selected";} ?>>2</option> <option value="3" <?php if (date('Y') == "3"){ echo "selected";} ?>>3</option> Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-534794 Share on other sites More sharing options...
BlueSkyIS Posted May 6, 2008 Share Posted May 6, 2008 for loop Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-534795 Share on other sites More sharing options...
realjumper Posted May 6, 2008 Share Posted May 6, 2008 Is there a better way to list the days of the month? This seems like it will take a while to type in ... How about.... <select name="date"> <?php $date1 = date("j"); echo "<option value=\"$date1\" selected=\"selected\">$date1</option>"; $date = 1; while ( $date <=31 ) { echo "<option value=\"$date\">$date</option>"; $date++; } ?> </select> Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-534829 Share on other sites More sharing options...
Mr. Despair Posted May 7, 2008 Share Posted May 7, 2008 How about.... <select name="date"> <?php $date1 = date("j"); echo "<option value=\"$date1\" selected=\"selected\">$date1</option>"; $date = 1; while ( $date <=31 ) { echo "<option value=\"$date\">$date</option>"; $date++; } ?> </select> Hmm, I think you'd get two options with the value of the current day then. This could also work if you only want it to show once: <?php for ($x=1; $x<=31; $x++) { if (date('j') != $x) { echo "<option value=\"$x\">$x</option>"; } else { echo "<option value=\"$x\" selected=\"selected\">$x</option>"; } } ?> Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-534833 Share on other sites More sharing options...
realjumper Posted May 7, 2008 Share Posted May 7, 2008 Yep...your way is probably better than mine...thanks Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-534843 Share on other sites More sharing options...
ablueycolor Posted May 7, 2008 Share Posted May 7, 2008 or to shorten things up a bit (also, please indulge me in why the code formatting is automatically removing my backslashes around selected=\"selected\" upon posting): <?php $day = date('j'); for ($x=1; $x<=31; $x++) { echo "<option value=\"$x\" . (($day != $x) ? " selected=\"selected\"" : "") . ">$x</option>"; } ?> Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-534879 Share on other sites More sharing options...
sanderphp Posted May 7, 2008 Author Share Posted May 7, 2008 Here is what I added but I'm getting a Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in: <label> Day 2 <select name="dateday2" id="dateday2"> <?php $day = date('j'); for ($x=1; $x<=31; $x++) { echo "<option value=\"$x\" . (($day != $x) ? " selected="\selected\"" : "") . ">$x</option>"; } ?> </label> Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-535114 Share on other sites More sharing options...
sanderphp Posted May 7, 2008 Author Share Posted May 7, 2008 any help on my parse error? Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-535372 Share on other sites More sharing options...
sanderphp Posted May 7, 2008 Author Share Posted May 7, 2008 The parse error is on the echo line if I didn't make that clear on my previous post. Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/sanderan/public_html/php/cycling/date.php on line 68 Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-535384 Share on other sites More sharing options...
revraz Posted May 7, 2008 Share Posted May 7, 2008 You need to fix your Quotes Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-535386 Share on other sites More sharing options...
sanderphp Posted May 7, 2008 Author Share Posted May 7, 2008 Can you expand on that? Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-535413 Share on other sites More sharing options...
revraz Posted May 7, 2008 Share Posted May 7, 2008 Sure can, you are using double quotes inside of double quotes so it's not giving you the results you want. Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-535451 Share on other sites More sharing options...
Mr. Despair Posted May 7, 2008 Share Posted May 7, 2008 echo "<option value=\"$x\" . (($day != $x) ? " selected="\selected\"" : "") . ">$x</option>"; Hmm, there is no ending quote after your escaped quote... And after you placed the backslash before the quote after selected. So something like below should work. Also, I think it should be the equal operator instead of not equal. echo "<option value=\"$x\"" . (($day == $x) ? " selected=\"selected\"" : "") . ">$x</option>"; Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-535462 Share on other sites More sharing options...
sanderphp Posted May 7, 2008 Author Share Posted May 7, 2008 perfect! Thanks Link to comment https://forums.phpfreaks.com/topic/104356-solved-best-way-to-insert-date-on-a-form/#findComment-535468 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.