Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: How to pass a date to mysql using Jason's Date Input Calendar

  1. #1
    Join Date
    Jan 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to pass a date to mysql using Jason's Date Input Calendar

    1) Script Title:

    Jason's Date Input Calendar

    2) Script URL (on DD):

    http://www.dynamicdrive.com/dynamici...oncalendar.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!

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,926
    Thanks
    43
    Thanked 3,192 Times in 3,155 Posts
    Blog Entries
    12

    Default

    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/...s/ddws/24.html

    For more help on that post in the:

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

    forum here.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    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 Code:
    <?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.
    Last edited by rangana; 01-08-2009 at 02:03 PM. Reason: noted from the typo
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,926
    Thanks
    43
    Thanked 3,192 Times in 3,155 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by rangana View Post
    Here's a basic example on how to make this work using POST method:
    Code:
    <?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'?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    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.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  6. #6
    Join Date
    Jan 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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?

  7. #7
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    One thing, you should rectify the inputs.

    Add highlighted:
    Code:
    $sQuery = "INSERT INTO $table_name ($col_name) VALUES ('mysql_real_escape_string($inp)')"; // Insert query
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  8. #8
    Join Date
    Jan 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    It worked like a charm!!! Thanks a bunch!!!!

  9. #9
    Join Date
    Feb 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.
    Code:
    <!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

  10. #10
    Join Date
    Feb 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok I am not getting that error anymore I have just moved isset statement before setting the variables.
    Code:
    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •