Php 处理表单验证时避免代码重复使用

Php 处理表单验证时避免代码重复使用,php,Php,我正在生成表单并在同一个文件中处理提交事件 如果用户尚未输入标题,我希望再次显示表单并包含错误消息(例如,“您忘记了标题”) 这意味着我必须复制代码两次—一次显示空表单,第二次显示带正文的表单,并要求用户输入标题: <?php if(strlen(strip_tags($_POST['posttitle'])) == 0): // Display the form with question body that user has entered so far and ask use

我正在生成表单并在同一个文件中处理提交事件

如果用户尚未输入标题,我希望再次显示表单并包含错误消息(例如,“您忘记了标题”)

这意味着我必须复制代码两次—一次显示空表单,第二次显示带正文的表单,并要求用户输入标题:

<?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" />
?>

标题:

这里有一个快速的方法

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


您可以使用输出缓冲区获取表单,然后将其分配给变量,如下所示:

<?php

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

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

?>


希望这对您有所帮助

当您显示表单时,使用可能为空的
$\u POST
值作为标题和问题正文的默认字段值。如果其中一个为空,则表单将第二次显示,另一个已填写:

<?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>

读一下这个


您可以在视图中编写表单,在控制器中编写处理程序,在模型中编写业务逻辑

如果
REQUEST\u METHOD
POST
,我将验证输入并收集数组中的消息(
$errors

然后我会打印表单,如果有错误,代码会打印它

<?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>


可能使用变量而不是复制粘贴,例如,
if(form为空){$message='form为空'}elseif{$message=null;}然后在表单的某个地方显示变量,它只会在出现问题时显示,如果我不理解,很抱歉,这就是我使用注释的原因..你理解正确。我明白了。谢谢