MoMoMajor Posted October 15, 2008 Share Posted October 15, 2008 Hi. I am trying to learn php again and decided to start the ebook i have over. I am reading about session maintenance. The author is trying to explain how you can use different techniques to maintain sessions etc etf Below is the first technique he bought up (Hidden Form Fields). I have absolutely no idea what he is talking about. I mean, I understand how hidden forms, and forms in general work, but I have no idea what point he is trying to make here. I have bolded the parts that I do not understand. If anyone could explain to me how hidden forms are used in session maintenance or simplify what he is saying here I would be grateful. Hidden Form Fields You've seen how hidden form fields work to send data (coded by the designer) back to the server when the form is submitted. If you want to maintain state using hidden form fields (for example, a unique value representing a product the user is working with, such as the product ID), you can simply place the current product ID value into a hidden form field. Of course, the idea is that you would have the user select the product from a drop-down box (the select element) and then submit the form, using something like this code: <form action="myform.php" method="post"> <select name="selected_product_id"> <option value="121">Product 121</option> <option value="122">Product 122</option> </select> <input type="submit" name="button" value="Select Product"> </form> You could return a page to him using the following code: <input type="hidden" name="chosen_product_id" value="<?php echo $selected_product_id; ?>"> Before the page is returned, the user's input would be processed as follows (assuming he chose product 122): <form action="myform.php" method="post"> <input type="hidden" name="chosen_product_id" value="122"> So the next time he submits this form, part of the data submitted would be chosen_product_id, which PHP would turn into $chosen_product_id, and which you could then use anytime you wanted to perform processing on the product he chose, no matter how many page requests ago he chose that product. Don't forget, though, you'll need to populate that same hidden form field value each time the user requests another page or you'll lose that value. Link to comment https://forums.phpfreaks.com/topic/128497-noob-question/ Share on other sites More sharing options...
Orio Posted October 15, 2008 Share Posted October 15, 2008 What the author is talking about is forms that have multiple pages. Say on the first page they select a product, and on the second they enter their name. Then a third page adds the information to a database/emails the customer etc'. So there's a need to pass the data from the first form to the last (third) page, the form processor. The $_POST array only holds the information from the last form submission, so you need to pass somehow the data from the first form. This is done using hidden fields or using sessions, in most of the cases (you could use a database for that too). So if this is your first form: page1.php <form action="page2.php" method="post"> <select name="selected_product_id"> <option value="121">Product 121</option> <option value="122">Product 122</option> </select> <input type="submit" name="button" value="Select Product"> </form> This will take the user to page2. page2 will ask for the user's name and after that take him to page3 that will process the form. But you also need to pass the data from page1 to page3, so a hidden field is added: page2.php <form action="page3.php" method="post"> <input type="text" name="username"> <input type="hidden" name="selected_product_id" value="<?php echo $_POST['selected_product_id']; ?>"> <input type="submit" name="button" value="Go!"> </form> Now page3 will receive both the username (in $_POST['username']) and the selected product (in $_POST['selected_product_id']). I hope this sheds some light. Orio. Link to comment https://forums.phpfreaks.com/topic/128497-noob-question/#findComment-665959 Share on other sites More sharing options...
MoMoMajor Posted October 15, 2008 Author Share Posted October 15, 2008 What the author is talking about is forms that have multiple pages. Say on the first page they select a product, and on the second they enter their name. Then a third page adds the information to a database/emails the customer etc'. So there's a need to pass the data from the first form to the last (third) page, the form processor. The $_POST array only holds the information from the last form submission, so you need to pass somehow the data from the first form. This is done using hidden fields or using sessions, in most of the cases (you could use a database for that too). So if this is your first form: page1.php <form action="page2.php" method="post"> <select name="selected_product_id"> <option value="121">Product 121</option> <option value="122">Product 122</option> </select> <input type="submit" name="button" value="Select Product"> </form> This will take the user to page2. page2 will ask for the user's name and after that take him to page3 that will process the form. But you also need to pass the data from page1 to page3, so a hidden field is added: page2.php <form action="page3.php" method="post"> <input type="text" name="username"> <input type="hidden" name="selected_product_id" value="<?php echo $_POST['selected_product_id']; ?>"> <input type="submit" name="button" value="Go!"> </form> Now page3 will receive both the username (in $_POST['username']) and the selected product (in $_POST['selected_product_id']). I hope this sheds some light. Orio. This was very helpful, thank you. Link to comment https://forums.phpfreaks.com/topic/128497-noob-question/#findComment-666002 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.