SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    canada
    Posts
    153
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    passing javascript variable to php - working with a calendar script

    Hello,

    I implemented a javascript calendar into my script but i cant seem to extract the end user selection to save the selection into the database.

    basically need to find a way to pass the calendar selection to a varialbe that php can understand.


    PHP Code:
    <?php
    include('connectionv2.php');
    if(isset(
    $_POST['btn_test']))
    { echo 
    $_POST['hidden1']; 
       echo 
    '<br />';
       
    $db Connection::getConnection();
        try
        {    
    $STH $db->prepare("INSERT INTO tbl_servicerequest (projectexpire ) VALUES (:stamp )");
            
    $STH->bindParam(':stamp'$_POST['hidden1']);
            
    $STH->execute();
        }
        catch(
    PDOException $e)   {    echo 'ERROR: ' $e->getMessage(); }  
    }
    ?>

    <html>
        <head>
            <meta charset="utf-8" />
            <title>jQuery UI Datepicker </title>
            <script type="text/javascript" src="jquery.js"></script>
            <script type="text/javascript" src="jquery-ui-1.10.3/ui/jquery-ui.js"></script>
            <link rel="stylesheet" type="text/css" href="jquery-ui-1.10.3/themes/base/jquery-ui.css" />

            <script type="text/javascript">
               $(document).ready(function(){                
                     $("#date").datepicker({
                        showButtonPanel: true,
                        minDate: '0M',
                        maxDate: '+90D',    
                        dateFormat: "d-MM-yy",    
                       });
                       var dateString = $('#date').val();
                        var timestamp = Date.parse(dateString).getTime()/1000;                    
                        document.getElementById('#hidden1').value = timestamp;
               });
            </script>
        </head>
        <body>
        <div> 
            <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
                <p> Enter expire date: <br />
                <input type="text" name="date" id="date"/></p>    
                <input type="hidden" name="hidden1" id="hidden1"  />            
                <button type="submit" name="btn_test" value="">Test Button</button>
            </form>
        </div>
        </body>
    </html>

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,831
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Add a hidden field to your form and have the JavaScript update the value of that field.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    canada
    Posts
    153
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Add a hidden field to your form and have the JavaScript update the value of that field.
    Hi felgall,

    there is a hidden field in the script. i thinks there might be something wrong with the way im passing the variable to the hidden field. any ideas on what im doing wrong?

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,831
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    When you are looking at JavaScript interacting with HTML it is easier if you actually look at the HTML that is in the page and not the PHP that generates that HTML.

    Often the HTML that is actually being generated is not what you expect and that is why the JavaScript doesn't interact properly. Even where the HTML is what you expect it makes it easier to see what is happening without the PHP.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    738
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    As mentioned, the datepicker is just updating the form field value.
    Where you grab the value to use in php is when you post the form.
    Printing your POST will show what is being passed. And this to the top of your script to test.
    PHP Code:
    echo "<pre>";
    print_r($_POST); 
    echo 
    "</pre>"
    You should see something like this when you post your form.
    Array
    (
    [date] => 2013-06-05
    [hidden1] =>
    [btn_test] =>
    )
    IF you don't see the form field value being updated with your calendar then your datepicker script is not setup right. That is a different issue then getting the results from the form.

  6. #6
    SitePoint Addict Shaydez's Avatar
    Join Date
    Jul 2006
    Location
    Boca Raton, Florida
    Posts
    356
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    at glance it looks right to me.

    are you sure timestamp value is not null?
    Sr. Website Developer and Internet Marketing
    www.CarlosJa.com Note: If anyone
    needs to get ahold of me please feel free to email me through
    my site. Apparently i missed quite a few private messages.


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
  •