Skip to main content
Inspiring
October 5, 2011
Answered

Submit form in two parts (PHP, MySQL)

  • October 5, 2011
  • 1 reply
  • 2066 views

I don't even know if this is possible - my PHP knowledge is scant - but here goes:

I have a PHP form that submits data to a MySQL database. What I would like is to have the user complete the form, then click Submit, then be taken to a page where they enter their email address (for later varification) and for this information to be submitted with the original form.

Is this possible? Can I create hidden fields in a form that only appear after the form is submitted in the first instance? And how do I make sure that the email address that is collected is still linked to the initial form submission?

Any ideas gratefully received.

This topic has been closed for replies.
Correct answer bregent

>Well, the reason is merely a user-experience one - it

>is quite a long survey, so I'm trying to break it up a bit.

OK, that makes sense although you mentioned that you only want the email field on the second part. How many total fields are there?

One way to break it up is to use the method that Rob suggested. One drawback with that approach is that if the user abandons the site without completing all parts, your database is left with incomplete data. This may lead to problems, but there are ways to deal with that too.

Another approach is the one I mentioned earlier - pass the completed form fields from one page to the next and store them in hidden fields. You'll need to use server side scripting to retrieve the posted data and store in the fields.

Another common approach is to store the fields in session variables. Then they are all available when you finally perform the insert.

1 reply

Participating Frequently
October 5, 2011

>Is this possible?

Of course

>Can I create hidden fields in a form that only

>appear after the form is submitted in the first instance?

>And how do I make sure that the email address that is

>collected is still linked to the initial form submission?

There are a few methods, but it depends on your needs.

What you can do is have the first form post to another page that stores the contents of the first form in hidden fields, and only presents the email field. Then submit to the db all at once.

Perhaps if you explained why you want to have two parts. Why do you want the user to submit the form and them be presented with another field to complete? Do they need to complete this?

_AJD_TUS_Author
Inspiring
October 6, 2011

Well, the reason is merely a user-experience one - it is quite a long survey, so I'm trying to break it up a bit.

How do hidden fields work? How do I create them, and get the first page to send to a second page?

If I'm honest, it's not a big deal - I could just as easily keep it as it is - but I'm just rtying to see if I can improve the user experience a little.

Thanks again, bregant

Rob Hecker2
Legend
October 7, 2011

An alternative to storing lots of data in hidden fields is to go ahead and submit the data from the first form to the database (after it has been validated and sanitized, of course), then ask the database for the unique auto_increment id it generates, like this:

$user_id = mysql_insert_id(); 

(NOTE: unless you are using PDO, in which case you would do it like this: $user_id=$dbh->lastInsertId();)

Then you pass the user_id with the url to the next page:

form_page_2.php?user_id=<?php echo $user_id ?>

Then, on the second page, instead of your SQL being an INSERT INTO you use UPDATE ...WHERE user_id='$user_id'

If a more secure approach is needed, you can pass the user_id as a session variable instead of with the url to the second page

The form section of your second page will need just one hidden field, to hold the user_id. Like so:

Put this somewhere above the form in the PHP:

$user_id=$_GET['user_id'];

Then in the form. . .

<input type='hidden' name='user_id' value='<?php echo $user_id ?>'/>