php 在当前页验证表单数据的解决办法
内容摘要
这篇文章主要为大家详细介绍了php 在当前页验证表单数据的简单示例,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:
<?p
对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:
<?p
文章正文
这篇文章主要为大家详细介绍了php 在当前页验证表单数据的简单示例,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | <code class = "php" > <?php /** * 验证表单数据 * * @param * @arrange (www.idcnote.com) **/ 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教程(www.idcnote.com) ***/ ?></code> |
注:关于php 在当前页验证表单数据的简单示例的内容就先介绍到这里,更多相关文章的可以留意
代码注释