在PHP中使用表单的最佳做法是什么?可以使用OOP PHP形式吗?

前端之家收集整理的这篇文章主要介绍了在PHP中使用表单的最佳做法是什么?可以使用OOP PHP形式吗? 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

现在,我正在为评论部分,登录部分创建表单,并为我正在做的网站发送电子邮件.我使用的代码来自我学习的教程,并且其中包含过多的HTML.这是我知道如何创建表单和验证数据的唯一方法.我想问问是否有更好的方法来创建表单,以及是否有一种方法可以在OOP PHP中创建表单?我想学习超越基本知识的面向对象操作.

这是我目前使用的代码示例:

<?PHP include 'includes/validation.PHP' ?>

<!-- START CONTENT -->
  <div id="mainContent">
  <div class="portContentTop"><img src="graphx/image.gif" alt="" border="0" /></div>
  <div class="portContent">
   <img src="graphx/image.gif" alt="Contact Me" border="0" /><h1 class="hidden">Contact</h1><br />
            If you wish to contact me,please fill out the form or send me an email at <a href="mailto:user@gmail.com">user@gmail.com</a>.
   <br />
             <? if( isset($_POST['send'])&& (!validateName($_POST['name']) || !validateEmail($_POST['email']) || !validateMessage($_POST['message']) ) ):?> 
            <div id="errorMessage">
             <ul>
                 <? if(!validateName($_POST['name'])):?>
                 <li>Your name must be 4 characters long.</li>
                    <? endif ?>
                    <? if(!validateEmail($_POST['email'])):?>
                    <li>Your email address has an error.</li>
     <? endif ?>
                    <? if(!validateMessage($_POST['message'])):?> 
                    <li>Your comment must be 10 characters long.</li>
                    <? endif ?>
             </ul>
            </div>
            <?PHP elseif(isset($_POST['send'])):?>
            <?PHP
    $name=strip_tags($_POST['name']);
    $email=strip_tags($_POST['email']);
    $message=strip_tags($_POST['message']);
    $subject= "A message from me was submitted by ".$name." ";
    $sentto= "user@gmail.com";

    $contents = "Name: ".$name. "\n".
       "Email: ".$email. "\n".
       "Message: ".$message. "\n";

    mail($sentto,$subject,$contents);

   ?>
            <div id="errorMessage">
                <ul class="errorValid">
                 <li>Your message has been sent.</li>
             </ul>
            </div>
            <? endif?>
   <form method="post" id="customForm" name="customForm" action="" >
             <table  border="0" align="center" cellpadding="0" cellspacing="0" style="padding:0px; margin:0px;">
                  <tr valign="top">
                    <td align="center"><img src="graphx/h_name.gif" border="0" alt="Name:" /></td>
                    <td><input class="contactInput" id="name" name="name" type="text" size="30" /></td>
                  </tr>
                  <tr valign="top">
                    <td align="center"><img src="graphx/h_email.gif" border="0" alt="Email:" /></td>
                    <td><input class="contactInput" id="email" name="email" type="text" size="30" /></td>
                  </tr>
                  <tr valign="top">
                    <td align="center"><img src="graphx/h_comments.gif" border="0" alt="Comments:" /></td>
                    <td><textarea id="message" name="message" cols="30" rows="15" wrap="off"  class="contactInput2"></textarea></td>
                  </tr>
                  <tr valign="top">
                    <td></td>
                    <td align="left"><input id="send" name="send" type="submit" value="Submit" ></td>
                  </tr>
               </table>
          </form>
 </div>
<!-- END CONTENT -->
最佳答案
是的,有很多“更好”的方式来创建表格.您可能想看看流行的框架是如何做到的.

这是Zend Framework的示例
http://www.framework.zend.com/manual/en/zend.form.quickstart.html

  $form = new Zend_Form();
  $form->setAction('/user/login')
       ->setMethod('post');

  // Create and configure username element:
  $username = $form->createElement('text','username');
  $username->addValidator('alnum')
           ->addValidator('regex',false,array('/^[a-z]+/'))
           ->addValidator('stringLength',array(6,20))
           ->setrequired(true)
           ->addFilter('StringToLower');

  // Create and configure password element:
  $password = $form->createElement('password','password');
  $password->addValidator('StringLength',array(6))
           ->setrequired(true);

  // Add elements to form:
  $form->addElement($username)
       ->addElement($password)
       // use addElement() as a factory to create 'Login' button:
       ->addElement('submit','login',array('label' => 'Login'));

猜你在找的MySQL相关文章