1 2 Previous Next 56 Replies Latest reply: Apr 26, 2013 4:36 AM by osgood_ RSS

    Variable issues

    kiran18

      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

        • 1. Re: Variable issues
          Ken Binney MVP

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

          • 2. Re: Variable issues
            kiran18 Community Member

            Yes i am. Textbox in a form

            • 3. Re: Variable issues
              Ken Binney MVP

              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?

              • 4. Re: Variable issues
                kiran18 Community Member

                Basically i'm creating an food order site. There are five different pages with different items on each page.

                 

                Ive got texboxes which customers add their quantity in, I need that quantity to stay when i move pages.

                 

                So basically the textboxes is where the customers add their quantity.

                 

                Hope this sounds clearer

                • 5. Re: Variable issues
                  bregent MVP

                  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?

                  • 6. Re: Variable issues
                    kiran18 Community Member

                    I am using php

                    • 7. Re: Variable issues
                      kiran18 Community Member

                      Any idea on how I can solve my problem?

                      • 8. Re: Variable issues
                        osgood_ MVP

                        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'];} ?>" />

                        • 9. Re: Variable issues
                          kiran18 Community Member

                          Thanks for that, but it still hasn't worked.

                           

                          The Quantity still disappears

                           

                          Is there any other way to get it to work

                          • 10. Re: Variable issues
                            osgood_ MVP

                            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>

                            • 11. Re: Variable issues
                              osgood_ MVP

                              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>

                              • 12. Re: Variable issues
                                kiran18 Community Member

                                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?

                                • 13. Re: Variable issues
                                  osgood_ MVP

                                  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.

                                  • 14. Re: Variable issues
                                    kiran18 Community Member

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

                                     

                                    How do i keep those variables aswell the others?

                                    • 15. Re: Variable issues
                                      osgood_ MVP

                                      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']; } ?>" />

                                      • 16. Re: Variable issues
                                        kiran18 Community Member

                                        How will this work by keeping the hotdogs and cookies quantity aswell?

                                         

                                        It doesnt seem to work

                                        • 17. Re: Variable issues
                                          osgood_ MVP

                                          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>

                                          • 18. Re: Variable issues
                                            osgood_ MVP

                                            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>

                                            • 19. Re: Variable issues
                                              osgood_ MVP

                                              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'];

                                              }

                                              ?>

                                              • 20. Re: Variable issues
                                                osgood_ MVP

                                                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'];

                                                }

                                                ?>

                                                • 21. Re: Variable issues
                                                  kiran18 Community Member

                                                  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?

                                                  • 22. Re: Variable issues
                                                    osgood_ MVP

                                                    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.

                                                    • 23. Re: Variable issues
                                                      kiran18 Community Member

                                                      Do you reckon an 'If' statement will work?

                                                      • 24. Re: Variable issues
                                                        osgood_ MVP

                                                        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.

                                                        • 25. Re: Variable issues
                                                          kiran18 Community Member

                                                          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?

                                                          • 26. Re: Variable issues
                                                            osgood_ MVP

                                                            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";

                                                            }

                                                            • 27. Re: Variable issues
                                                              kiran18 Community Member

                                                              Ok brilliant thanks,

                                                               

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

                                                              • 28. Re: Variable issues
                                                                osgood_ MVP

                                                                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.

                                                                • 29. Re: Variable issues
                                                                  kiran18 Community Member

                                                                  Ok ive done this. Thanks for the help you are amazing.

                                                                   

                                                                  One more thing how do I post this to an email, because i'm trying it and email is not working.

                                                                   

                                                                  Thanks

                                                                  • 30. Re: Variable issues
                                                                    kiran18 Community Member

                                                                    Any idea on how the email will work?

                                                                    Thanks

                                                                    • 31. Re: Variable issues
                                                                      osgood_ MVP

                                                                      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.

                                                                      • 32. Re: Variable issues
                                                                        kiran18 Community Member

                                                                        any idea on how i could add a total price sum in these pages?

                                                                        Thanks

                                                                        • 33. Re: Variable issues
                                                                          osgood_ MVP

                                                                          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;

                                                                          • 34. Re: Variable issues
                                                                            kiran18 Community Member

                                                                            This is excellent.

                                                                             

                                                                            Thank you

                                                                            • 35. Re: Variable issues
                                                                              kiran18 Community Member

                                                                              Hi any idea on making the variables go when ive ordered what i want.

                                                                               

                                                                              For example i want to order another meal for someone else so all the variables get cleared.

                                                                               

                                                                              For example ive ordered 3 cookies in my first order and sent that but now i want to order 2 cookies and 1 icecream in my next order.

                                                                               

                                                                              Thanks

                                                                              • 36. Re: Variable issues
                                                                                osgood_ MVP

                                                                                On the order page try adding:

                                                                                 

                                                                                unset($_SESSION['cookiesQuantity']);

                                                                                session_destroy();

                                                                                • 37. Re: Variable issues
                                                                                  kiran18 Community Member

                                                                                  That's not working.

                                                                                   

                                                                                  Basically when I finished ordering it i have a thank-you page saying what i ordered.

                                                                                   

                                                                                  Then i go back to order page and click on one the links i want to order from and the quantities are still there.

                                                                                   

                                                                                  To make it easier to understand, this is how my pages look:

                                                                                   

                                                                                  ORDER page:

                                                                                  Breakfast Buffets

                                                                                  Platters

                                                                                  Combi buffet

                                                                                  Drinks

                                                                                  Add-ons

                                                                                  All the order page has is those five links and that's it.

                                                                                   

                                                                                  Checkout page - everything i ordered seen on this page with links to go back to each of the five page. For example:

                                                                                  Ordered 2 breakfast

                                                                                  Ordered 3 Coffees

                                                                                   

                                                                                  Breakfast Buffets Platters etc..... (these are links)

                                                                                   

                                                                                  Checkout 2 page - the customer enters their details i.e. address, name etc..

                                                                                   

                                                                                  Thank you page - This confirms the customers order and sends an email to the company they ordered from and to the customer themselves.

                                                                                   

                                                                                  So I finished ordering i click on the order page and the quantity i type previously is still there. Any idea how this can be solved?

                                                                                   

                                                                                  Thanks

                                                                                  • 38. Re: Variable issues
                                                                                    kiran18 Community Member

                                                                                    Actually don't worry about it, I've got it working

                                                                                    • 39. Re: Variable issues
                                                                                      kiran18 Community Member

                                                                                      Hi is there any chance if you could help me with one more thing.

                                                                                       

                                                                                      Any ideas oh how you can recieve email by selecting only certain products.

                                                                                       

                                                                                      For example:

                                                                                      Contact Form

                                                                                      Name:...............          Address:..............

                                                                                       

                                                                                      Delivery Date.............     Etc. etc.....

                                                                                       

                                                                                      Selected Products

                                                                                      Cookies 2

                                                                                      Tea      4

                                                                                      Ice Cream 0

                                                                                       

                                                                                      I want the email to show everything apart from Ice Cream. Any ideas on how this can work?

                                                                                       

                                                                                      Thanks

                                                                                      1 2 Previous Next