*

Author Topic: Passing PHP form variables from one page to other PHP Form #FreeScriptingTip  (Read 122 times)

Offline admin

  • Administrator
  • Newbie
  • *****
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Passing PHP form variables from one page to other.

Note from Rick "The Automation Man" Kirkham: I ran across this and thought it may be helpful for intermediate programmers and website designers. Of course you could always just hire me for programming and website design. ComputerHelp808@gmail.com



PHP Form, Tutorials php form variables


Multi-page forms using hidden input fields

Hidden input fields are form fields that are not visible. The user can’t see or change these fields, and they are used to transmit state information between different pages. Let’s use hidden fields to transport our data across our form, to the final processing script.
We start with the same form for step 1:

1
<form method="post" action="form2.php">
2
    <input type="text" name="name">
3
    <input type="text" name="email_address">
4
    <input type="submit" value="Go To Step 2">
5
</form>
Ok, so nothing more than 2 input fields and a submit button to take us to step 2. In the following page, apart from the HTML form to gather membership data, we are going to need code to store the submitted data from step 1 in the session.

1
<form method="post" action="form3.php">
2
<input type="radio" group="membership_type" value="Free">
3
 
4
<input type="radio" group="membership_type" value="Normal">
5
 
6
<input type="radio" group="membership_type" value="Deluxe">
7
 
8
<input type="checkbox" name="terms_and_conditions">
9
 
10
<input type="hidden" name="name"
11
value="<?php echo $_POST['name']; ?>">
12
 
13
<input type="hidden" name="email_address"
14
 value="<?php echo $_POST['email_address']; ?>">
15
 
16
<input type="submit" value="Go To Step 3">
17
</form>
Please note that although the hidden fields are not visible to the visitor, they are visible in source of the page, so you must not use them to store critical information.

Now our form contains all the data entered so far, which we are going to post to the third part of the form.

1
<form method="post" action="form_process.php">
2
 
3
<input type="text" name="name_on_card">
4
 
5
<input type="text" name="credit_card_number">
6
 
7
<input type="text" name="credit_card_expiration_date">
8
 
9
<input type="hidden" name="name"
10
 value="<?php echo $_POST['name']; ?>">
11
 
12
<input type="hidden" name="email_address"
13
 value="<?php echo $_POST['email_address'];
14
 
15
<input type="hidden" name="membership_type"
16
 value="<?php echo $_POST['membership_type']; ?>">
17
 
18
<input type="hidden" name="terms_and_conditions"
19
  value="<?php echo $_POST['terms_and_conditions]; ?>">
20
   
21
<input type="submit" value="Finish">
22
</form>
Now that we have all our fields available, let’s proceed to the form processing.

1
<?php
2
 
3
//let's create the query
4
$insert_query = "insert into subscriptions (".
5
"name,".
6
"email_address,".
7
"membership_type".
8
"terms_and_conditions,".
9
"name_on_card,".
10
"credit_card_number,".
11
"credit_card_expiration_data".
12
") values (".
13
"'" . $_POST['name'] . "', ".
14
"'" . $_POST['email_address'] . "',".
15
"'" . $_POST['membership_type'] . "',".
16
"'" . $_POST['terms_and_conditions'] . "',".
17
"'" . $_POST['name_on_card'] . "',".
18
"'" . $_POST['credit_card_number'] . "',".
19
"'" . $_POST['credit_card_expiration'] ."' )";
20
 
21
//let's run the query
22
mysql_query($insert_query);
23
 
24
?>
And that’s it. Please note that this time, we have all the data available in the $_POST array.

A good idea would be to use form validation for each step of the form, so you don’t end up with incomplete data in the database.

Now you know two methods of creating multipart forms, to create better user-experience for your visitors.


Source http://form.guide/php-form/php-form-variables.html

 

Recent

Members
Stats
  • Total Posts: 44
  • Total Topics: 44
  • Online Today: 2
  • Online Ever: 38
  • (September 02, 2017, 11:52:22 PM)
Users Online
Users: 0
Guests: 1
Total: 1