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条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧