PDA

View Full Version : How to pass a date to mysql using Jason's Date Input Calendar



duckofprey
01-07-2009, 02:51 AM
1) Script Title:

Jason's Date Input Calendar

2) Script URL (on DD):

http://www.dynamicdrive.com/dynamicindex7/jasoncalendar.htm

3) Describe problem:

I can't figure out how to pass a date to a mysql database.

This is probably really easy to do... but as I'm a total Javascript noob... ;-) I have everything set up correctly to the point where the user selects the date. But I can't figure out how to send this value to a mysql database. This is what I have so far:

<head>

<script type="text/javascript" src="calendarDateInput.js">

/***********************************************
* Jason's Date Input Calendar- By Jason Moon http://calendar.moonscript.com/dateinput.cfm
* Script featured on and available at http://www.dynamicdrive.com
* Keep this notice intact for use.
***********************************************/

</script>

</head>

<snip>

<script>
DateInput('fromdate', true, 'YYYY-MM-DD');
</script>
<input type="button" onClick="alert(this.form.fromdate.value)" value="Show date value passed">

<snip>

As you can see, I gave it the field name "fromdate" and formated the date so that mysql likes it. It works perfectly to this point. From here, the user would hit the "Submit" button and the fromdate needs to be stored to the database. It's apparently stored in a hidden field... but I have no idea how to access it. Please help!

Thanks in advance!

jscheuer1
01-07-2009, 04:45 PM
I don't know how MySQL works, but when you submit the form, it is either post or get, and has an action. None of this matters from the point of view of the script, but in either the post or get data will be:


fromdate=the_date_value

Where the_date_value is the value of the date. It is the action of the form which determines what is done with this data.

So, the question really is, do you know how to pass the get or post data from a form to MySQL via the action attribute of a form?

This page appears to address this latter question:

http://dev.mysql.com/tech-resources/articles/ddws/24.html

For more help on that post in the:

http://www.dynamicdrive.com/forums/forumdisplay.php?f=19

forum here.

rangana
01-08-2009, 12:16 PM
The script creates a hidden textbox, with the name equivalent to the first argument passed on DateInput() function.

With that said, you can get the value of the date (as what John said) via get/post method.

Here's a basic example on how to make this work using POST method:


<?php

/***********************
Start of DB details
***********************/
$user = "db_user"; // DB Username
$pass = "db_pass"; // DB pass
$name = "db_name"; // DB Name
$host = "localhost"; // DB Host
$webmaster = "webmaster@site.com" ; // Set the webmaster's email here

///// No need to edit beyond this line
$db=mysql_connect($host,$user,$pass) or die("Connection refused. Please check your database or contact the <a href='mailto:".$webmaster."'>webmaster</a>."); // Establish a connection
mysql_select_db($name,$db); // Select DB
/***********************
End of DB details
***********************/


$inp = $_POST['fromdate']; // Get the textbox that holds the date
$table_name='table_name'; // Set the name of your table
$col_name = 'fromdate'; // Set the column that will hold the dates

if(isset($_POST['submit']))
{
$sQuery = "INSERT INTO $table_name ($col_name) VALUES ('$inp')"; // Insert query
mysql_query($sQuery) or die(mysql_error()); // Do the query
}
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<script>DateInput('fromdate', true, 'DD-MON-YYYY')</script>
<input type="submit" name="submit" value="Submit">
</form>


For the GET method, you could use $_GET['querystring'].

Hope that helps.

jscheuer1
01-08-2009, 01:54 PM
Here's a basic example on how to make this work using POST method:


<?php

/***********************
Start of DB details
***********************/
$user = "db_user"; // DB . . .
. . . die(mysql_error()); // Do the query
}
?>

<form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<script>DateInput('fromdate', true, 'DD-MON-YYYY')</script>
<input type="submit" name="submit" value="Submit">
</form>


If this is via $_POST['fromdate'], shouldn't the method of the form be 'post', not 'get'?

rangana
01-08-2009, 03:04 PM
Yes, it should be post. Thanks for noticing it. I was testing the get method at my end when uploaded the script.

I've edited it already.

duckofprey
01-09-2009, 02:16 AM
Thank you so much rangna and jscheuer1!! I'm trying your suggestions out now!! :-D

Just a question though.. again, this is a javascript noob question... i was under the impression that a "hidden textbox" was somewhere in javascript... that's why i did not think about using the post and get methods... i thought i need so somehow pass something directly from javascript to the database... am i correct in thinking that this was an error?

rangana
01-09-2009, 02:20 AM
One thing, you should rectify the inputs.

Add highlighted:


$sQuery = "INSERT INTO $table_name ($col_name) VALUES ('mysql_real_escape_string($inp)')"; // Insert query

duckofprey
01-09-2009, 04:24 AM
It worked like a charm!!! Thanks a bunch!!!! :D

Glamsy
02-17-2009, 11:00 PM
Hi,

I am using the same calendar to input a date into my database, But I get the error message "Notice: Undefined index: date in C:\wamp\www\nope.php on line 29" and if I still click on submit it only enters "0000-00-00"into the database.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Bookings</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" src="calendarDateInput.js">

/***********************************************
* Jason's Date Input Calendar- By Jason Moon http://calendar.moonscript.com/dateinput.cfm
* Script featured on and available at http://www.dynamicdrive.com
* Keep this notice intact for use.
***********************************************/

</script>
</head>
<body>
<?php
$DBConnect = @mysql_connect("localhost", "root");
if (!$DBConnect)
{die("<p>The database server is not available</p>");
}

$dbselect = @mysql_select_db("hairsalon");

if (!$dbselect){
die("<p>The database is not available</p>");
}

$inp = $_POST['date']; // Get the textbox that holds the date
$table_name='bookings'; // Set the name of your table
$col_name = 'booking_date'; // Set the column that will hold the dates

if(isset($_POST['submit']))
{
$sQuery = "INSERT INTO $table_name($col_name) VALUES ('mysql_real_escape_string($inp)')"; // Insert query
mysql_query($sQuery) or die(mysql_error()); // Do the query
}


?>

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<script>DateInput('date', true, 'YYYY-MM-DD')</script>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>


I was wondering if anyone could help me with this,Its driving me crazy :-(

Thanks

Glamsy
02-17-2009, 11:31 PM
Ok I am not getting that error anymore I have just moved isset statement before setting the variables.

if(isset($_POST['submit']))
{

$inp = ($_POST['date']); // Get the textbox that holds the date
$table_name='bookings'; // Set the name of your table
$col_name = 'booking_date'; // Set the column that will hold the dates


But in the database date field has still got zeros instead of the date

trochia
04-04-2009, 04:54 PM
Hi Thread!! Thx for this info, but I'm trying to desgin a form that has (4) calenders showing in order to gather 4 seperate dates.

I already have my form in place, (using simple TEXTBOX entry) along with the DB and it posts fine....but I am new to this calender (and fairly new to javascript also, let alone passing values from it...to php & mysql) and would like to use it, as to reduce entry errors (from the user side).

If anyone is still reading this thread, I'll then go ahead and post my code, and also the URL's I am am testing at. Thank's for reading.. !!

trochia
04-04-2009, 07:00 PM
OK, As I move along (pulling my hair out), here is my present sql in db for testing.

--
-- Table structure for table `dateloaded`
--

CREATE TABLE IF NOT EXISTS `dateloaded` (
`id` int(10) unsigned NOT NULL auto_increment,
`email` text NOT NULL,
`loadEarly` text NOT NULL,
`loadLate` text NOT NULL,
`deliverEarly` text NOT NULL,
`deliverLate` text NOT NULL,
`dateEntered` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_2` (`id`),
KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='None Yet' AUTO_INCREMENT=3 ;


But I am thinking...I am better off storing as date() timestamp for sure ??? As I will have the need later...for things to "not be displayed" and expire from view...once a date (or time/hour) has passed, or also be able to search within a date range in bewtween EARLY or LATE of either...which I want to add also.

Am I making sense to anyone? (laugh)

This is my exisiting form:
http://www.tishllc.com/calendar/form1.html

And the ultimate goal, along the lines of : http://www.tishllc.com/calendar/formentry.htm

Really in effect, a reservation system ( for open machining time )

http://www.amtrak.com/servlet/ContentServer?pagename=Amtrak/HomePage

I'd really love to have the "hours" to be used also in my app, as again...it's like a booking or reservation system....and like "your time", you base it on hours...etc..

Thank's!!