inserting dates from php/html form

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

darktick13
Forum Newbie
Posts: 6
Joined: Wed Aug 10, 2005 11:57 pm

inserting dates from php/html form

Post 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...
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Type in a date in a text box or using 3 selects - date, month and year ?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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);
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

could always use a date picker :)
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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;
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd advise to not rely on the submit button being in the submission.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

we could use the 'onblur' event. But I would like to know the problem with the submit button feyd.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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>
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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;
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

escape()

I think it's silly to do a submission immediately though.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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;
        
    }
Post Reply