开发者

Avoid code re-use when handling form validation

开发者 https://www.devze.com 2023-02-15 20:18 出处:网络
I am generating form and handling the submit event in the same file. If user has not entered the title, I want开发者_运维百科 to display the form again and include an error message (e.g. \"You forgot

I am generating form and handling the submit event in the same file.

If user has not entered the title, I want开发者_运维百科 to display the form again and include an error message (e.g. "You forgot the title.").

That means that I have to duplicate code twice - once to diplay empty form and second to display form with body and ask user to enter title:

<?php if(strlen(strip_tags($_POST['posttitle'])) == 0):
    // Display the form with question body that user has entered so far and ask user to enter title.
?>
    <label for="title"><b>Title:</label><br/>
    <input type="text" name="posttitle" id="posttitle" />           
<?php endif;?>

<?php elseif ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action']) && $_POST['action'] == 'post') : ?>
<!-- Everything ok - insert post to DB -->
<?php else : 

   // just display form here (again ouch!)
    <label for="title"><b>Title:</label><br/>
    <input type="text" name="posttitle" id="posttitle" />
?>


I would do it like this:

If REQUEST_METHOD is POST I will validate the input and collect messages in an array ($errors in my code).

Then I would just print the form and if there was an error the code will print it.

<?php

$errors = array();

function print_value_for($attr) {
    if (isset($_POST[$attr]))
        echo $_POST[$attr];
}

function print_error_for($attr) {
    global $errors;

    if (isset($errors[$attr]))
        echo $errors[$attr];
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // do validation here and add messages to $errors
    // like $errors['posttitle'] = "The title you entered is bad bad bad";

    if (empty($errors)) {
        // update database and redirect user
    }
}
?>

<!-- display the form and print errors if needed -->
<form>
    <?php print_error_for('posttitle'); ?>
    <input name="posttitle" type="text" value="<?php print_value_for('posttitle') ?>">

    <?php print_error_for('postauthor'); ?> 
    <input name="postauthor" type="text" value="<?php print_value_for('posttitle') ?>">

    <?php print_error_for('postbody'); ?>   
    <textarea name="postbody">
        <?php print_value_for('posttitle') ?>
    </textarea>

    <input type="submit">
</form>

PS. Consider using MVC to separate code and templates.


Here is a quick way to do that.

<form>
    <input type="text" name="title" value="<?php echo $_REQUEST['title']; ?>"/>
    <input type="text" name="field_a" value="<?php echo $_REQUEST['field_a']; ?>"/>
    ....
</form>

But I can also advise you to display a var called $title which is the result of a check on $_REQUEST['title].


You could use an output buffer to grab the form and then assign it to a variable like so:

<?php

ob_start();
include('path/to/your/form');
$form = ob_get_flush();

// then later you can just go
print $form;

?>

Hope this helps


When you display the form, use the possibly empty $_POST values as default field values for both the title and question body. If either is empty, the form will display the second time with the other already filled in:

<?php
$message = "";
if (empty($_POST['title'])) $message .= " Please enter a title.";
if (empty($_POST['body'])) $message .= " Please enter a body.";
?>

<form action='me.php'>
  <input name='title' type='text' value='<?php if (!empty($_POST['title'])) echo htmlentities($_POST['title'], ENT_QUOTES); ?>' />
  <textarea name='body'><?php if (!empty($_POST['body'])) echo $_POST['body']; ?></textarea>
</form>


Read this MVC

Your can write form in view, handler in controller, and business logic in model

0

精彩评论

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

关注公众号