2011-07-27 02:09
浏览 87


I have a form in the footer of my website that appears on each page. I need to make it so if a user submits the form with blank fields, it will flag the error message in the form. It currently directs them to a different page where there data is submitted to another server. I'm already using JavaScript for this, but need to add an additional validation layer with server-side code.

I know how to detect if the form fields are empty, but how can I use server-side code to redirect back to the referring page and include some parameters that can be used in the footer to detect which fields were empty?

I'm assuming I'd have to use PHP's header() with the http_referer, but how can I pass the parameters back to that page without anything showing up in the URL?

图片转代码服务由CSDN问答提供 功能建议

我的网页页脚中有一个表单出现在每个页面上。 我需要这样做,如果用户提交带有空白字段的表单,它将在表单中标记错误消息。 它目前将它们定向到另一个页面,其中数据被提交给另一个服务器。 我已经在使用JavaScript,但需要添加一个带服务器端代码的额外验证层。

我知道如何检测表单字段是否为空,但我怎么能 使用服务器端代码重定向回引用页面并包含一些可以在页脚中使用的参数来检测哪些字段为空?

我假设我必须 将PHP的header()与http_referer一起使用,但是如何将参数传递回该页面而不会在URL中显示任何内容?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douluan1533
    douluan1533 2011-07-27 02:14

    Keep a record in $_SESSION of the invalid/empty fields and flag them with CSS in your form:

    // When you begin validating your form, clear out any old fields from $_SESSION
    $_SESSION['previous'] = array();
    $_SESSION['invalid'] = array();
    // when you encounter an empty or invalid field,
    // add it to an array in $_SESSION
    $_SESSION['invalid']['fieldname'] = TRUE;
    // Store the previous POST value too
    $_SESSION['previous']['fieldname'] = $_POST['fieldname'];

    In your form, you can check if there's an invalid field and add a CSS class to indicate invalidity. This sets the class invalid:

    <input name='fieldname' type='text'
      class='<?php if (isset($_SESSION['invalid']['fieldname'])) echo "invalid";?>' 
      value='<?php if (isset($_SESSION['previous']['fieldname'])) echo htmlentities($_SESSION['previous']['fieldname'], ENT_QUOTES); ?>'

    Do this for all your form fields. Use the same method to retain the previous $_POST values and echo them out into the field's value attribute.

    点赞 评论
  • dtlh12053
    dtlh12053 2011-07-27 02:21

    I don't think there is a need to send them to another page. As you already know data validation is easy to do using Javascript, and PHP.

    1. you should check the POSTed form data at the top of the file.
    2. Validate the data. Put errors into an array so you can echo the errors at anytime.
    3. Check to see if you found any errors.

         // Loop through the $_POST array, which comes from the form...
      $errors = array();
          foreach($_POST AS $key => $value)
      // first need to make sure this is an allowed field
          if(in_array($key, $allowedFields))
          $$key = $value;
          // is this a required field?
          if(in_array($key, $requiredFields) && $value == '')
              $errors[] = "The field $key is required.";


    4. If there are errors, redisplay the page and list the errors so the users know what they did wrong, and can fix them. This method also allows you to repopulate the form fields with saved values so if there is an error the user doesn't have to type everything in again.

    点赞 评论