Reputation: 23
I am trying to carry or echo the information that a user puts in a form, there are 3 forms the user needs to complete which are all on different pages, by the time they get to the 4th page i need all this information to be inserted into the mysql database all together.
Now i have my sql query which puts the data into the database and this inputs the data fine but only puts in the data on the last page/which is the last form the user completes. Any other previous information the user puts into the earlier forms is not input into mysql even though i am using the values.
i think you need to create a session to carry the data over to the next page, and so i am trying to do that like so, but its not working:
page 1 / form 1:
<?php
session_start();
?>
<form class="" method="post" action="register_p2.php">
<div class="row first_name">
<input type="text" id="first_name" name="first_name" placeholder="First Name" />
</div>
<div class="row last_name">
<input type="text" id="last_name" name="last_name" placeholder="Last Name" />
</div>
<div class="row email">
<input type="email" id="email" name="email" placeholder="Email" />
</div>
<input type="submit" value="Next >" />
</form>
page 2 / form 2:
<?php
session_start();
// other php code here
$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;
$_SESSION['email'] = $email;
?>
<form name="myForm" method="post" action="register_p3.php" onsubmit="return validateForm()" >
<div class="row date_of_birth">
<input type="text" id="date_of_birth" name="date_of_birth" placeholder="D.O.B 10/02/1990" />
</div>
<div class="row number">
<input type="text" id="contact_number" name="contact_number" placeholder="Mobile Number" />
</div>
<div class="row confirm">
<input type="text" id="confirm" name="confirm" placeholder="Are You a UK resident?" />
</div>
<input type="submit" value="Next >" />
</form>
page 3 / form3:
<?php
session_start();
// other php code here
$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;
$_SESSION['email'] = $email;
$_SESSION['dat_of_birth'] = $date_of_birth;
$_SESSION['number'] = $number;
?>
<form class="" method="post" action="register_p4.php">
<div class="row date_of_birth">
<input type="text" id="display_name" name="display_name" placeholder="Display Name" />
</div>
<div class="row password">
<input type="password" id="password" name="password" placeholder="Password" />
</div>
<div class="row password2">
<input type="password" id="password2" name="password2" placeholder="Password (Confirm)" />
</div>
<input type="submit" value="Next >" />
</form>
and then on the final page i try to collect all the information like so and input it into the database:
<? ob_start(); ?>
<?php
// GET ACCOUNT INFORMATION FROM FORM AND ASSIGN VARIABLES
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$date_of_birth = $_POST['date_of_birth'];
$contact_number = $_POST['contact_number'];
$display_name = $_POST['display_name'];
$password = $_POST['password'];
?>
<?php
/*
// ECHO ACCOUNT INFORMATION
echo "<strong> Account Information: </strong>";
echo "<br />";
echo First Name: ";
echo "<br />";
echo $first_name;
echo "<br />";
echo "<br />";
echo "Last Name: ";
echo "<br />";
echo $last_name;
echo "<br />";
echo "<br />";
echo "Email: ";
echo "<br />";
echo $email;
echo "<br />";
echo "<br />";
echo "Password: ";
echo "<br />";
echo $password;
echo "<br />";
echo "<br />";
echo "date_of_birth: ";
echo "<br />";
echo $date_of_birth;
echo "<br />";
echo "<br />";
echo "Contact_number: ";
echo "<br />";
echo $contact_number;
echo "<br />";
echo "<br />";
echo "display_name: ";
echo "<br />";
echo $display_name;
echo "<br />";
echo "<br />";
*/
?>
Upvotes: 0
Views: 2441
Reputation: 146310
Combine all of your forms into one form.
I see no issue with all of that information being in one form submit.
The way you have it now means (as you can tell) you need to keep track of what was submitted previously and keep it saved in the session files.
If you have everything in one form, you should not have an issue.
If you want it "split" over several pages then you can use some javascript magic to make it look as if the form is on multiple pages, but really would be one <form>
element, just with different fieldsets that are displayed only when the user is up to the next part of the form.
Upvotes: 3
Reputation:
It's answered, however:
change this on page 2:
<?php
session_start();
// other php code here
$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;
$_SESSION['email'] = $email;
?>
to:
<?php
session_start();
// other php code here
$_SESSION['first_name'] = $_POST['first_name'];
$_SESSION['last_name'] = $_POST['last_name'];
$_SESSION['email'] = $_POST['email'];
?>
and change code on page 3 to:
$_SESSION['dat_of_birth'] = $_SESSION['date_of_birth'];
$_SESSION['number'] = $_SESSION['number'];
?>
And so on, on other pages, just save $_SESSION['varname'] = $_POST['varname']; and finally, on your last page: change it to: (updated)
<?php ob_start(); ?>
<?php
session_start();
// GET ACCOUNT INFORMATION FROM FORM AND ASSIGN VARIABLES
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
$email = $_SESSION['email'];
$date_of_birth = $_SESSION['date_of_birth'];
$contact_number = $_SESSION['contact_number'];
$display_name = $_POST['display_name'];
$password = $_POST['password'];
?>
You really not need to make $first_name = $_SESSION['first_name']; just echo $_SESSION['first_name']; it saves memory...
Upvotes: 0
Reputation: 376
On the second page, you write
$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;
Since there is no $first_name on that page, you are overwriting the SESSION-vars with nothing. So it is correct that on the last page the SESSION-variables of the pages before are empty.
Store only the variables of the page before and it should work.
Other point: I really really hope you sanitize your variables before using them in a sql-query...?
Upvotes: 0