服务器验证发生之前,数据需要自动存储在html元素中 [英] data needs to be stored in html elements automatically before server validation occurs

查看:151
本文介绍了服务器验证发生之前,数据需要自动存储在html元素中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是有一个疑问:

我有一个在其中进行服务器端验证的表格..我具有验证的np,但概率是:

在一个表单中,假设我输入了大约9个字段(而不是10个),它向我显示了一个错误,说缺少第10个字段",我必须重新输入在9个字段中输入的所有数据.有时由于小错误,很难重新输入相同的数据)

因此,我们的目标是认为无论先前在textfield或textarea或其他html元素中输入的数据是什么,即使在服务器验证后也应自动保存.

我怎么能做到这一点.我在google上尝试过,但不了解如何搜索这种情景,我还没有找到任何解决方案.

很抱歉我的英语不好..希望你能理解我的问题.

那么,有什么建议吗??

解决方案

@Jim,情况与

因此,不必每次都发回以下内容...

<input type="text" id="mytext" name="mytext">

您需要发回最初在控件中输入的内容...

<input type="text" id="mytext" name="mytext"
   value="<?php echo $_POST['mytext']; ?>>

使用复选框和收音机(这是您最初的问题),您需要类似...

<input type="checkbox" id="mycheck" name="mycheck"
   value="<?php echo (isset($_POST['mycheck']) ? "checked" : ""); ?>>

在谈论<textarea>控件时,您会做类似...

<textarea id="mytext" name="mytext"><?php echo $_POST['mytext']; ?></textarea>


更新

在将HTML构建到$echo字符串变量中(而不只是直接标记HTML)时,您需要在字符串中包含上述代码 . >

如果您在构建字符串的过程中 执行了echo命令,则被echo修饰的内容将立即发送到屏幕.然后,当您通过命令echo $echo;发送$echo的内容时,您将获得所有其他内容.

因此,对于单行文本框,您可以...

$echo = '<input type="text" id="mytext" name="mytext" value="'. $_POST['mytext'] .'">';

对于复选框,您可以...

$echo = '<input type="checkbox" id="mycheck" name="mycheck" '. (isset($_POST['mycheck']) ? "checked" : "") .'>';

对于多行文本框,您可以......

$echo = '<textarea id="mytext" name="mytext">'. $_POST['mytext'] . '</textarea>';

因此,您看到我们将控件的现有值包含在要构建的字符串中.

I just had one doubt:

I have a form where I do server side validation.. i have np with validation, BUT the prob is:

In a Form, assume I entered some 9 fields (instead of 10 ), it shows me an erros saying that '10th field missing' and all the data I entered in 9 fields has to be re-entered again.. (sometimes its really damn tough to re-enter same data because of small mistake )

so, aim thinking that whatever the data entered previously in textfield or textarea or some other html element, it should be saved automatically even after server validation does.. so that user need not re-type again..

how can I achive this.. I tried in google, but didnt understood how to search for this kind of sittuation and i did not find any solution yet..

sory for my bad english.. Hope u udnerstood my prob..

so, any suggestions..?

解决方案

@Jim, this is the same situation as in your original question.
(Before anybody points out that it is a different account, it's the same actual person - long story!)

PHP will not keep the "state" of the control when you send it back to the browser after a post-back.

So instead of sending back the following each and every time...

<input type="text" id="mytext" name="mytext">

You need to send back what was originally entered as part of the control...

<input type="text" id="mytext" name="mytext"
   value="<?php echo $_POST['mytext']; ?>>

With checkboxes and radios (which was your original question), you need something like...

<input type="checkbox" id="mycheck" name="mycheck"
   value="<?php echo (isset($_POST['mycheck']) ? "checked" : ""); ?>>

And as you're talking about <textarea> controls, you'd do something like...

<textarea id="mytext" name="mytext"><?php echo $_POST['mytext']; ?></textarea>


Update

As you are building the HTML into the $echo string variable (rather than just having the HTML mark up directly), you need to include the above code as part of the string.

If you do an echo command during the building of the string, the thing being echoed will be sent to the screen straight away. Then when you send the contents of $echo via the command echo $echo; you get everything else.

So, for a single line textbox you would do...

$echo = '<input type="text" id="mytext" name="mytext" value="'. $_POST['mytext'] .'">';

For a checkbox you would do...

$echo = '<input type="checkbox" id="mycheck" name="mycheck" '. (isset($_POST['mycheck']) ? "checked" : "") .'>';

For a multi line textbox you would do....

$echo = '<textarea id="mytext" name="mytext">'. $_POST['mytext'] . '</textarea>';

So you see that we are including the existing value of the control as part of the string that you are building.

这篇关于服务器验证发生之前,数据需要自动存储在html元素中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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