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 11 of 11
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Turning $_POST into $_SESSION??

    Hi guys,

    I'm trying to turn $_POST into $_SESSION. I'm creating an Inspection form which once submitted will echo the form and display another form. This will occur 3x allowing 3 different inspection times.

    If I use $_POST, the form data will appear however display once another form is submitted hence why I want to create a session. I've done some research however I cannot seem to get it. I will put example code below.


    Assistance would be greatly appreciated.

    Code:
    <form>
    <input type="text" name="date"/>
    <input type="text" name="time"/></form>
    PHP Code:
    <?php
    session_start
    ();
    $_SESSION['date'] = $_POST['date'];
    $_SESSION['time'] = $_POST['time'];
    ?>

    <?php
    if(isset($_POST['date'],$_POST['time']))
    {
      echo 
    'Inspection Time 1:, '$_SESSION['date'], $_SESSION['time']; // Won't display form here as is just example
    }
    ?>

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,538
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    Before you continue with this, I have a question that might change how your original post gets answered.

    What should happen if the user enters 1 or 2 inspection times and then walks away from their computer and closes their browser? The 3rd inspection time never gets entered, and the previous data is lost. Is it important for you to retain the inspection times even after the browser closes?

    This will be a difference between using cookies or using sessions. And also a difference if the computer is used by one person, or several people (which creates log-in issues).

    Describe what should happen, where the data now goes, and how the process should work.

  • #3
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    you could do this instead of using session:

    PHP Code:
    <?php

    echo '<form method="POST"><input type="hidden" name="form" value="sent">'.PHP_EOL;
    if (isset(
    $_POST['form'])){
        foreach (
    $_POST['date'] as $k => $v){
            echo 
    '<input type="hidden" name="date['.$k.']" value="'.$v.'"/>'.PHP_EOL;
        }
        foreach (
    $_POST['time'] as $k1 => $v1){
            echo 
    '<input type="hidden" name="time['.$k1.']" value="'.$v1.'"/>'.PHP_EOL;
        }
    }
    echo 
    '
        <input type="text" name="date[]"/>
        <input type="text" name="time[]"/>
        <input type="submit">
    </form>'
    ;
    if (isset(
    $_POST['date'])){
        foreach (
    $_POST['date'] as $k2 => $v2){
            echo 
    'Date ' $k2 ': ' $v2 ' - Time ' $k2 ': ' $_POST['time'][$k2] .'<BR>'.PHP_EOL;
        }
    }

    ?>
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #4
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    mlseim raises good points so pay attention to that.

    However, I think your most immediate problem is the lack of a method attribute on your form tag. You will also want to set an action attribute for good measure.

    If the method is not set to "post" then no post data will exist.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #5
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    Before you continue with this, I have a question that might change how your original post gets answered.

    What should happen if the user enters 1 or 2 inspection times and then walks away from their computer and closes their browser? The 3rd inspection time never gets entered, and the previous data is lost. Is it important for you to retain the inspection times even after the browser closes?

    This will be a difference between using cookies or using sessions. And also a difference if the computer is used by one person, or several people (which creates log-in issues).

    Describe what should happen, where the data now goes, and how the process should work.
    Excellent point

    What I had planned to occur:

    Inspection form 1 already visible which will consist of (date, start hour, start minute & end hour, end minute)

    One the form is posted, it will b echo'd for the user to see it, along with inspection two form appearing (echo'ing the form), and the process continues up to say 3/4 inspection times.

    The only reason POST is not working for me now is that when the second inspection form is posted, the first echo disappears.

    If there is a better way, then would love to know.
    Last edited by rexhvn; 04-17-2013 at 02:01 AM.

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,538
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    If only one person is using the computer, laptop, or browser, then you should use cookies to save values. At least they will be retained if the user accidentally closes their browser. If the computer has multiple users, then the cookie idea won't work because everyone will be using the same cookies.

    If you use a cookie array, you can interrogate the array each time and determine which form to display based on which array items have a value.

    Here's more about cookie arrays:
    http://www.plus2net.com/php_tutorial...kies_array.php

    Once the third (or last) inspection time has been entered, the cookie array can be emailed to someone (needing the report) and then cleared. Or the array can simply be written into a log file of some kind.

  • #7
    New Coder
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    1
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Arcticwarrio View Post
    you could do this instead of using session:

    PHP Code:
    <?php

    echo '<form method="POST"><input type="hidden" name="form" value="sent">'.PHP_EOL;
    if (isset(
    $_POST['form'])){
        foreach (
    $_POST['date'] as $k => $v){
            echo 
    '<input type="hidden" name="date['.$k.']" value="'.$v.'"/>'.PHP_EOL;
        }
        foreach (
    $_POST['time'] as $k1 => $v1){
            echo 
    '<input type="hidden" name="time['.$k1.']" value="'.$v1.'"/>'.PHP_EOL;
        }
    }
    echo 
    '
        <input type="text" name="date[]"/>
        <input type="text" name="time[]"/>
        <input type="submit">
    </form>'
    ;
    if (isset(
    $_POST['date'])){
        foreach (
    $_POST['date'] as $k2 => $v2){
            echo 
    'Date ' $k2 ': ' $v2 ' - Time ' $k2 ': ' $_POST['time'][$k2] .'<BR>'.PHP_EOL;
        }
    }

    ?>
    agreed, you can do this *and maybe should; just be sure to include a connectdb.php file.

  • #8
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    If only one person is using the computer, laptop, or browser, then you should use cookies to save values. At least they will be retained if the user accidentally closes their browser. If the computer has multiple users, then the cookie idea won't work because everyone will be using the same cookies.

    If you use a cookie array, you can interrogate the array each time and determine which form to display based on which array items have a value.

    Here's more about cookie arrays:
    http://www.plus2net.com/php_tutorial...kies_array.php

    Once the third (or last) inspection time has been entered, the cookie array can be emailed to someone (needing the report) and then cleared. Or the array can simply be written into a log file of some kind.
    One person at a time will be selecting inspections. I may try this out!

    Thanks,

  • #9
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Arcticwarrio View Post
    you could do this instead of using session:

    PHP Code:
    <?php

    echo '<form method="POST"><input type="hidden" name="form" value="sent">'.PHP_EOL;
    if (isset(
    $_POST['form'])){
        foreach (
    $_POST['date'] as $k => $v){
            echo 
    '<input type="hidden" name="date['.$k.']" value="'.$v.'"/>'.PHP_EOL;
        }
        foreach (
    $_POST['time'] as $k1 => $v1){
            echo 
    '<input type="hidden" name="time['.$k1.']" value="'.$v1.'"/>'.PHP_EOL;
        }
    }
    echo 
    '
        <input type="text" name="date[]"/>
        <input type="text" name="time[]"/>
        <input type="submit">
    </form>'
    ;
    if (isset(
    $_POST['date'])){
        foreach (
    $_POST['date'] as $k2 => $v2){
            echo 
    'Date ' $k2 ': ' $v2 ' - Time ' $k2 ': ' $_POST['time'][$k2] .'<BR>'.PHP_EOL;
        }
    }

    ?>
    This works great, but I wish to change two things.

    1. The first result that appears comes as 'Date 0' when i want it to be date 1

    2. I've included a delete button... is there a way I can delete an inspection?
    PHP Code:
    if (isset($_POST['date'])){
        foreach (
    $_POST['date'] as $k2 => $v2){
            echo 
    'Date ' $k2 ': ' $v2 ' - Time ' $k2 ': ' $_POST['time'][$k2] . '<input type="button" value="Delete" id="delete"/>' '<BR>'.PHP_EOL;
        }


  • #10
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    if you need tracking with add/remove capability you'd be better off using a database

    as for the numbers

    PHP Code:

     <?php

    echo '<form method="POST"><input type="hidden" name="form" value="sent">'.PHP_EOL;
    if (isset(
    $_POST['form'])){
        foreach (
    $_POST['date'] as $k => $v){
            echo 
    '<input type="hidden" name="date['.$k.']" value="'.$v.'"/>'.PHP_EOL;
        }
        foreach (
    $_POST['time'] as $k1 => $v1){
            echo 
    '<input type="hidden" name="time['.$k1.']" value="'.$v1.'"/>'.PHP_EOL;
        }
    }
    echo 
    '
        <input type="text" name="date[]"/>
        <input type="text" name="time[]"/>
        <input type="submit">
    </form>'
    ;
    if (isset(
    $_POST['date'])){
        foreach (
    $_POST['date'] as $k2 => $v2){
            echo 
    'Date ' . ($k2+1) . ': ' $v2 ' - Time ' . ($k2+1) . ': ' $_POST['time'][$k2] .'<BR>'.PHP_EOL;
        }
    }

    ?>
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #11
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    if you need tracking with add/remove capability you'd be better off using a database

    as for the numbers

    PHP Code:

     <?php

    echo '<form method="POST"><input type="hidden" name="form" value="sent">'.PHP_EOL;
    if (isset(
    $_POST['form'])){
        foreach (
    $_POST['date'] as $k => $v){
            echo 
    '<input type="hidden" name="date['.$k.']" value="'.$v.'"/>'.PHP_EOL;
        }
        foreach (
    $_POST['time'] as $k1 => $v1){
            echo 
    '<input type="hidden" name="time['.$k1.']" value="'.$v1.'"/>'.PHP_EOL;
        }
    }
    echo 
    '
        <input type="text" name="date[]"/>
        <input type="text" name="time[]"/>
        <input type="submit">
    </form>'
    ;
    if (isset(
    $_POST['date'])){
        foreach (
    $_POST['date'] as $k2 => $v2){
            echo 
    'Date ' . ($k2+1) . ': ' $v2 ' - Time ' . ($k2+1) . ': ' $_POST['time'][$k2] .'<BR>'.PHP_EOL;
        }
    }

    ?>
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month


  •  

    Posting Permissions

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