Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,072
    Thanks
    26
    Thanked 0 Times in 0 Posts

    When is Form Value populated?

    When is the Form Value below populated?
    PHP Code:
        <input type="hidden" name="createAccount_finish" value="<?php echo time(); ?>" />

    Is it populated when the HTML is output to the screen, or when my Form is submitted?


    (I am trying to use the code above to check how long it takes a user to complete a Form, but when I submit my Form, the Start and Finish times come up identical?!)

    Sincerely,


    Debbie

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,916
    Thanks
    15
    Thanked 227 Times in 227 Posts
    Don't know why yours isn't working. I made up this simple test page and it works very well.
    PHP Code:
    <?php
     
    @session_start();
     if(isset(
    $_GET['mytime']))
    {
    echo 
    time(true)-$_GET['mytime'];
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <title>javascript &amp; php</title>
    </head>
    <body>
    <form method="get" action="./index.php">
    <input type="hidden" name="mytime" value="<?php echo time(true); ?>" />
    <input type="submit" value="Submit Me!" />
    </form>
    </body>
    </html>
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #3
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,072
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Here is a stripped down version of my code...

    PHP Code:
    <?php
        
    // Initialize Session.
        
    session_start();

        
    // Set Start Time.
        
    if (!isset($_SESSION['createAccount_start'])){
            
    $_SESSION['createAccount_start'] = time();
        }


        
    // HANDLE FORM.
        
    if ($_SERVER['REQUEST_METHOD']=='POST'){
            
    // Form was Submitted (Post).


            //NEW
            
    var_dump($_POST);
            
    $formCompletionTime = ($_POST['createAccount_finish'] - $_SESSION['createAccount_start']);
            echo 
    '<p>$_SESSION[createAccount_start] = ' $_SESSION['createAccount_start'] . '</p>';
            echo 
    '<p>$_POST[createAccount_finish] = ' $_POST['createAccount_finish'] . '</p>';
            echo 
    '<p>$formCompletionTime = ' $formCompletionTime '</p>';
            unset(
    $_SESSION['createAccount_start']);
            exit();

        }else{
            
    // Form was not Submitted (Get).
            // Drop through to display Form.

        
    }//End of HANDLE FORM
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>
        <!-- Form -->
        <input type="hidden" name="createAccount_finish" value="<?php echo time(); ?>" />

        <!-- Submit Form -->
        <input type="submit" name="createAccount" class="button" value="Create Account"/>
    </body>
    </html>
    As mentioned above, the Start and Finish Times are coming out exactly the same.

    I've gotta be missing something super simple here?!

    Sincerely,


    Debbie

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,313
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by doubledee View Post

    PHP Code:
    <?php
        
    // Set Start Time.
        
    if (!isset($_SESSION['createAccount_start'])){
            
    $_SESSION['createAccount_start'] = time();
        }

            
    $formCompletionTime = ($_POST['createAccount_finish'] - $_SESSION['createAccount_start']);
    ?>

        <!-- Form -->
        <input type="hidden" name="createAccount_finish" value="<?php echo time(); ?>" />
    Yes you are missing something super simple.

    You are printing the finish time into the form at the time you output the form. The finish time is therefore the same as the start time (might differ by a few microseconds but thats it).

    Then you're trying to take the start time away from the finish time - which is the same.

    Instead of putting the time into the form with an echo, just get the time from the system when you process the form instead (IE when it's been submitted).

    In response to your original question, it's populated when the php code runs - in other words, when the form is output and NOT when it's submitted. PHP can't run on the clients computer.
    Last edited by tangoforce; 10-06-2013 at 07:39 PM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,072
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    Yes you are missing something super simple.

    You are printing the finish time into the form at the time you output the form. The finish time is therefore the same as the start time (might differ by a few microseconds but thats it).

    Then you're trying to take the start time away from the finish time - which is the same.

    Instead of putting the time into the form with an echo, just get the time from the system when you process the form instead (IE when it's been submitted).

    In response to your original question, it's populated when the php code runs - in other words, when the form is output and NOT when it's submitted. PHP can't run on the clients computer.
    I have been suspecting what you just said, Tango.

    So I'm still not sure how to do what you suggested...

    How do I assign a time() when the Form is submitted?

    Sincerely,


    Debbie

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,313
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Well when your script processes the forms submitted data, get the time from time() like this:

    PHP Code:
    <?php
        
    // Initialize Session.
        
    session_start();

        
    // Set Start Time.
        
    if (!isset($_SESSION['createAccount_start'])){
            
    $_SESSION['createAccount_start'] = time();
        }


        
    // HANDLE FORM.
        
    if ($_SERVER['REQUEST_METHOD']=='POST'){
            
    // Form was Submitted (Post).

                   
    $Submitted time();

                   
    //Now use $Submitted as the time the form was submitted
                  // and use it in your calculations.

    Job done
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #7
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,072
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    Well when your script processes the forms submitted data, get the time from time() like this:

    PHP Code:
    <?php
        
    // Initialize Session.
        
    session_start();

        
    // Set Start Time.
        
    if (!isset($_SESSION['createAccount_start'])){
            
    $_SESSION['createAccount_start'] = time();
        }


        
    // HANDLE FORM.
        
    if ($_SERVER['REQUEST_METHOD']=='POST'){
            
    // Form was Submitted (Post).

                   
    $Submitted time();

                   
    //Now use $Submitted as the time the form was submitted
                  // and use it in your calculations.

    Job done

    I did this, but I think it is hokey...

    PHP Code:
        // HANDLE FORM.
        
    if ($_SERVER['REQUEST_METHOD']=='POST'){
            
    // Form was Submitted (Post).

            
    $_POST['createAccount_finish'] = time();

            
    //NEW
            
    var_dump($_POST);

            
    $formCompletionTime = ($_POST['createAccount_finish'] - $_SESSION['createAccount_start']);

            echo 
    '<p>$_SESSION[createAccount_start] = ' date('Y-m-d g:i:sa'$_SESSION['createAccount_start']) . '</p>';
            echo 
    '<p>$_POST[createAccount_finish] = ' date('Y-m-d g:i:sa'$_POST['createAccount_finish']) . '</p>';
            echo 
    '<p>$formCompletionTime = ' gmdate("H:i:s"$formCompletionTime) . '</p>';
            unset(
    $_SESSION['createAccount_start']);
            exit(); 

    I guess it is bad form to assign something to $_POST like that right?

    Also, based on what you said above, I guess that means I can remove this new hidden field in my Form, right?

    Code:
    <!-- NEW -->
    	<!-- Form -->
    	<input type="hidden" name="createAccount_finish" value="<?php //echo time(); ?>" />
    Sincerely,


    Debbie

  • #8
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,916
    Thanks
    15
    Thanked 227 Times in 227 Posts
    The beginning and ending times don't need to be a part of $_SESSION, which has a memory like an Elephant. You only want the difference between the time the form was loaded onto the page and the time the form processing started, just the way I had it. If you want to make that difference a part of $_SESSION, for instance to put it in a database, then do it after the difference is calculated.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,313
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by DrDOS View Post
    The beginning and ending times don't need to be a part of $_SESSION, which has a memory like an Elephant.
    What are you on about? Have you ever looked inside a session file? - There's pretty much nothing there except what you've stored in it. It's purely a serialized array.

    Quote Originally Posted by DrDOS View Post
    You only want the difference between the time the form was loaded onto the page and the time the form processing started
    Indeed but I would personally store the output time in the session or the database. You don't want to expose any possibly sensitive information / weakness to the end user than you need to.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #10
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,072
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by DrDOS View Post
    The beginning and ending times don't need to be a part of $_SESSION, which has a memory like an Elephant. You only want the difference between the time the form was loaded onto the page and the time the form processing started, just the way I had it. If you want to make that difference a part of $_SESSION, for instance to put it in a database, then do it after the difference is calculated.
    If I don't store the Start Time in the Session, then when my Form gets submitted and reloaded onto itself, then how would I know the Start Time then?

    I wouldn't!

    Um, Sessions provide state that HTTP doesn't offer. (And it would be overkill - as far as I can see - to store the Start Time in the database.)


    Debbie

  • #11
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,916
    Thanks
    15
    Thanked 227 Times in 227 Posts
    Quote Originally Posted by doubledee View Post
    If I don't store the Start Time in the Session, then when my Form gets submitted and reloaded onto itself, then how would I know the Start Time then?

    I wouldn't!

    Um, Sessions provide state that HTTP doesn't offer. (And it would be overkill - as far as I can see - to store the Start Time in the database.)
    Debbie
    I thought you just wanted to know the total time it took to fill out the form. After all you've sent the start time as $_POST data, and of course you can put it in session, along with the total. I just know that my code gives the difference between the time the form is loaded till it is submitted.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #12
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,072
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by DrDOS View Post
    I thought you just wanted to know the total time it took to fill out the form.
    I do.


    Quote Originally Posted by DrDOS View Post
    After all you've sent the start time as $_POST data, and of course you can put it in session, along with the total. I just know that my code gives the difference between the time the form is loaded till it is submitted.
    I didn't follow your code, and have no clue what's up with your use of $_GET which I hate for security reasons.

    Obviously there are many ways to do the same thing.

    Here is a snippet of what I have done...

    PHP Code:
        // Initialize Session.
        
    session_start();

        if (!isset(
    $_SESSION['createAccount_start'])){
            
    $_SESSION['createAccount_start'] = time();
        }


        
    // HANDLE FORM.
        
    if ($_SERVER['REQUEST_METHOD']=='POST'){
            
    // Form was Submitted (Post).

            // Set Form Start Time.
            
    $startForm $_SESSION['createAccount_start'];

            
    // Set Form End Time.
            
    $finishForm time();


            
    var_dump($_POST);

            
    // Calculate Completion Time.
            
    $formCompletionTime = ($finishForm $startForm);

            echo 
    '<p>$startForm = ' date('Y-m-d g:i:sa'$startForm) . '</p>';
            echo 
    '<p>$finishForm = ' date('Y-m-d g:i:sa'$finishForm) . '</p>';
            echo 
    '<p>$formCompletionTime = ' gmdate("H:i:s"$formCompletionTime) . '</p>';

            
    // Clear Start Time.
            
    unset($_SESSION['createAccount_start']);

            exit(); 

    Basically I took Tango's approach, which seems to be better than what I had, and I think is fine as far as security goes.

    Sincerely,


    Debbie

  • #13
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,916
    Thanks
    15
    Thanked 227 Times in 227 Posts
    You can always substitute between $_GET and $_POST, as long as you do it across the board. Sometimes I use get temporarily because I can see the result in the URL , but I prefer post too.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.


  •  

    Posting Permissions

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