Page 1 of 2
inserting dates from php/html form
Posted: Thu Aug 11, 2005 12:15 am
by darktick13
I can't figure out how to get a date from a form to the mysql database... i don't want it to be "today" or "now" i want the user to be able to type in a date and insert it into a field in the database. please help been looking for 2 days...
Posted: Thu Aug 11, 2005 12:25 am
by anjanesh
Type in a date in a text box or using 3 selects - date, month and year ?
Posted: Thu Aug 11, 2005 5:36 am
by timvw
Code: Select all
<form>
<input type='text' name='date' value='1995-12-15'/>
</form>
Code: Select all
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);
mysql_connect(...) or trigger_error('Could not connect to database.', E_USER_ERROR);
mysql_select_db(...) or trigger_error(mysql_error(), E_USER_ERROR);
$date = mysql_real_escape_string($POST['date']);
$query = "INSERT INTO table VALUES ('$date');
$rs = mysql_query($query) or trigger_error(mysql_error(), E_USER_ERROR);
Posted: Thu Aug 11, 2005 7:52 am
by CoderGoblin
anjanesh wrote:Type in a date in a text box or using 3 selects - date, month and year ?
I prefer to have a single field, with the lebel showing the expected date format. Three select boxes means addition mouse/key presses by the user. One means more validation but keeps the use happy. Once validation has been worked out it can always be the same.
Posted: Thu Aug 11, 2005 8:54 am
by feyd
could always use a date picker

Posted: Fri Aug 12, 2005 6:18 am
by raghavan20
I think this might help you
Code: Select all
<!-- getDate.php-->
<html>
<head>
<script language = 'javascript'>
function validateForm(form){
var myDate =form.txtDate.value;
re = new RegExp(/^(\d{2}\/){2}(\d){4}$/i);
valid = myDate.match(re);
if (valid){
return true;
}else{
alert("The format of date is dd/mm/yyyy. Use leading zeros for day and month!!!");
document.frmTest.txtDate.focus();
return false;
}
}
</script>
</head>
<form name = 'frmTest' method = 'get' action = '' onsubmit = 'return validateForm(this)'>
<input type = 'text' name = 'txtDate' value = 'dd/mm/yyyy' />
<input type = 'submit' name = 'subDate' value = 'Submit' />
</form>
<?php
if ($_GET["subDate"] == "Submit"){
$myDate = $_GET["txtDate"];
$tempArray = explode("/", $myDate);
$day = $tempArray[0];
$month = $tempArray[1];
$year = $tempArray[2];
echo $myDate."<br />".$day."<br />".$month."<br />".$year;
}
?>
Posted: Fri Aug 12, 2005 6:20 am
by feyd
I'd advise to not rely on the submit button being in the submission.
Posted: Fri Aug 12, 2005 6:30 am
by raghavan20
we could use the 'onblur' event. But I would like to know the problem with the submit button feyd.
Posted: Fri Aug 12, 2005 6:41 am
by feyd
Run the following in IE for a simple example of why:
Code: Select all
<html>
<head>
<title>feyd's worldly examples</title>
</head>
<body>
<form method="get">
Fill the field and press enter while still inside: <input type="text" name="example" value="" />
<input type="submit" name="submitButton" value="Submit" />
</form>
</body>
</html>
Posted: Fri Aug 12, 2005 6:46 am
by raghavan20
do you mean to say that an 'enter hit' inside the text box submits the value alone not the submit value. Actually, one has to click on the submit button for the value of the submit to be passed on to the url. I have see this character before.
Then I think there is a command like (form.submit()) which should be used on the onclick event of the text box.
Posted: Fri Aug 12, 2005 6:49 am
by anjanesh
I had this issue before - if a form has one textbox and 1 submit button, only the textbox gets sent as argument.
If you have another control or a hidden variable I guess all 3 are sent across.
Posted: Fri Aug 12, 2005 6:53 am
by feyd
what for? just look for a field that always should be in the submission.. like the date field, since that's the topic of conversation.
I don't see a reason why a submission should take place automatically, plus the original poster didn't ask for this..
Posted: Fri Aug 12, 2005 7:09 am
by raghavan20
The code works and it used onblur event but I have not encoded the url where I pass the date paramater with slashes.
I hope feyd would help with url encoding and decoding.
Code: Select all
<html>
<head>
<script language = 'javascript'>
function validateDate(myDate){
re = new RegExp(/^(\d{2}\/){2}(\d){4}$/i);
valid = myDate.match(re);
if (valid){
window.location = "getDate1.php?date=" + myDate;//this needs url encoding
}else{
alert("The format of date is dd/mm/yyyy. Use leading zeros for day and month!!!");
document.txtDate.focus();
}
}
</script>
</head>
<input type = 'text' name = 'txtDate' value = 'dd/mm/yyyy' onblur = 'validateDate(this.value)' />
<?php
if (isset($_GET["date"])){
$myDate = $_GET["date"];
$tempArray = explode("/", $myDate);
$day = $tempArray[0];
$month = $tempArray[1];
$year = $tempArray[2];
echo $myDate."\n".$day."\n".$month."\n".$year;
}
?>
Posted: Fri Aug 12, 2005 7:18 am
by feyd
escape()
I think it's silly to do a submission immediately though.
Posted: Fri Aug 12, 2005 10:28 am
by timvw
Here is an excerpt from my date class..
Code: Select all
function getInternalDate ($input)
// convert date from external format (as input by user)
{
// look for d(d)?m(m)?y(yyy) format
$pattern = '(^[0-9]{1,2})' // 1 or 2 digits
. '([^0-9a-zA-Z])' // not alpha or numeric
. '([0-9]{1,2})' // 1 or 2 digits
. '([^0-9a-zA-Z])' // not alpha or numeric
. '([0-9]{1,4}$)'; // 1 to 4 digits
if (ereg($pattern, $input, $regs)) {
$result = $this->verifyDate($regs[1], $regs[3], $regs[5]);
return $result;
}
// look for d(d)?MMM?y(yyy) format
$pattern = '(^[0-9]{1,2})' // 1 or 2 digits
. '([^0-9a-zA-Z])' // not alpha or numeric
. '([a-zA-Z]{1,})' // 1 or more alpha
. '([^0-9a-zA-Z])' // not alpha or numeric
. '([0-9]{1,4}$)'; // 1 to 4 digits
if (ereg($pattern, $input, $regs)) {
$result = $this->verifyDate($regs[1], $regs[3], $regs[5]);
return $result;
}
// look for d(d)MMMy(yyy) format
$pattern = '(^[0-9]{1,2})' // 1 or 2 digits
. '([a-zA-Z]{1,})' // 1 or more alpha
. '([0-9]{1,4}$)'; // 1 to 4 digits
if (ereg($pattern, $input, $regs)) {
$result = $this->verifyDate($regs[1], $regs[2], $regs[3]);
return $result;
}
// look for MMM?d(d)?y(yyy) format
$pattern = '(^[a-zA-Z]{1,})' // 1 or more alpha
. '([^0-9a-zA-Z])' // not alpha or numeric
. '([0-9]{1,2})' // 1 or 2 digits
. '([^0-9a-zA-Z])' // not alpha or numeric
. '([0-9]{1,4}$)'; // 1 to 4 digits
if (ereg($pattern, $input, $regs)) {
$result = $this->verifyDate($regs[3], $regs[1], $regs[5]);
return $result;
}
// look for MMMddyyyy format
$pattern = '(^[a-zA-Z]{1,})' // 1 or more alpha
. '([0-9]{2})' // 2 digits
. '([0-9]{4}$)'; // 4 digits
if (ereg($pattern, $input, $regs)) {
$result = $this->verifyDate($regs[2], $regs[1], $regs[3]);
return $result;
}
// look for yyyy?m(m)?d(d) format
$pattern = '(^[0-9]{4})' // 4 digits
. '([^0-9a-zA-Z])' // not alpha or numeric
. '([0-9]{1,2})' // 1 or 2 digits
. '([^0-9a-zA-Z])' // not alpha or numeric
. '([0-9]{1,2}$)'; // 1 to 2 digits
if (ereg($pattern, $input, $regs)) {
$result = $this->verifyDate($regs[5], $regs[3], $regs[1]);
return $result;
}
// look for ddmmyyyy format
$pattern = '(^[0-9]{2})' // 2 digits
. '([0-9]{2})' // 2 digits
. '([0-9]{4}$)'; // 4 digits
if (ereg($pattern, $input, $regs)) {
$result = $this->verifyDate($regs[1], $regs[2], $regs[3]);
return $result;
}
// look for yyyy?MMM?d(d) format
$pattern = '(^[0-9]{4})' // 4 digits
. '([^0-9a-zA-Z])' // not alpha or numeric
. '([a-zA-Z]{1,})' // 1 or more alpha
. '([^0-9a-zA-Z])' // not alpha or numeric
. '([0-9]{1,2}$)'; // 1 to 2 digits
if (ereg($pattern, $input, $regs)) {
$result = $this->verifyDate($regs[5], $regs[3], $regs[1]);
return $result;
}
return false;
} // getInternalDate
function verifyDate($day, $month, $year)
{
// convert alpha month to digits
if (eregi('([a-z]{3})', $month)) {
$month = ucfirst(strtolower($month));
if (!$month = array_search($month, $this->monthalpha)) {
return false;
}
}
// ensure that year has 4 digits
if (strlen($year) == 1) {
$year = '200' . $year;
}
if (strlen($year) == 2) {
$year = '20' . $year;
} // if
if (strlen($year) == 3) {
$year = '2' . $year;
}
if (!checkdate($month, $day, $year)) {
return false;
} else {
if (strlen($day) < 2) {
$day = '0' . $day; // add leading zero
}
if (strlen($month) < 2) {
$month = '0' . $month; // add leading zero
}
$this->internaldate = $year . '-' . $month . '-' . $day;
return $this->internaldate;
}
return;
}