开发者

How to flag a missing field in PHP when redirecting back to the page with the submitted form?

I have a form in the footer of开发者_JS百科 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?


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

session_start();

// 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜