Collecting date from select tags

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 39737 Views - Last Post: 22 November 2010 - 01:12 AM Rate Topic: -----

#1 Shado3225  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 325
  • Joined: 23-February 10

Collecting date from select tags

Posted 19 November 2010 - 12:58 AM

Hi guys

I'm hoping you can help me as I'm at a loss.

I have a contact form which requires the user to enter their birthday which is all sent via email. My problem is that I can't seem to get my code to accept the day, month and date.

At the beginning I had it as an input which worked fine but I wanted to rather use select boxes and it keeps a consistent look but I have never worked with select tags in php before.

This is the HTML - I have removed all but 2 of the options in each select tag to save space.
	<div class="formfield">
		<div class="labelbox"><label for="dob">Date of Birth:</label></div>
		<div class="inputbox"><select name="dob_dd" type="text" class="field" id="dob" tabindex="4" value="<?= $_SESSION['myForm']['dob']; ?>">
				<option value="0" disabled="disabled" selected="selected" style="color:#9999FF;">Day</option>
				<option value="1">1</option>
				<option value="2">2</option>
			</select>
			<select name="dob_mm" type="text" class="field" id="dob" tabindex="5" value="<?= $_SESSION['myForm']['dob']; ?>">
				<option value="0" disabled="disabled" selected="selected" style="color:#9999FF;">Month</option>
				<option value="January">January</option>
				<option value="Febuary">Febuary</option>
			</select>
			<select name="dob_yy" type="text" class="field" id="dob" tabindex="6" value="<?= $_SESSION['myForm']['dob']; ?>">
				<option value="0" disabled="disabled" selected="selected" style="color:#9999FF;">Year</option>
				<option value="2004">2010</option>
				<option value="2004">2009</option>
			</select>
		</div>
	</div>


I'm sure this is where the problem is - I need to tell the code to take dob_dd, dob_mm and dob_yy and covert it to dd/mm/yyyy.
$dob = trim($_POST['dob']);


This is my if statement but I'm sure I can sort that out ones I have the date code working.
	if (empty($_POST['dob']) ) {
		$errors[]='You forgot to enter your date of birth';
		}


Any assistance will be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Collecting date from select tags

#2 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5191
  • View blog
  • Posts: 26,899
  • Joined: 10-May 07

Re: Collecting date from select tags

Posted 19 November 2010 - 01:02 AM

Your month & year input id's are the same. They must be unique. I would suggest making them the same as the name value.
Was This Post Helpful? 1
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3489
  • View blog
  • Posts: 10,057
  • Joined: 08-June 10

Re: Collecting date from select tags

Posted 19 November 2010 - 01:05 AM

you have no form element named "dob"!
Was This Post Helpful? 1
  • +
  • -

#4 Shado3225  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 325
  • Joined: 23-February 10

Re: Collecting date from select tags

Posted 19 November 2010 - 01:08 AM

Good point, thank you, I have changed it.

View PostDormilich, on 19 November 2010 - 09:05 AM, said:

you have no form element named "dob"!


Ya, I'm kind of stumped on that one as my form elements are dob_dd. dob_mm and dob_yy but I need to get it to print dob_dd/dob_mm/dob_yyyy to the email and I have no idea how to do that.
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3489
  • View blog
  • Posts: 10,057
  • Joined: 08-June 10

Re: Collecting date from select tags

Posted 19 November 2010 - 01:10 AM

$_POST['dob_dd'] . "/" . $_POST['dob_mm'] . "/" . $_POST['dob_yy']
?

This post has been edited by Dormilich: 19 November 2010 - 01:10 AM

Was This Post Helpful? 1
  • +
  • -

#6 Shado3225  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 325
  • Joined: 23-February 10

Re: Collecting date from select tags

Posted 19 November 2010 - 01:19 AM

I have now inserted $dob = trim($_POST['dob_dd'] . "/" . $_POST['dob_mm'] . "/" . $_POST['dob_yy']); but It's still not picking it up. I suspect its to do with $dob. As you said there is no element named dob but what do I name it then as all 3 select tags have been named already.

This post has been edited by Shado3225: 19 November 2010 - 01:21 AM

Was This Post Helpful? 0
  • +
  • -

#7 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5191
  • View blog
  • Posts: 26,899
  • Joined: 10-May 07

Re: Collecting date from select tags

Posted 19 November 2010 - 01:22 AM

Change the id to match the name, on your select values.

Change this :

<select name="dob_yy" type="text" class="field" id="dob" tabindex="6" value="<?= $_SESSION['myForm']['dob']; ?>">



To this :
<select name="dob_yy" type="text" class="field" id="dob_yy" tabindex="6" value="<?= $_SESSION['myForm']['dob']; ?>">


Was This Post Helpful? 1
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3489
  • View blog
  • Posts: 10,057
  • Joined: 08-June 10

Re: Collecting date from select tags

Posted 19 November 2010 - 01:22 AM

what does $dob = trim($_POST['dob_dd'] . "/" . $_POST['dob_mm'] . "/" . $_POST['dob_yy']); return? what does your form tag say?

This post has been edited by Dormilich: 19 November 2010 - 01:23 AM

Was This Post Helpful? 1
  • +
  • -

#9 Shado3225  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 325
  • Joined: 23-February 10

Re: Collecting date from select tags

Posted 19 November 2010 - 01:30 AM

This is my code at present.

<select name="dob_dd" type="text" class="field" id="dob_dd" tabindex="4" value="<?= $_SESSION['myForm']['dob']; ?>">
<select name="dob_mm" type="text" class="field" id="dob_mm" tabindex="5" value="<?= $_SESSION['myForm']['dob']; ?>">
<select name="dob_yy" type="text" class="field" id="dob_yy" tabindex="6" value="<?= $_SESSION['myForm']['dob']; ?>">



$dob = trim($_POST['dob_dd'] . "/" . $_POST['dob_mm'] . "/" . $_POST['dob_yy']);


if (empty($_POST['dob']) ) {
		$errors[]='You forgot to enter your date of birth';
		}


You can see a working copy at this link
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5191
  • View blog
  • Posts: 26,899
  • Joined: 10-May 07

Re: Collecting date from select tags

Posted 19 November 2010 - 01:39 AM

I would suggest taking out the value="" in the select tag.

In my PHP/Select Tutorial the only value that I set to the select tag is it's name & each of the options contains it's own value.
Was This Post Helpful? 0
  • +
  • -

#11 Shado3225  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 325
  • Joined: 23-February 10

Re: Collecting date from select tags

Posted 19 November 2010 - 01:43 AM

It's worth a try but how is my code going to know to add dob_dd to $dob?
Was This Post Helpful? 0
  • +
  • -

#12 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5191
  • View blog
  • Posts: 26,899
  • Joined: 10-May 07

Re: Collecting date from select tags

Posted 19 November 2010 - 01:49 AM

Here, try this :

	<div class="formfield">
		<div class="labelbox"><label for="dob">Date of Birth:</label></div>
		<div class="inputbox"><select name="dob_dd" type="text" tabindex="4">
				<option value="0" disabled="disabled" selected="selected" style="color:#9999FF;">Day</option>
				<option value="1">1</option>
				<option value="2">2</option>
			</select>
			<select name="dob_mm" class="field" tabindex="5">
				<option value="0" disabled="disabled" selected="selected" style="color:#9999FF;">Month</option>
				<option value="January">January</option>
				<option value="Febuary">Febuary</option>
			</select>
			<select name="dob_yy" type="text" tabindex="6">
				<option value="0" disabled="disabled" selected="selected" style="color:#9999FF;">Year</option>
				<option value="2004">2010</option>
				<option value="2004">2009</option>
			</select>
		</div>
	</div>



It's being evaluated as value="" because I don't have a session running, so there is nothing in the value. Besides, to have a 'value' you set the option to selected, not the select tag.
Was This Post Helpful? 1
  • +
  • -

#13 Shado3225  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 325
  • Joined: 23-February 10

Re: Collecting date from select tags

Posted 19 November 2010 - 01:49 AM

Tested it by taking out the value but I'm still getting the same error.
Was This Post Helpful? 0
  • +
  • -

#14 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5191
  • View blog
  • Posts: 26,899
  • Joined: 10-May 07

Re: Collecting date from select tags

Posted 19 November 2010 - 01:55 AM

http://akroncdnr.com/dob.php

<?php
$err=0;
if($_POST['dob_dd']) $day=$_POST['dob_dd'];
else $err++;
if($_POST['dob_mm']) $month=$_POST['dob_mm'];
else $err++;
if($_POST['dob_yy']) $year=$_POST['dob_yy'];
else $err++;
if($err==0) {
  $dob=$day."/".$month."/".$year;
  echo "<p>Birthday : ".$dob;
}
?>
        <form method=post action="dob.php">
        <div class="formfield">
                <div class="labelbox"><label for="dob">Date of Birth:</label></div>
                <div class="inputbox"><select name="dob_dd" type="text" tabindex="4">
                                <option value="0" disabled="disabled" selected="selected" style="color:#9999FF;">Day</option>
                                <option value="1">1</option>
                                <option value="2">2</option>
                        </select>
                        <select name="dob_mm" class="field" tabindex="5">
                                <option value="0" disabled="disabled" selected="selected" style="color:#9999FF;">Month</option>
                                <option value="January">January</option>
                                <option value="Febuary">Febuary</option>
                        </select>
                        <select name="dob_yy" type="text" tabindex="6">
                                <option value="0" disabled="disabled" selected="selected" style="color:#9999FF;">Year</option>
                                <option value="2004">2010</option>
                                <option value="2004">2009</option>
                        </select>
                </div>
        </div>
        <input type="submit">
</form>



Was This Post Helpful? 1
  • +
  • -

#15 Shado3225  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 325
  • Joined: 23-February 10

Re: Collecting date from select tags

Posted 19 November 2010 - 01:59 AM

I'm thinking and I might be wrong but maybe your value suggest is right and the problem is with my if statement. I'm going to take it out and see if it will pass.

Well seems I was right. It was indeed the if statement. After taking it out it passed all fields and mailed the following back.

From: test
Surname: test
Email: test@test.co.za
DOB: 3/Febuary/2004
Comments: test


So now the question is - what have I done wrong.

Old code
	if (empty($_POST['dob']) ) {
		$errors[]='You forgot to enter your date of birth';
		}


Should it be

	if (empty($_POST['dob_dd']['dob_mm']['dob_yy']) ) {
		$errors[]='You forgot to enter your date of birth';
		}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2