3
現在我一直在爲評論部分,登錄部分和發送電子郵件的網站創建表單。我使用的代碼來自我學習的教程,其中包含太多的HTML。這是我知道如何創建表單並驗證數據的唯一途徑。我想問問是否有更好的方式來創建表單,以及是否有方法在OOP PHP中創建表單?我想學會超越基本的東西到面向對象。在PHP中使用表單的最佳做法是什麼?你可以在表單中使用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:[email protected]">[email protected]</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= "[email protected]";
$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 -->
這很酷的例子。有沒有教程或沒有框架的方法?我現在想學習沒有框架,看看我能不能做到。 – blackbull77 2010-08-07 21:55:53