php - display user entered data in the form before submitting to database

90

I have a user form where user enters his data. After user enters data i should display it for his confirmation before submitting to database. I have written a code. if i display the data for user confirmation, it is not getting stored after confirmation. if i directly submit to the database it will get stored.

Please look onto my code and help me where i am doing wrong.

Here is my form

<form name="registration_form" method="post" action="user_confirm.php" 
    onsubmit="return Validate();">
    User Name:
<input type="text" name="username"/><br /><br />

    Password: 
    <input type="password" name="password" /><br /><br />
    Retype Password: 
    <input type="password" name="password_confirmation"><br /><br />
    Name:
    <input type="text" name="name" /><br /><br />
    Phone Number: 
    <input type="text" name="phone_number"><br />
    <br />
<input type="submit" value="Click here to confirm"  /><br />

    </form>

and here is my user_confirm.php

<?php
$username= $_POST['username'];
$name = $_POST['name'];
$phone_numbe = $_POST['phone_numbe'];

echo $username "<br>";
echo $name "<br>";
echo $phone_number " <br>";
?>

<form action="register2_db.php" method="post">
<input type="hidden" name="redirect_values" value="true"> 
<input type="submit" name="confirm" value="Confirm Details">
<input type="button" name="return" value="Return to data" onClick="javascript: window.history.back(-1)";>
</form>

Why this value is not getting stored in database after confirmation ?

Here is my register2_db.php

<?

$host="localhost"; // Host name 
$username="fissioni"; // Mysql username 
$password="password("; // Mysql password 
$db_name="fissioni_test"; // Database name 
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$query = 'insert into members (username,password,name,phone_number) 
                    values
                    (
                    "' . $_POST['username'] . '", 
                    "' . md5($_POST['password']) . '",
                    "' . $_POST['name'] . '",
                    "' . $_POST['phone_number'] . '",
                    )';

$result = mysql_query($query);

if($result){
    echo '<h1>Thank you</h1> <br> Go back to the main page <a href="index.html");';
}else{
    echo "ERROR: ".mysql_error(); 
}

?>
119

Answer

Solution:

This is because none of your confirmation values are in the form itself. You're just sending an empty form to your PHP file. Modifying your confirmation form like below to include those values in a hidden text field will fix it for you.

<form action="register2_db.php" method="post">
<input type="hidden" name="name" value="<?php echo $name?>"> 
<input type="hidden" name="password" value="<?php echo $password?>"> 
<input type="hidden" name="username" value="<?php echo $username?>"> 
<input type="hidden" name="phone_number" value="<?php echo $phone_number?>"> 

<input type="hidden" name="redirect_values" value="true"> 
<input type="submit" name="confirm" value="Confirm Details">
<input type="button" name="return" value="Return to data" onClick="javascript: window.history.back(-1)";>
</form>
84

Answer

Solution:

Values in $_POST stay only for the next request, so to send those values to another page you have 2 ways.
1. Put submitted data into hidden fields and they will get sent again after clicking "confirm".
2. Save data in the session and take it when you need it.

764

Answer

Solution:

You have created on first page. but i think you have not created it on user_confirm.php.

Pass all element in hidden format on user_confirm.php e.g

<form action='final.php' method = 'POST'>
<input type = 'hidden' name = 'username' value='<?php echo $username;?>' />
....
</form>
<?php
echo $username "
"; echo $name "
"; echo $phone_number "
"; 
?>

People are also looking for solutions to the problem: postgresql - PHP json_encode Problem with Backslash and Array Name

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.