我正在尝试发送一张表格,它花了我3天的时间来设法构建!我真的很挣钱但学习。无论如何,我已经做了我认为正确但我的形式没有找到我?我希望这是一个简单的解决方案!代码如下,我非常感谢任何帮助:
<?php
// define variables and set to empty values
$addDateErr = $nameErr = $emailErr = $subjectErr = $messageErr = $questionErr = "";
$addDate = $name = $email = $subject = $message = $WebSearch = $SocialMedia = $WordOfMouth = $Other ="";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
//Date
if (empty($_POST["addDate"]))
{$comment = "";}
else
{$comment = test_input($_POST["addDate"]);}
//Name
if (empty($_POST["name"]))
{$nameErr = "Name is required";}
else
{
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name))
{
$nameErr = "Only letters and white space allowed";
}
}
//Email
if (empty($_POST["email"]))
{$emailErr = "Email is required";}
else
{
$email = test_input($_POST["email"]);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$emailErr = "Invalid email format";
}
}
//Subject
if (empty($_POST["subject"]))
{$comment = "";}
else
{$comment = test_input($_POST["subject"]);}
//Message
if (empty($_POST["message"]))
{$messageErr = "A message is required";}
else
{$comment = test_input($_POST["message"]);}
//Question
if (isset($_POST['question']))
{
$menuVar = $_POST['question'];
} else {
$menuVar = "----------";}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" id="form" action="send.php">
<p><span class="error">* required field.</span></p><br />
<!--<form method="post" id="form" action="<php echo htmlspecialchars($_SERVER["PHP_SELF"]);>">-->
<!--Date--><div class="contact-font">
Date Photography needed (if necessary)<br />
<input type="text" name="addDate" id="datepicker" size="25" value="<?php if(isset($_POST['addDate'])) {echo $_POST['addDate']; } ?>">
</div>
<!--Name--><div class="contact-font" style=" margin-top: 20px;">
<span class="asterix">* </span>Name:<br />
<input type="text" name="name" class="border" size="25" value="<?php if(isset($_POST['name'])) {echo $_POST['name']; } ?>">
<span class="error"><?php echo $nameErr;?></span>
</div>
<!--Email--><div class="contact-font" style=" margin-top: 20px;">
<span class="asterix">* </span>Email: (please double check enty)<br />
<input type="text" name="email" class="border" size="25" value="<?php if(isset($_POST['email'])) {echo $_POST['email']; } ?>"><span class="error">
<?php echo $emailErr;?></span>
</div>
<!--Subject--><div class="contact-font" style=" margin-top: 20px;">
Subject:<br />
<input type="text" name="subject" class="border" size="25" value="<?php if(isset($_POST['subject'])) {echo $_POST['subject']; } ?>">
</div>
<!--Message--><div class="contact-font" style=" margin-top: 20px;">
<span class="asterix">* </span>Message:<br />
<textarea cols="40" rows="10" name="message" class="border"><?php if(isset($_POST['message'])) {echo $_POST['message']; } ?></textarea>
<span class="error"><?php echo $messageErr;?></span>
</div><br />
<select name="question">
<option <?php if($menuVar=="----------") echo 'selected="selected"'; ?> value="----------">----------</option>
<option <?php if($menuVar=="WebSearch") echo 'selected="selected"'; ?> value="WebSearch">Web Search</option>
<option <?php if($menuVar=="SocialMedia") echo 'selected="selected"'; ?> value="SocialMedia">Social Media</option>
<option <?php if($menuVar=="Wordofmouth") echo 'selected="selected"'; ?> value="Wordofmouth">Word of mouth</option>
<option <?php if($menuVar=="Other") echo 'selected="selected"'; ?> value="Other">Other</option>
</select>
<div>
<input type="submit" value="Send" id="submit">
</div>
</form>
<script>
$(document).ready(function() {
$("#datepicker").datepicker({minDate: 129, maxDate: "+5Y", changeMonth: true,changeYear: true, showButtonPanel: true, dateFormat: 'dd MM yy'});
});
$('#form').on('submit', function(){
return $('#email').val() == $('#emailConfirmation').val();
});
</script>
<style type="text/css">
.asterix{color: #bc2021;font-size: 20px;}
.contact-font, #info-req, #opening-text{font-family: 'Raleway', sans-serif; font-weight: 100; color: #000;}
.border:focus{border: 2px solid #bc2021;}
#datepicker:focus{border: 2px solid #bc2021;}
.error {color: #FF0000;}
#marketing{border: 1px solid black; width: 150px; height: 22px;}
#submit{width: 100px; height: 25px; font-size: 15px;}
#submit:active{background-color: #bc2021;}
</style>
以下是send.php文件内容:
<?php
$addDate = $_POST['addDate'];
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$question = $_POST['question'];
$to = "some@email.com";
$subject = "Help!";
$body = "Please do not reply!";
mail($to,$subject,$body);
echo "Message Sent!<a href='index.html'>Home</a>";
?>