验证数据失败时,保留表单字段中的值 [英] Retaining values in forms fields when validation of data fails

查看:118
本文介绍了验证数据失败时,保留表单字段中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在确定失败时如何保留用户数据时遇到问题。我对PHP有些新鲜,所以我可能会在逻辑上犯一些大错。



目前,如果验证失败,则所有字段都将被清理干净,并且$ _Post数据也将消失。



是一些代码,假设用户输入一个无效的电子邮件,我希望名称字段被保留。

 <?php 
if($ _ POST ['doSubmit'] =='提交'){

$ usr_name = $ data ['Name'];
$ usr_email = $ data ['Email'];

if(isEmail($ usr_email)== FALSE){
$ err =电子邮件无效。);
header(Location:index.php?msg = $ err);
exit();


//用数据

}

做任何事情if(isset($ _ GET ['msg'])){
$ msg = mysql_real_escape_string($ _ GET ['msg']);
echo< div class = \msg\> $ msg< / div>< hr />;
}
if(isset($ _POST ['Name'])){
$ reusername = $ _POST ['Name'];}
else {$ reusername =NOTHING ;} //测试

?>
< form action =index.phpmethod =post>
< input name =UserNametype =textsize =30value =<?echo $ reusername;?>>
< input name =Emailtype =textsize =30>
< input name =doSubmittype =submitvalue =submit>
< / form>




}


解决方案

您可以使用AJAX将您的表单数据提交给您的PHP脚本,并让它返回指定验证是否成功的JSON数据。这样,你的领域将不会被擦干净。

另一种方法是将记录的参数返回到发布页面,并在发布页面中使用PHP填充字段。



然而,我认为第一种解决方案更好。

更新



编辑使你的代码更清晰,所以我注意到了一些东西。您的输入字段在HTML中称为 UserName ,但您在PHP中引用 Name 。这可能是因为它不工作。你的领域是否总是被值 NOTHING 填满?确保在 $ _ POST 中使用的输入字段和下标的名称相同。



另外如果有错误,则不需要重定向到另一个页面(使用标题)。维护 $ errors 数组或变量以在同一页面中打印错误消息。但就像我之前提到的那样,使用JSON方法可能会更好,因为您可以将视图图层(html)与PHP(控制器图层)分开。所以你会把你的HTML放在一个文件中,而你的PHP放在另一个文件中。


I am having problems figuring out how to retain users data when the validation fails. I am somewhat new to PHP so I might be making some huge mistakes in my logic.

Currently if the validation fails all the fields are wiped clean and $_Post data is also gone.

Here is some code assuming the user enters an invalid email I want the Name field to be retained. This code is not working.

<?php
if($_POST['doSubmit'] == 'Submit') {

   $usr_name = $data['Name'];
   $usr_email = $data['Email'];

   if (isEmail($usr_email)==FALSE){
       $err = "Email is invalid.");
       header("Location: index.php?msg=$err");
       exit();
    }

   //do whatever with data

}

if (isset($_GET['msg'])) {
   $msg = mysql_real_escape_string($_GET['msg']);
   echo "<div class=\"msg\">$msg</div><hr />";
}
if (isset ($_POST['Name'])){
   $reusername = $_POST['Name'];}
else{$reusername = "NOTHING";}//to test

 ?>
 <form action="index.php" method="post" >
 <input name="UserName" type="text" size="30" value="<?echo $reusername;?>">
 <input name="Email" type="text" size="30">
 <input name="doSubmit" type="submit" value="submit">
 </form>




 }

解决方案

You can use AJAX to submit your form data to your PHP script and have it return JSON data that specifies whether the validation was successful or not. That way, your fields won't be wiped clean.

Another way is to send back the recorded parameters to the posting page, and in the posting page, populate the fields using PHP.

However, I think the first solution is better.

UPDATE

The edit makes your code clearer and so I noticed something. Your input field is called UserName in the HTML, but you are referring to Name in PHP. That's probably why it's not working. Is your field always being filled with the value NOTHING? Make sure the name of the input field and the subscript you are using in $_POST are the same.

Also, there's no need to redirect to another page (using header) if you have an error. Maintain an $errors array or variable to print error messages in the same page. But like I mentioned before, it's probably better to use the JSON approach since then you can separate your view layer (the html) from the PHP (controller layer). So you'd put your HTML in one file, and your PHP in another file.

这篇关于验证数据失败时,保留表单字段中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆