Jump to content

How do I keep form data from disappearing?


patrickmathews

Recommended Posts

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

Link to comment
Share on other sites

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?>"/>
Link to comment
Share on other sites

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>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.