表单值不应清晰

发布于 2024-12-05 18:37:22 字数 229 浏览 0 评论 0原文

我有一张表格可以提交我的联系信息。 我使用 php 获取表单字段,例如:

if(isset($_post['submit']))
{
//submit cantact info
}
else
{
//bad user
}

我的问题是:如果用户未能提交表单,则“表单值不应清除”....

但是单击提交按钮时表单值将被清除..!那么有人知道如何防止清晰的表单值吗?

I have a form to submit my contact info.
and i'm fetching form fields using php like:

if(isset($_post['submit']))
{
//submit cantact info
}
else
{
//bad user
}

my problem is: if the user is failed to submit form the "form values should not clear"....

but form values are clearing on click submit button..! so anyone know how to prevent clear form values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回忆凄美了谁 2024-12-12 18:37:22

您必须手动填充这些字段。

总体思路是

在收到 POST 数据后使用 POST/Redirect/GET 方法,您必须检查它并引发错误标志
如果出现某些错误,您必须显示相同的表单,其中包含填写的输入和相应的错误消息。

这是一个粗略的示例:

<?  
$err = array();
if ($_SERVER['REQUEST_METHOD']=='POST') {  

  //performing all validations and raising corresponding errors
  if (empty($_POST['name']) $err[] = "Username field is required";  
  if (empty($_POST['text']) $err[] = "Comments field is required";  

  if (!$err) {  
    // if no errors - saving data 
    // and then redirect:
    header("Location: ".$_SERVER['PHP_SELF']);
    exit;
  }  else {
    // all field values should be escaped according to HTML standard
    foreach ($_POST as $key => $val) {
      $form[$key] = htmlspecialchars($val);
    }
} else {
  $form['name'] = $form['comments'] = '';  
}
include 'form.tpl.php';
?>  

然后在 form.tpl.php 模板中将其设置为如下:

<? if ($err): ?>
  <? foreach($err as $e): ?>
<div class="err"><?=$e?></div>
  <? endforeach ?>
<? endif ?>
<form>
  <input type="text" name="name" value="<?=$form['name']?>">
  <textarea name="comments"><?=$form['comments']?></textarea>
  <input type="submit">
</form>

you have to populate these fields manually.

the general idea stands for using POST/Redirect/GET method

after receiving POST data you have to check it and raise error flag
and in case of some errors you have to show the same form back, with filled inputs and corresponding error messages.

here is a rough example:

<?  
$err = array();
if ($_SERVER['REQUEST_METHOD']=='POST') {  

  //performing all validations and raising corresponding errors
  if (empty($_POST['name']) $err[] = "Username field is required";  
  if (empty($_POST['text']) $err[] = "Comments field is required";  

  if (!$err) {  
    // if no errors - saving data 
    // and then redirect:
    header("Location: ".$_SERVER['PHP_SELF']);
    exit;
  }  else {
    // all field values should be escaped according to HTML standard
    foreach ($_POST as $key => $val) {
      $form[$key] = htmlspecialchars($val);
    }
} else {
  $form['name'] = $form['comments'] = '';  
}
include 'form.tpl.php';
?>  

and then in the form.tpl.php template make it like this:

<? if ($err): ?>
  <? foreach($err as $e): ?>
<div class="err"><?=$e?></div>
  <? endforeach ?>
<? endif ?>
<form>
  <input type="text" name="name" value="<?=$form['name']?>">
  <textarea name="comments"><?=$form['comments']?></textarea>
  <input type="submit">
</form>
失眠症患者 2024-12-12 18:37:22

您必须(手动)将提交的值放入表单元素中。例子:

<input type="text" name="username" value="<?=( isset( $_POST['username'] ) ? $_POST['username'] : '' )?>" />

You have to (manually) put the submitted values in the form elements. Example:

<input type="text" name="username" value="<?=( isset( $_POST['username'] ) ? $_POST['username'] : '' )?>" />
旧竹 2024-12-12 18:37:22

您需要跟踪表单值。一个建议是设置一组在呈现表单标记时使用的默认值。

在 POST 上,您可以合并发布数据。例如

$formData = array(
    'foo' => '',
    'bar' => 'default value',
    'baz' => ''
);

if ('POST' == $_SERVER['REQUEST_METHOD') {
    $formData = array_merge($formData, $_POST);

    // do validation, handle success
}

,然后,在 HTML 中

<input name="foo" value="<?php echo htmlspecialchars($formData['foo']) ?>">
<!-- etc -->

You need to keep track of the form values. One suggestion is to setup an array of default values that is used when presenting the form markup.

On POST, you then merge the post data. For example

$formData = array(
    'foo' => '',
    'bar' => 'default value',
    'baz' => ''
);

if ('POST' == $_SERVER['REQUEST_METHOD') {
    $formData = array_merge($formData, $_POST);

    // do validation, handle success
}

Then, in HTML

<input name="foo" value="<?php echo htmlspecialchars($formData['foo']) ?>">
<!-- etc -->
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文