Page 1 of 4
[SOLVED] Using Select boxes to input date
Posted: Mon May 23, 2005 5:11 am
by mohson
would anyone know how I could use 3 select boxes to input the date. what im finding difficulty in understanding is. How is it possible to have 3 different select boxes (one for year, one for month and one for day) to input into one field.
currently I just use a text box but this causes confusion and erros by users, I want to provide them with 3 text boxes:
Code: Select all
<tr>
<td><font face="Times New Roman, Times, serif"><strong>Date of Last Contact</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="dateoflastcontact" type="text" value="YYYY/MM/01" size="10"style="color: #000000;
background-color: #ADD8E6">
</font></td>
</tr>
Posted: Mon May 23, 2005 6:00 am
by JayBird
try this function
Code: Select all
/*
** Function: DateSelector
** Input: STRING inName, INTEGER useDate
** Output:
** Description: Creates three form fields for get month/day/year
*/
function DateSelector($inName, $useDate)
{
$monthName = array(1=>"January", "February", "March",
"April", "May", "June", "July", "August",
"September", "October", "November", "December");
if($useDate == "")
{
$useDate = Time();
}
print("<SELECT NAME=\"" . $inName . "Month\">\n");
for($currentMonth = 1; $currentMonth <= 12; $currentMonth++)
{
echo "<OPTION VALUE=\"";
echo intval($currentMonth);
echo "\"";
if(intval(date("m", $useDate))==$currentMonth)
{
echo " SELECTED";
}
echo ">".$monthName[$currentMonth]."\n";
}
echo "</SELECT>";
echo "<SELECT NAME=".$inName."Day>\n";
for($currentDay=1; $currentDay <= 31; $currentDay++)
{
echo "<OPTION VALUE=\"$currentDay\"";
if(intval(date("d", $useDate))==$currentDay)
{
echo " SELECTED";
}
echo ">$currentDay\n";
}
echo "</SELECT>";
echo "<SELECT NAME=".$inName."Year>\n";
$startYear = date("Y", $useDate);
if($startYear < 1997)
{
$startYear = date("Y");
}
for($currentYear = $startYear-1; $currentYear <= $startYear+2;$currentYear++)
{
echo "<OPTION VALUE=\"$currentYear\"";
if(date("Y", $useDate)==$currentYear)
{
echo " SELECTED";
}
echo ">$currentYear\n";
}
echo "</SELECT>";
}
useage
Code: Select all
<? DateSelector("field_name",""); ?>
Posted: Mon May 23, 2005 6:08 am
by mohson
Would you mind explaining how I use this is my code, it might take a little of your time but it would help me understand how functions are incorporated into the code.
Ive never used any pre prepared functions before. I dont even understand why people code so much if there are so many functions that can do the job, was this function one you created yourself?
anyhow heres my code that allows me to submit records to the database - again my first quetion would you show me how to incorporate this function into my code.
Lines 72 and 84 are my two date fields
Code: Select all
<?php
/* Connecting, selecting database */
$link = mysql_connect("****", "***", "*****")
or die("Could not connect : " . mysql_error());
echo "";
mysql_select_db("contact_management_system") or die("Could not select database");
?>
<body onLoad="focus()">
<form method="post" action="processpeople.html">
<table width="100%" border="0">
<tr>
<td width="17%"><font face="Times New Roman, Times, serif"><strong>Salutation</strong></font></td>
<td width="27%"><font face="Times New Roman, Times, serif">
<select name="salutation" style="color: #000000;
background-color: #ADD8E6">
<option>Mr</option>
<option>Mrs</option>
<option>Ms</option>
<option>Miss</option>
<option>Prof</option>
<option>Dr</option>
</select>
</font></td>
<td width="27%"><font face="Times New Roman, Times, serif"><strong>Telephone</strong></font></td>
<td width="29%"><input name="telephone" type="text"style="color: #000000;
background-color: #ADD8E6" size="20" maxlength="20">
</td>
</tr>
<tr>
<td><font face="Times New Roman, Times, serif"><strong>First Name</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="firstname" type="text" size="20"style="color: #000000;
background-color: #ADD8E6">
</font></td>
<td><font face="Times New Roman, Times, serif"><strong>Mobile</strong></font></td>
<td><input name="mobile" type="text"style="color: #000000;
background-color: #ADD8E6" size="20" maxlength="20"></td>
</tr>
<tr>
<td><font face="Times New Roman, Times, serif"><strong>Surname</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="surname" type="text" size="15"style="color: #000000;
background-color: #ADD8E6">
</font></td>
<td><font face="Times New Roman, Times, serif"><strong>Fax</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="fax" type="text"style="color: #000000;
background-color: #ADD8E6" size="20" maxlength="20">
</font></td>
<tr>
<td><font face="Times New Roman, Times, serif"><strong>Organisation</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="organisation" type="text" size="20"style="color: #000000;
background-color: #ADD8E6">
<td><font face="Times New Roman, Times, serif"><strong>E-mail</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="email" type="text"style="color: #000000;
background-color: #ADD8E6" size="25" maxlength="50">
</font></td>
</font></tr>
<tr>
<td><font face="Times New Roman, Times, serif"><strong>Role</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="role" type="text" size="25"style="color: #000000;
background-color: #ADD8E6">
</font></td>
<td><font face="Times New Roman, Times, serif"><strong>Date of Last Contact</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="dateoflastcontact" type="text" value="YYYY/MM/01" size="10"style="color: #000000;
background-color: #ADD8E6">
</font></td>
</tr>
<tr>
<td><font face="Times New Roman, Times, serif"><strong>Address (1)</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="address1" type="text" size="20" style="color: #000000;
background-color: #ADD8E6">
</font></td>
<td><font face="Times New Roman, Times, serif"><strong>Date Contact Again</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="datecontactagain" type="text" value="YYYY/MM/01"size="10"style="color: #000000;
background-color: #ADD8E6">
</font></td>
</tr>
<tr>
<td><font face="Times New Roman, Times, serif"><strong>Address(2) </strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="address2" type="text" size="20"style="color: #000000;
background-color: #ADD8E6">
</font></td>
<td><font face="Times New Roman, Times, serif"><strong>OID</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="org_id" type="text"style="color: #000000;
background-color: #ADD8E6" size="5">
</font></td>
</tr>
<tr>
<td><font face="Times New Roman, Times, serif"><strong>City</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="city" type="text" size="20"style="color: #000000;
background-color: #ADD8E6">
</font></td>
<td><font face="Times New Roman, Times, serif"> </font></td>
<td> </td>
</tr>
<tr>
<td height="43"><font face="Times New Roman, Times, serif"><strong>Post
Code</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="postcode" type="text" size="7"style="color: #000000;
background-color: #ADD8E6">
</font></td>
<td><font face="Times New Roman, Times, serif"> </font></td>
<td> </td>
</tr>
</table>
<input type="submit" name="submit" value = "Enter Information">
</form>
<p>
Posted: Mon May 23, 2005 6:29 am
by JayBird
right, i have incorparated the function into your script. analyze it and you will see how easy it is. This was just a function i grabbed from somewhere...no point re-inventing the wheel.
Code: Select all
<?php
/*
** Function: DateSelector
** Input: STRING inName, INTEGER useDate
** Output:
** Description: Creates three form fields for get month/day/year
*/
function DateSelector($inName, $useDate)
{
$monthName = array(1=>"January", "February", "March",
"April", "May", "June", "July", "August",
"September", "October", "November", "December");
if($useDate == "")
{
$useDate = time();
}
print("<SELECT NAME=\"" . $inName . "Month\">\n");
for($currentMonth = 1; $currentMonth <= 12; $currentMonth++)
{
echo "<OPTION VALUE=\"";
echo intval($currentMonth);
echo "\"";
if(intval(date("m", $useDate))==$currentMonth)
{
echo " SELECTED";
}
echo ">".$monthName[$currentMonth]."\n";
}
echo "</SELECT>";
echo "<SELECT NAME=".$inName."Day>\n";
for($currentDay=1; $currentDay <= 31; $currentDay++)
{
echo "<OPTION VALUE=\"$currentDay\"";
if(intval(date("d", $useDate))==$currentDay)
{
echo " SELECTED";
}
echo ">$currentDay\n";
}
echo "</SELECT>";
echo "<SELECT NAME=".$inName."Year>\n";
$startYear = date("Y", $useDate);
if($startYear < 1997)
{
$startYear = date("Y");
}
for($currentYear = $startYear-1; $currentYear <= $startYear+2;$currentYear++)
{
echo "<OPTION VALUE=\"$currentYear\"";
if(date("Y", $useDate)==$currentYear)
{
echo " SELECTED";
}
echo ">$currentYear\n";
}
echo "</SELECT>";
}
/* Connecting, selecting database */
$link = mysql_connect("****", "***", "*****")
or die("Could not connect : " . mysql_error());
echo "";
mysql_select_db("contact_management_system") or die("Could not select database");
?>
<body onLoad="focus()">
<form method="post" action="processpeople.html">
<table width="100%" border="0">
<tr>
<td width="17%"><font face="Times New Roman, Times, serif"><strong>Salutation</strong></font></td>
<td width="27%"><font face="Times New Roman, Times, serif">
<select name="salutation" style="color: #000000;
background-color: #ADD8E6">
<option>Mr</option>
<option>Mrs</option>
<option>Ms</option>
<option>Miss</option>
<option>Prof</option>
<option>Dr</option>
</select>
</font></td>
<td width="27%"><font face="Times New Roman, Times, serif"><strong>Telephone</strong></font></td>
<td width="29%"><input name="telephone" type="text"style="color: #000000;
background-color: #ADD8E6" size="20" maxlength="20">
</td>
</tr>
<tr>
<td><font face="Times New Roman, Times, serif"><strong>First Name</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="firstname" type="text" size="20"style="color: #000000;
background-color: #ADD8E6">
</font></td>
<td><font face="Times New Roman, Times, serif"><strong>Mobile</strong></font></td>
<td><input name="mobile" type="text"style="color: #000000;
background-color: #ADD8E6" size="20" maxlength="20"></td>
</tr>
<tr>
<td><font face="Times New Roman, Times, serif"><strong>Surname</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="surname" type="text" size="15"style="color: #000000;
background-color: #ADD8E6">
</font></td>
<td><font face="Times New Roman, Times, serif"><strong>Fax</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="fax" type="text"style="color: #000000;
background-color: #ADD8E6" size="20" maxlength="20">
</font></td>
<tr>
<td><font face="Times New Roman, Times, serif"><strong>Organisation</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="organisation" type="text" size="20"style="color: #000000;
background-color: #ADD8E6">
<td><font face="Times New Roman, Times, serif"><strong>E-mail</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="email" type="text"style="color: #000000;
background-color: #ADD8E6" size="25" maxlength="50">
</font></td>
</font></tr>
<tr>
<td><font face="Times New Roman, Times, serif"><strong>Role</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="role" type="text" size="25"style="color: #000000;
background-color: #ADD8E6">
</font></td>
<td><font face="Times New Roman, Times, serif"><strong>Date of Last Contact</strong></font></td>
<td><font face="Times New Roman, Times, serif"><?php DateSelector("unique_name_1",""); ?>
</font></td>
</tr>
<tr>
<td><font face="Times New Roman, Times, serif"><strong>Address (1)</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="address1" type="text" size="20" style="color: #000000;
background-color: #ADD8E6">
</font></td>
<td><font face="Times New Roman, Times, serif"><strong>Date Contact Again</strong></font></td>
<td><font face="Times New Roman, Times, serif"><?php DateSelector("unique_name_2",""); ?></font></td>
</tr>
<tr>
<td><font face="Times New Roman, Times, serif"><strong>Address(2) </strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="address2" type="text" size="20"style="color: #000000;
background-color: #ADD8E6">
</font></td>
<td><font face="Times New Roman, Times, serif"><strong>OID</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="org_id" type="text"style="color: #000000;
background-color: #ADD8E6" size="5">
</font></td>
</tr>
<tr>
<td><font face="Times New Roman, Times, serif"><strong>City</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="city" type="text" size="20"style="color: #000000;
background-color: #ADD8E6">
</font></td>
<td><font face="Times New Roman, Times, serif"> </font></td>
<td> </td>
</tr>
<tr>
<td height="43"><font face="Times New Roman, Times, serif"><strong>Post
Code</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="postcode" type="text" size="7"style="color: #000000;
background-color: #ADD8E6">
</font></td>
<td><font face="Times New Roman, Times, serif"> </font></td>
<td> </td>
</tr>
</table>
<input type="submit" name="submit" value = "Enter Information">
</form>
<p>
Posted: Mon May 23, 2005 6:54 am
by mohson
Thank you Pimptastic,
Ive followed this exactly as youve done it but come up with a blank screen any ideas what im doing wrong - heres my code - I really appreciate your time.
Code: Select all
<h1>Administration</h1>
<?php
/*** Function: DateSelector**
Input: STRING inName, INTEGER useDate** Output: **
Description: Creates three form fields for get month/day/year*/ function
DateSelector($inName, $useDate)
{ $monthName = array(1=>"January", "February", "March", "April", "May", "June", "July", "August","September", "October", "November", "December");
if($useDate == "")
{
$useDate = time();
}
print("<SELECT NAME=\"" . $inName . "Month\">\n");
for($currentMonth = 1; $currentMonth <= 12; $currentMonth++)
{
echo "<OPTION VALUE=\"";
echo intval($currentMonth);
echo "\"";
if(intval(date("m", $useDate))==$currentMonth)
{
echo " SELECTED";
}
echo ">".$monthName[$currentMonth]."\n";
}
echo "</SELECT>";
echo "<SELECT NAME=".$inName."Day>\n";
for($currentDay=1; $currentDay <= 31; $currentDay++)
{
echo "<OPTION VALUE=\"$currentDay\"";
if(intval(date("d", $useDate))==$currentDay)
{
echo " SELECTED";
}
echo ">$currentDay\n";
}
echo "</SELECT>";
echo "<SELECT NAME=".$inName."Year>\n";
$startYear = date("Y", $useDate);
if($startYear < 1997)
{
$startYear = date("Y");
}
for($currentYear = $startYear-1; $currentYear <= $startYear+2;$currentYear++) {
echo "<OPTION VALUE=\"$currentYear\"";
if(date("Y", $useDate)==$currentYear)
{
echo " SELECTED";
}
echo ">$currentYear\n";
} echo "</SELECT>";}
<?php
/* Connecting, selecting database */
$link = mysql_connect("XXXXX", "XXXX", "XXXXXX")
or die("Could not connect : " . mysql_error());
echo "";
mysql_select_db("contact_management_system") or die("Could not select database");
?>
<body onLoad="focus()">
<form method="post" action="processpeople.html">
<table width="100%" border="0">
<tr>
<td width="17%"><font face="Times New Roman, Times, serif"><strong>Salutation</strong></font></td>
<td width="27%"><font face="Times New Roman, Times, serif">
<select name="salutation" style="color: #000000;
background-color: #ADD8E6">
<option>Mr</option>
<option>Mrs</option>
<option>Ms</option>
<option>Miss</option>
<option>Prof</option>
<option>Dr</option>
</select>
</font></td>
<td width="27%"><font face="Times New Roman, Times, serif"><strong>Telephone</strong></font></td>
<td width="29%"><input name="telephone" type="text"style="color: #000000;
background-color: #ADD8E6" size="20" maxlength="20">
</td>
</tr>
<tr>
<td><font face="Times New Roman, Times, serif"><strong>First Name</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="firstname" type="text" size="20"style="color: #000000;
background-color: #ADD8E6">
</font></td>
<td><font face="Times New Roman, Times, serif"><strong>Mobile</strong></font></td>
<td><input name="mobile" type="text"style="color: #000000;
background-color: #ADD8E6" size="20" maxlength="20"></td>
</tr>
<tr>
<td><font face="Times New Roman, Times, serif"><strong>Surname</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="surname" type="text" size="15"style="color: #000000;
background-color: #ADD8E6">
</font></td>
<td><font face="Times New Roman, Times, serif"><strong>Fax</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="fax" type="text"style="color: #000000;
background-color: #ADD8E6" size="20" maxlength="20">
</font></td>
<tr>
<td><font face="Times New Roman, Times, serif"><strong>Organisation</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="organisation" type="text" size="20"style="color: #000000;
background-color: #ADD8E6">
<td><font face="Times New Roman, Times, serif"><strong>E-mail</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="email" type="text"style="color: #000000;
background-color: #ADD8E6" size="25" maxlength="50">
</font></td>
</font></tr>
<tr>
<td><font face="Times New Roman, Times, serif"><strong>Role</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="role" type="text" size="25"style="color: #000000;
background-color: #ADD8E6">
</font></td>
<td><font face="Times New Roman, Times, serif"><strong>Date of Last Contact</strong></font></td>
<td><font face="Times New Roman, Times, serif"> <?php DateSelector("unique_name_1",""); ?>
</font></td>
</tr>
<tr>
<td><font face="Times New Roman, Times, serif"><strong>Address (1)</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="address1" type="text" size="20" style="color: #000000;
background-color: #ADD8E6">
</font></td>
<td><font face="Times New Roman, Times, serif"><strong>Date Contact Again</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<?php DateSelector("unique_name_2",""); ?>
</font></td>
</tr>
<tr>
<td><font face="Times New Roman, Times, serif"><strong>Address(2) </strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="address2" type="text" size="20"style="color: #000000;
background-color: #ADD8E6">
</font></td>
<td><font face="Times New Roman, Times, serif"><strong>OID</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="org_id" type="text"style="color: #000000;
background-color: #ADD8E6" size="5">
</font></td>
</tr>
<tr>
<td><font face="Times New Roman, Times, serif"><strong>City</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="city" type="text" size="20"style="color: #000000;
background-color: #ADD8E6">
</font></td>
<td><font face="Times New Roman, Times, serif"> </font></td>
<td> </td>
</tr>
<tr>
<td height="43"><font face="Times New Roman, Times, serif"><strong>Post
Code</strong></font></td>
<td><font face="Times New Roman, Times, serif">
<input name="postcode" type="text" size="7"style="color: #000000;
background-color: #ADD8E6">
</font></td>
<td><font face="Times New Roman, Times, serif"> </font></td>
<td> </td>
</tr>
</table>
<input type="submit" name="submit" value = "Enter Information">
</form>
<p>
Posted: Mon May 23, 2005 6:56 am
by JayBird
you haven't copied and pasted my code properly
You have an opening <?php tag then another one before your Database connection
Posted: Mon May 23, 2005 10:02 am
by mohson
Thanks Pimptastic , I should really have noticed that, Ive sorted it and my form now appears with the three select boxes.
Theres only one problem when I add a new record and select the dates the dates are not inserted into the database, if I go into the mysql table and check the record the date value is stored as 0000-00-00 im assuming this is because I know longer have the "fieldname" in the form as this has been replaced by thedate selector??
any ideas why this is happening??
Posted: Mon May 23, 2005 10:15 am
by JayBird
yes, you will now be recieving 3 values from the form....you will need to do whatever you need to with these value to insert them into the field in the DB.
Posted: Mon May 23, 2005 10:37 am
by mohson
o/k So I will be receiving 3 values and then I need to make these values combine to make one value - any advice on how to do this or any tutorials you could reccomend.
Posted: Mon May 23, 2005 10:41 am
by JayBird
research "concatenation"....should give you some clues.
It is really easy
Posted: Tue May 24, 2005 4:39 am
by mohson
Hi Pimp,
I looked into concatenation 'the process of joining strings together' and as you said its pretty simple. this is what I grasp of the concept
Code: Select all
<?php
$var1 = "mohson ";
$var2 = "khan";
$var3 = $var1.$var2;
echo "$var3";
echo "$var1";
echo "$var2";
?>
which outpts:
mohson khanmohson khan
Ok so now to stage two where do I apply this concept,
My insert statement is on a seperate script do I apply this code there?
for example:
Code: Select all
<?php
/* Connecting, selecting database */
$link = mysql_connect("xxxxx", "xxx", "xxxx")
or die("Could not connect : " . mysql_error());
mysql_select_db("contact_management_system",$link) or die("Could not select database");
$sql = "INSERT INTO people (person_id, salutation, firstname, surname, organisation, role, address1, address2, city, postcode, telephone, mobile, fax, dateoflastcontact, datecontactagain, notes, email, org_id )
VALUES ('$person_id','$salutation','$firstname','$surname', '$organisation', '$role', '$address1', '$address2', '$city', '$postcode', '$telephone', '$mobile', '$fax', '$dateoflastcontact', '$datecontactagain', '$notes', '$email','$org_id')";
$result = mysql_query($sql,$link) or die ( mysql_error($link));;
header ("location: http://www.soi.city.ac.uk/organisation/ ... eople.html");
?>
the above is my insert statement, I think I need to create three variables for the date field and then use procedure I described above combine the string of date and insert it into the database.
Is this correct or do I need to carry out the concatenation in the form which is shown in my previous posts?
Posted: Tue May 24, 2005 4:45 am
by JayBird
what kind of field do you want to store the data in?
Posted: Tue May 24, 2005 4:54 am
by mohson
the 'dateoflastcontact' and 'datecontactagain' fields are date fields
Posted: Tue May 24, 2005 5:00 am
by JayBird
okay, you now have all the information to be able to do this yourself.
1) Take a look at what information is being passed from the drop down date selectors
2) Take a look at how the database stores the date in the database.
3) Use you new found knowledge of concat, and work your magic
Mark
Posted: Tue May 24, 2005 5:47 am
by mohson
Thanks for your help and information Mark, eventhough I am not a good programmer I am determined to be able to do this myself, AND I WILL DO IT!!!
some questions firstly:
where do I do the coding? on my form script or my Insert script?
1) Take a look at what information is being passed from the drop down date selectors
from what I can see the dateselector is passing the following variables;
I expected there to be three variables one for Year,Day and MONTH