需要使用 php 的表单字段

发布于 2024-12-01 18:10:28 字数 784 浏览 1 评论 0原文

鉴于以下代码示例,我需要向 check_input 函数添加什么,以便它处理丢失/必需的表单字段。基本上,我想做的就是在最终用户尝试提交表单而不填写所有必填字段时,在表单顶部向他们显示一条错误消息,其中显示类似“标有 * 的字段是必需的”之类的内容。

如有任何帮助,我们将不胜感激,并提前感谢您的宝贵时间。

 // Don't post the form until the submit button is pressed.
if(isset($_POST['submit'])) {

  echo( 
   check_input($_POST['name']) . <br> .
   check_input($_POST['city']);

}

// check_input function
function check_input($data)
{
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data, ENT_QUOTES);
  return $data;
}

表格

<form action="test.php" method="post">
  <input type="text" name="name">
  <input type="text" name="city">
  <input type="submit" name="submit" value="submit">
</form>

Given the following code example, what would I need to add to the check_input function so that it deals with missing / required form fields. Basically, all I am trying to do is to show the end user an error message on the top of my form that says something like "Fields marked with a * are required" if they try to submit the form without filling out all the required fields.

Any help would be greatly appreciated and thank in advance for your time.

 // Don't post the form until the submit button is pressed.
if(isset($_POST['submit'])) {

  echo( 
   check_input($_POST['name']) . <br> .
   check_input($_POST['city']);

}

// check_input function
function check_input($data)
{
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data, ENT_QUOTES);
  return $data;
}

The Form

<form action="test.php" method="post">
  <input type="text" name="name">
  <input type="text" name="city">
  <input type="submit" name="submit" value="submit">
</form>

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

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

发布评论

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

评论(1

铜锣湾横着走 2024-12-08 18:10:28
<?php
// Don't post the form until the submit button is pressed.
$requiredFields = array('name', 'city');    // Add the 'name' for all required fields to this array
$errors = false;
if(isset($_POST['submit'])) 
{
    // Clean all inputs
    array_walk($_POST, 'check_input');

    // Loop over requiredFields and output error if any are empty
    foreach($requiredFields as $r) {
        if( strlen($_POST[$r]) == 0 ) {
            $errors = true;
            break;
        }
    }

    // Error/success check
    if( $errors == true ) {
        echo 'Fields marked with a * are required';
    }else{
        // no errors
        // ...
    }
}

// check_input function
function check_input(&$data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data, ENT_QUOTES);
    return $data;
}
?>

PS:我注意到您的 HTML 表单中存在引用不匹配的情况。该方法应为 method="post",而不是 method="post'

<?php
// Don't post the form until the submit button is pressed.
$requiredFields = array('name', 'city');    // Add the 'name' for all required fields to this array
$errors = false;
if(isset($_POST['submit'])) 
{
    // Clean all inputs
    array_walk($_POST, 'check_input');

    // Loop over requiredFields and output error if any are empty
    foreach($requiredFields as $r) {
        if( strlen($_POST[$r]) == 0 ) {
            $errors = true;
            break;
        }
    }

    // Error/success check
    if( $errors == true ) {
        echo 'Fields marked with a * are required';
    }else{
        // no errors
        // ...
    }
}

// check_input function
function check_input(&$data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data, ENT_QUOTES);
    return $data;
}
?>

PS: I noticed a quote mismatch in your form HTML. The method should read method="post", not method="post'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文