patrickmathews Posted December 31, 2013 Share Posted December 31, 2013 I have site that was written as a custom job several years ago. Part of the site uses authorize.net allowing new customers to register and buy products online. It is all php/mysql. There is no framework involved. Most of the code is 3 - 5 years old. I am starting to go through it as we are migrating it to a new hosting platform and I"m realizing there are some changes that need to be made. One glaring annoyance is that the buynow.php page which is where people register and buy stuff has a link to enter a coupon code. The link takes the user to a new page with a simple form to enter a coupon code. Once the code is entered and the user hits submit, the code is validated and if all is well, the user is taken back to the buynow.php page. The only problem is that the user has entered a bunch of data (name, address, email etc..) and then got to the coupon code link, clicked it, came back to buynow.php, and the form is empty again. What is the best way to keep the form data saved and repopulate the form with that data when the coupon code is entered? I think it may be better to change the coupon code logic so it happens on the same buynow.php page, and updates the price when the user hits and "apply coupon" button. I am a php novice - I have done programming before but it's been a while and it certainly wasn't php. I've been through lots of php tutorials and read a lot about it. I'm hoping I can get some guidance here so I can make these changes myself and turn it into a learning experience. Thanks Patrick buynow.php Quote Link to comment https://forums.phpfreaks.com/topic/284998-how-do-i-keep-form-data-from-disappearing/ Share on other sites More sharing options...
dungpt29 Posted December 31, 2013 Share Posted December 31, 2013 I will work out a solution but I do not know if it is the best way to solve your problem. I think its only shortcoming is that it will make your code longer. First, you should edit your code in buynow.php so that when users click the link to navigate to your coupon code page, this link will submit the form containing data such as name, address, email, etc. This means that this link has the role as a button instead of a normal link. To achieve this, Javascript or JQuery is recommended. In your coupon code page, your get user data using $_POST as normal. For example: $address= $_POST["txtAddress"]; Next, you should complement the hidden fields to the form in your coupon code page that are specialized in containing user data temporarily. For example: <input type="hidden" id="hdAddress" name="hdAddress" value="<?php echo $address?>" /> You get the temporary user data when turning back to your buynow.php as the following: $hdAddress= $_POST["hdAddress"]; You repopulate user data in buynow.php as the following: <input type="text" name="txtAddress" id="txtAddress" size="100" maxlength="100" value="<?php echo $hdAddress?>"/> Quote Link to comment https://forums.phpfreaks.com/topic/284998-how-do-i-keep-form-data-from-disappearing/#findComment-1463430 Share on other sites More sharing options...
adam_bray Posted December 31, 2013 Share Posted December 31, 2013 The simple option is to store your $_POST data in $_SESSION's. Here's a basic example on one page - <?php session_start(); if( $_POST['submit'] ) { if( strlen( $_POST['input'] ) < 10 ) { $_SESSION['data_input'] = $_POST['input']; } else { print 'Correct sized input!'; } } ?> <form method="post" action="<?=$_SERVER['PHP_SELF'];?>"> <input type="text" name="input" placeholder="Input" value="<?=$_SESSION['data_input'];?>" /> <input name="submit" type="submit" value="Submit Form" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/284998-how-do-i-keep-form-data-from-disappearing/#findComment-1463431 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.