私は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>