我对 php 很陌生,所以我需要一些帮助,将必填字段添加到我的联系表单中。我想显示一个很好的“错误”消息,告诉查看者在必填字段中输入一些内容。
这是我的 php 脚本:
<?php
if(isset($_POST['submit'])){
$to = "benlevygraphics@gmail.com";
$headers = "From: " . $_POST['email'];
$subject = "Ben, you have been contacted...";
$body = "Name: " . $_POST['name'] . "\nEmail: " . $_POST['email'] . "\nWebsite: " . $_POST['web'] . "\n" . "\nFavorite Piece of work: " . $_POST['favwork'] . "\n" . "\nMessage: " . $_POST['message'];
if(mail($to, $subject, $body, $headers)){
echo("<p class=contactformsent>".$_POST['name'].", your information was received. For your records an email has also been sent to you!</p>");
}
else{
echo("<p class=contactformnotsent>".$_POST['name'].", Message delivery failed...</p>");
}
}
if(isset($_POST['submit'])){
$to = $_POST['email'];
$headers = "From: benlevygraphics@gmail.com" ;
$subject = "You contacted benlevywebdesign";
$body = $_POST['name'].", Thank you for taking a look at my portfolio and contacting me. I have received your contact information/message and I will get back to you as soon as I can!" . "\n" . "\nFor your records I have:" . "\nEmail: " . $_POST['email'] . "\nWebsite: " . $_POST['web'] . "\n" . "\nFavorite Piece of work: " . $_POST['favwork'] . "\n" . "\nMessage: " . $_POST['message'];
if(mail($to, $subject, $body, $headers)){
}
}
?>
这是html代码:
<?php include("contactform.php"); ?>
<form id="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<fieldset>
<p class="form">Name*
<input type="text" name="name" id="name" size="30"/>
</p>
<p class="form">Email*
<input type="text" name="email" id="email" size="30" />
</p>...(the rest of the form html code here)
然后是最后几行:
<p class="submit"><button name="submit" type="submit">Send</button></p>
</form>