PHP表单验证和处理同一页面
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP表单验证和处理同一页面相关的知识,希望对你有一定的参考价值。
great to validate and process a form in the same page
<?php function VerifyForm(&$values, &$errors) { // Do all necessary form verification $errors['name'] = 'Name too short'; $errors['name'] = 'Name too long'; // Needs better checking ;) $errors['email'] = 'Email address invalid'; $errors['text'] = 'Text required'; } function DisplayForm($values, $errors) { ?> <html> <head> <title>Yadda yadda</title> <style> TD.error { color: red; font-weight: bold; } </style> </head> <body> <?php 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 class="error"><?= $errors['name'] ?></td> </tr> <tr> <td>Email:</td> <td class="error"><?= $errors['email'] ?></td> </tr> <tr> <td valign="top">Text:</td> <td> </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('[email protected]', '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; if (!VerifyForm($formValues, $formErrors)) DisplayForm($formValues, $formErrors); else ProcessForm($formValues); } else DisplayForm(null, null); ?>
以上是关于PHP表单验证和处理同一页面的主要内容,如果未能解决你的问题,请参考以下文章