Right now i been creating a form for a comment section, a Login section, and sending email for a site i'm doing. The code I using was from a tutorial I learned and has too much HTML in it. It was the only way I knew how to create a form and valid the data. I wanted to ask if there was a better way to create forms and if there was a way to create a form in OOP PHP? I want learn to go beyond basic stuff to OOP.
Here's an example of the code I using for the moment:
<?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 -->
Yes, there are 'better' ways of creating forms. You might want to look at how popular frameworks do it.
Here's example from 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', false, array(6, 20))
->setRequired(true)
->addFilter('StringToLower');
// Create and configure password element:
$password = $form->createElement('password', 'password');
$password->addValidator('StringLength', false, 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'));
精彩评论