开发者

display user entered data in the form before submitting to database

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开发者_Python百科" 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(); 
}

?>


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>


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.


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 "
"; 
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜