dpoppu4300 2012-12-19 12:44
浏览 329
已采纳

如果发现错误,则需要在单击“提交”按钮后保留文本框中的值

heartily regards from me I need to retain values in text boxes after submit button on click if found error. basically the thing I want, if user left any fields blank and press save button then error message pop up and without refreshing the page it takes back the user to the form where he just have to fill the fields that were left blank... below is my code

<html>
<form method="post" action="">
Enter Name  :   <input type="text" name="name" /><br/>
Enter Password  :   <input type="password" name="pass" /><br/>
<input type="submit" name="save" value="Save" />
</form>
</html>
php code
<?php
if (isset($_POST["save"]))
{
$name = $_POST["name"];
$pass = $_POST["pass"];
if (($name == '') && ($pass == '')) 
{
echo "Fields Must Be Filled...";
exit();
}
if ($name == '') {
echo "Enter Name...";
exit();
}
if ($pass == '') {
echo "Enter Password...";
exit();
}
else
{        
echo "Your name " . $name;
echo "<br/>";
echo "Your Password " . $pass;
}
}
?>
  • 写回答

3条回答 默认 最新

  • doucheyi1347 2012-12-19 12:51
    关注

    First, a little easier approach for your POST handling. You can do a

    foreach( $_POST as $key => $value )
    {
       ${$key} = $value;
       //If you need to database process the data, you can put mysql_escape_string( $value );
    }
    

    Now, all your POSTs will be in variables with the name of the field.

    After processing them for errors and more, and you wish them to be in your form element's value. You can just use the variable

    <input type="text" name="email" value="<?=$email?>">
    

    Or, if you use the POST

    <input type="text" name="email" value="<?=$_POST['email']?>">
    

    Best regards. Jonas

    Working code :-)

    <?php
    if (isset($_POST["save"]))
    {
        //Run through all objects set in the POST array
        foreach( $_POST as $key => $value )
        {
            //Set a variable named the same as the input elements name, and with the value
            ${$key} = $value;
        }
    }
    
        $error = false;
        if( empty($name) && empty($pass) ) 
        { 
            $error = true;
            $message = "Fields must be filled...";
        }
        elseif( empty($name) )
        { 
            $error = true;
            $message = "Enter name...";
        }
        elseif( empty($pass) )
        {
            $error = true;
            $message = "Enter Password...";
        }
    
        if( $error == true && isset($message) )
        {
            echo $message."<br><br>";
        }
        else
        {
            echo "Your name " . $name;
            echo "<br/>";
            echo "Your Password " . $pass;
        }
    ?>
    <html><br><br>
    <form method="post" action="">
    Enter Name  :   <input type="text" name="name" value="<?=(isset($name) ? $name : "")?>"/><br/>
    Enter Password  :   <input type="password" name="pass" value="<?=(isset($pass) ? $pass : "")?>" /><br/>
    <input type="submit" name="save" value="Save" />
    </form>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?