inserting dates from php/html form
Moderator: General Moderators
-
darktick13
- Forum Newbie
- Posts: 6
- Joined: Wed Aug 10, 2005 11:57 pm
inserting dates from php/html form
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...
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);- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
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.anjanesh wrote:Type in a date in a text box or using 3 selects - date, month and year ?
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
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;
}
?>- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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>- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
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.
Then I think there is a command like (form.submit()) which should be used on the onclick event of the text box.
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
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.
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;
}
?>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;
}