• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

php在当前页验证表单数据

PHP 水墨上仙 3059次浏览

php在当前页验证表单数据
URL: http://support.jodohost.com/showthread.php?t=4350

<?php
 
function VerifyForm(&$values, &$errors)
{
    // Do all necessary form verification
 
    if (strlen($values['name']) < 3)
        $errors['name'] = 'Name too short';
    elseif (strlen($values['name']) > 50)
        $errors['name'] = 'Name too long';
 
    // Needs better checking ;)
    if (!ereg('.*@.*\..{2,4}', $values['email']))
        $errors['email'] = 'Email address invalid';
 
    if (strlen($values['text']) == 0)
        $errors['text'] = 'Text required';
 
    return (count($errors) == 0);
}
 
function DisplayForm($values, $errors)
{
    ?>
    <html>
    <head>
        <title>Yadda yadda</title>
        <style>
            TD.error
            {
                color: red;
                font-weight: bold;    
            }
        </style>
    </head>
    <body>
 
    <?php
    if (count($errors) > 0)
        echo "<p>There were some errors in your submitted form, please correct them and try again.</p>";
    ?>
 
    <form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
    <table>
        <tr>
            <td>Name:</td>
            <td><input type="text" size="30" name="name" value="<?= htmlentities($values['name']) ?>"/>
            <td class="error"><?= $errors['name'] ?></td>
        </tr>
        <tr>
            <td>Email:</td>
            <td><input type="text" size="30" name="email" value="<?= htmlentities($values['email']) ?>"/>
            <td class="error"><?= $errors['email'] ?></td>
        </tr>
        <tr>
            <td valign="top">Text:</td>
            <td>
                <textarea name="text" cols="30" rows="6"><?= htmlentities($values['text']) ?></textarea>
            </td>
            <td class="error"><?= $errors['text'] ?></td>
        </tr>
        <tr><td colspan="2" align="center"><input type="submit" value="Submit"></tr>
    </table>
    </form>
 
    </body>
    </html>
    <?php
}
 
function ProcessForm($values)
{
    mail('foo@bar.com', 'Form test', $values['text'], "From: \"{$values['name']}\" <{$values['email']}>");
 
    // Replace with actual page or redirect :P
    echo "<html><head><title>Thank you!</title></head><body>Thank you!</body></html>";
}
 
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $formValues = $_POST;
    $formErrors = array();
 
    if (!VerifyForm($formValues, $formErrors))
        DisplayForm($formValues, $formErrors);
    else
        ProcessForm($formValues);
}
else
    DisplayForm(null, null);
?>


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明php在当前页验证表单数据
喜欢 (0)
加载中……