Skip navigation
kiran18
Currently Being Moderated

Variable issues

Apr 9, 2013 7:42 AM

Hi, when I type something into a textbox and go to another page then whatever I typed is not available when i return back to the orginal page.

 

For example, on Page 1 i type 3 into a textbox, then go to Page 2. Then return back to Page 1 and 3 is now gone.

 

Any idea how to make that variable stay?

 

Thanks

 
Replies 1 2 Previous Next
  • Currently Being Moderated
    Apr 9, 2013 8:51 AM   in reply to kiran18

    A "textbox" could be anything; are you referring to a form?

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 9, 2013 9:34 AM   in reply to kiran18

    Thanks -

    Sorry to say I am not following your workflow.  Textbox elements are usually designed to be empty.  Why are you typing in it?

     

    Are you attempting to place a "hint" in it?

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 9, 2013 11:09 AM   in reply to kiran18

    HTTP is stateless. In order to 'save' values, you either need to pass them between pages or store them in session variables or cookies. Which server side scripting language are you using?

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 10, 2013 2:35 AM   in reply to kiran18

    If youre using php then you have to pass the value to other pages using SESSION variables.

     

    At the very top of each page you want to pass information to put:

     

    <?php session_start(); ?>

     

    Then assign the information you get from the forms input field to a $_SESSION variable like:

     

    $_SESSION['hotdogQuantity'] = $_POST['hotdogQuantity'];

     

    $_SESSION['cookiesQuantity'] = $_POST['cookiesQuantity'];

     

     

    Then in the forms input 'value' field which relates to the product quantity insert:

     

    <input type="text" name="hotdogQuantity" id="hotdogQuantity"  value="<?php if(isset($_SESSION['hotdogQuantity'])) {echo $_SESSION['hotdogQuantity'];} ?>" />

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 10, 2013 5:19 AM   in reply to kiran18

    It will work. Heres a simple example for you to look at. Copy all the code below and save it as session_1.php to your php enable server. (I'll post session_2.php in the following post)

     

     

     

    <?php session_start(); ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title>Session sample</title>

    </head>

     

    <body>

    <div id="sign_up">

    <form id="order" name="order" method="post" action="session_2.php">

    <p><label>

    How many Hot Dogs would you like:<br />

    <input type="text" name="hotdogsQuantity" id="hotdogsQuantity" value="<?php if(isset($_SESSION['hotdogsQuantity'])) {echo $_SESSION['hotdogsQuantity']; } ?>"/>

    </label>

    </p>

    <p>

    <label>

    How many packets of cookies would you like:<br />

    <input type="text" name="cookiesQuantity" id="cookiesQuantity" value="<?php if(isset($_SESSION['cookiesQuantity'])) {echo $_SESSION['cookiesQuantity']; } ?>" />

    </label>

    </p>

    <p>

    <label>

    <input type="submit" name="submit" id="submit" value="Submit" />

    </label>

    </p>

    </form>

    </div>

    </div>

    </body>

    </html>

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 10, 2013 5:21 AM   in reply to osgood_

    Here's session_2.php the previous page links to it. Copy the code and save it as session_2.php. Enter values in the two boxes on session_1.php, go to session_2.php where they will be displayed and then go back to session_1.php, see the two fields are still populated.

     

     

    <?php session_start(); ?>

    <?php

    $_SESSION['hotdogsQuantity'] = $_POST['hotdogsQuantity'];

    $_SESSION['cookiesQuantity'] = $_POST['cookiesQuantity'];

    ?>

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title>Session sample</title>

    </head>

     

    <body>

    <div id="sign_up">

    <form id="order" name="order" method="post" action="session_2.php">

    <p><label>

    How many Hot Dogs would you like:<br />

    <input type="text" name="hotdogsQuantity" id="hotdogsQuantiy" value="<?php if(isset($_SESSION['hotdogsQuantity'])) {echo $_SESSION['hotdogsQuantity']; } ?>"/>

    </label>

    </p>

    <p>

    <label>

    How many packets of cookies would you like:<br />

    <input type="text" name="cookiesQuantity" id="cookiesQuantity" value="<?php if(isset($_SESSION['cookiesQuantity'])) {echo $_SESSION['cookiesQuantity']; } ?>" />

    </label>

     

    </p>

    <p>

    <label>

    <input type="submit" name="submit" id="submit" value="Submit" />

    </label>

    </p>

    </form>

    <a href="session_1.php">Go back one page</a>

    </div>

    </div>

    </body>

    </html>

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 10, 2013 5:43 AM   in reply to kiran18

    kiran18 wrote:

     

    This is brillaint thank you.

     

    One more question (hopefully) if i just want to order hot dogs only then how do i only display the hot dogs item without having the cookies there aswell on session_2.php?

     

    The person who orders would never order cookies if they didnt want them and if they did by mistake, or no longer wants them they would delete the quantity from the field.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 11, 2013 4:39 AM   in reply to kiran18

    kiran18 wrote:

     

    I've got four other pages with different items in them.

     

    How do i keep those variables aswell the others?

     

    Just use the same principle of passing $_SESSION variables forwards and backwards from page to page

     

    So on your four other pages as long as this - <?php session_start(); ?> - is at the top the information on the first pages will get passed along.

     

    You obviously have to create new $_SESSION varaibles for new items, so on page 3 if you had another item - Chocolate Fudge:

     

    In the forms input field on page 3 you would write this:

     

    <input type="text" name="chocolateFudge" id="chocolateFudge" value="<?php if(isset($_SESSION['chocolateFudge'])) {echo $_SESSION['chocolateFudge']; } ?>" />

     

    The above form value field is only 'set' if you go back to page 3 after visiting page 4.

     

    Then on the page 4 create the session for Chocolate Fudge which gets the information from the forms 'value' field on page 3:

     

    $_SESSION['chocolateFudge'] = $_POST['chocolateFudge'];

     

    Pass it to the forms 'value' field on page 4:

     

    <input type="text" name="chocolateFudge" id="chocolateFudge" value="<?php if(isset($_SESSION['chocolateFudge'])) {echo $_SESSION['chocolateFudge']; } ?>" />

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 11, 2013 5:35 AM   in reply to kiran18

    You have to start getting a bit more 'creative' - below is an update to the session_2.php I posted yesterday. I'll post another page session_3.php in the following post.

     

    I've now got the below at the top of the page which says if $_SESSION['hotdogsQuantity']; is NOT SET get the value from the forms 'value' field, which we don't want as it IS SET.

     

    if(!isset($_SESSION['hotdogsQuantity'])) {

    $_SESSION['hotdogsQuantity'] = $_POST['hotdogsQuantity'];

    }

     

     

    Page code for session_2.php

     

     

    <?php session_start(); ?>

    <?php

    if(!isset($_SESSION['hotdogsQuantity'])) {

    $_SESSION['hotdogsQuantity'] = $_POST['hotdogsQuantity'];

    }

    if(!isset($_SESSION['cookiesQuantity'])) {

    $_SESSION['cookiesQuantity'] = $_POST['cookiesQuantity'];

    }

    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title>Session sample</title>

    </head>

     

    <body>

    <div id="sign_up">

    <form id="order" name="order" method="post" action="session_3.php">

    <p><label>

    How many Hot Dogs would you like:<br />

    <input type="text" name="hotdogsQuantity" id="hotdogsQuantiy" value="<?php if(isset($_SESSION['hotdogsQuantity'])) {echo $_SESSION['hotdogsQuantity']; } ?>"/>

    </label>

    </p>

    <p>

    <label>

    How many packets of cookies would you like:<br />

    <input type="text" name="cookiesQuantity" id="cookiesQuantity" value="<?php if(isset($_SESSION['cookiesQuantity'])) {echo $_SESSION['cookiesQuantity']; } ?>" />

    </label>

     

    </p>

    <p>

    <label>

    <input type="submit" name="submit" id="submit" value="Submit" />

    </label>

    </p>

    </form>

    <a href="session_1.php">Go to previous page</a> | <a href="session_3.php">Go to next page</a>

    </div>

    </div>

    </body>

    </html>

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 11, 2013 5:36 AM   in reply to osgood_

    Heres the page code for session_3.php which session_2.php links to.

     

     

    <?php session_start(); ?>

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title>Session sample</title>

    </head>

     

    <body>

    <div id="sign_up">

    <form id="order" name="order" method="post" action="session_4.php">

    <p><label>

    How many Hot Dogs would you like:<br />

    <input type="text" name="hotdogsQuantity" id="hotdogsQuantiy" value="<?php if(isset($_SESSION['hotdogsQuantity'])) {echo $_SESSION['hotdogsQuantity']; } ?>"/>

    </label>

    </p>

    <p>

    <label>

    How many packets of cookies would you like:<br />

    <input type="text" name="cookiesQuantity" id="cookiesQuantity" value="<?php if(isset($_SESSION['cookiesQuantity'])) {echo $_SESSION['cookiesQuantity']; } ?>" />

    </label>

    <p>

    <label>

    How many chocolate fudges would you like:<br />

    <input type="text" name="chocolateFudge" id="chocolateFudge" value="<?php if(isset($_SESSION['chocolateFudge'])) {echo $_SESSION['chocolateFudge']; } ?>" />

    </label>

     

    </p>

    <p>

    <label>

    <input type="submit" name="submit" id="submit" value="Submit" />

    </label>

    </p>

    </form>

    <a href="session_2.php">Go back one page</a>

    <p>You have already ordered <?php echo $_SESSION['hotdogsQuantity']; ?> hot dogs and <?php echo $_SESSION['cookiesQuantity'] ?> packets of cookies</p>

    </div>

    </div>

    </body>

    </html>

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 11, 2013 5:46 AM   in reply to osgood_

    The php block  at the top of session_2.php example should be as below NOT as posted.

     

     

    <?php session_start(); ?>

    <?php

    if (isset($_SESSION['hotdogsQuantity'])) {

    $_SESSION['hotdogsQuantity'] = $_SESSION['hotdogsQuantity'];

    }

    else {

    $_SESSION['hotdogsQuantity'] = $_POST['hotdogsQuantity'];

    }

    if (isset($_SESSION['cookiesQuantity'])) {

        $_SESSION['cookiessQuantity'] = $_SESSION['cookiesQuantity'];

    }

    else {

    $_SESSION['cookiesQuantity'] = $_POST['cookiesQuantity'];

    }

    ?>

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 11, 2013 6:31 AM   in reply to osgood_

    This is a better way of doing it. use this block of php code at the top of session_2.php. That seems to work more smoothy.

     

    <?php session_start(); ?>

    <?php

    if ($_POST && !empty($_POST['hotdogsQuantity'])) {

    $_SESSION['hotdogsQuantity'] = $_POST['hotdogsQuantity'];

    }

    if ($_POST && !empty($_POST['cookiesQuantity'])) {

    $_SESSION['cookiesQuantity'] = $_POST['cookiesQuantity'];

    }

    ?>

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 15, 2013 3:49 AM   in reply to kiran18

    kiran18 wrote:

     

    This is excellent thanks.

     

    But what i need is to display only what the customer orders.

     

    So if they want to order 4 cookies only and not any hot dogs, then how would this work?

     

    So my next page would display 4 Cookies only?

     

    I'm not sure this is possible without using some kind of a shopping cart where users actually have to click a link to add what they want to a basket.

     

    S_SESSIONS only pass information back and forth between pages. Its up to the user to check what they order. Normally before you order/buy anything from a website you get sent to an oredr confirmation page which shows what you have ordered. On the page you have the ability to either delete something you don't want or add more of something you do want.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 15, 2013 4:07 AM   in reply to kiran18

    kiran18 wrote:

     

    Do you reckon an 'If' statement will work?

     

    I can't see how?

     

    If you have a form field filled in which is not supposed to be  how does php know or not if it is meant to be filled in or not? Only a user knows that.

     

    If you are selling 'products' why are you not using some kind of cart to gather the information of what the user has selected.

     

    Paypal cart buttons are simple enough to set up.

     

    The user then gets to decide what to eject from the cart/order page before the order is sent.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 15, 2013 4:39 AM   in reply to kiran18

    kiran18 wrote:

     

    Ok if i just want to not show the quantity will that work because that's in a textbox?

     

    For example on page 1 I select 0 hot dogs and 4 cookies.

     

    Then page 2 displays this:

     

    Hot dogs

     

    Cookies 4

     

    Can this be done?

     

     

    Yes, Just include the below in the page where you want to display the quantity:

     

     

    <?php if(isset($_SESSION['cookiesQuantity'])) {

    echo "You have selected ".$_SESSION['cookiesQuantity']." packets of cookies";

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 15, 2013 5:34 AM   in reply to kiran18

    kiran18 wrote:

     

     

     

    Where will this go at the top of the page or in the value

     

    Anywhere on the page that you want to say:

     

    You have selected 4 packets of cookies.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 16, 2013 3:56 AM   in reply to kiran18

    kiran18 wrote:

     

    Any idea on how the email will work?

    Thanks

     

    I don't know of a way other than getting the information from form fields and sending the information to a php mail script to be processed.

     

    That's why when dealing with product selection you should be using a shopping cart.

     

    Getting information from a 'static' form like - Name, Address, Telephone form fields etc is easy BUT with 'products' which have different names the script needs to loop through the information to create the form field name so the php processing script can get the information from it.

     

    Like:

     

    <input type="text" name="Cookies" value="cookiesQuantity">

    <input type="text" name="Milkshakes" value="milkshakeQuantity">

     

    Next time it might be:

     

    <input type="text" name="IceCream" value="iceCreamQuantity">

    <input type="text" name="Doughnuts" value="doughnutQuantity">

     

    Shopping cart scripts do the above for you otherwise you have to write the code yourself to dynamically generate the correct form 'name' field and value field so the php mailing script can get the correct information.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 19, 2013 3:22 AM   in reply to kiran18

    You could try the below. Set all of the products prices as SESSIONS:

     

    // set product price

    $_SESSION['cookiesPrice'] = "1.50";

    $_SESSION['iceCreamPrice'] = "2.50";

     

     

    Get the quantity ordered from the form field:

     

    // get from form product quantity

    $_SESSION['cookiesQuantity'] = $_POST['cookiesQuantity'];

    $_SESSION['iceCreamQuantity'] = $_POST['iceCreamQuantity'];

     

     

    Add up the total price (could get quite long depending on the number of products. I'd test after adding each product to the striing)

     

    // get total price

    $totalPrice = number_format($_SESSION['cookiesPrice'] * $_SESSION['cookiesQuantity'] + $_SESSION['iceCreamPrice'] * $_SESSION['iceCreamQuantity'],2);

     

    Echo out the total price:

     

    echo "$".$totalPrice;

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2013 2:30 AM   in reply to kiran18

    On the order page try adding:

     

    unset($_SESSION['cookiesQuantity']);

    session_destroy();

     
    |
    Mark as:
1 2 Previous Next
Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points