我正在使用一些来自物化和一些PHP的CSS来构建一个“联系我们”表单。这是我到目前为止的代码。
HTML
<div class="row">
<form action="?" method="post">
<div class="row">
<div class="input-field col s6">
<input name="realfirstname" id="realfirstname" type="text">
<label class="active">First Name</label>
</div>
<div class="input-field col s6">
<input name="realsecondname" id="realsecondname" type="text" class="validate">
<label class="active" for="last_name">Last Name</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input name="email" id="email" type="text" class="validate">
<label class="active" for="last_name">Email</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input name="comments" id="comments" type="text" class="materialize-textarea">
<label class="active" for="textarea1">Comments</label>
</div>
</div>
<div class="row">
<button class="btn waves-effect waves-light" type="submit" name="send">Submit
</button>
</div>
</form>
</div>
</div>
我的PHP
<html>
<head>
<title>Thanks For Contacting Us</title>
</head>
<body>
<?php
$recipient = 'admin@example.com';
$email = $_POST['email'];
$realFirstName = $_POST['realfirstname'];
$realSecondName = $_POST['realsecondname'];
$subject = $_POST['comments'];
# We'll make a list of error messages in an array
$messages = array();
if (!preg_match("/^[\w\+\-.~]+\@[\-\w\.\!]+$/", $email)) {
$messages[] = "That is not a valid email address.";
}
if (!preg_match("/^[\w\ \+\-\'\"]+$/", $realName)) {
$messages[] = "The real name field must contain only " .
"alphabetical characters, numbers, spaces, and " .
"reasonable punctuation. We apologize for any inconvenience.";
}
$subject = preg_replace('/\s+/', ' ', $subject);
# Make sure the subject isn't blank afterwards!
if (preg_match('/^\s*$/', $subject)) {
$messages[] = "Please specify a subject for your message.";
}
$body = $_POST['body'];
# Make sure the message has a body
if (preg_match('/^\s*$/', $body)) {
$messages[] = "Your message was blank. Did you mean to say " .
"something?";
}
if (count($messages)) {
foreach ($messages as $message) {
echo("<p>$message</p>\n");
}
echo("<p>Click the back button and correct the problems. " .
"Then click Send Your Message again.</p>");
} else {
mail($recipient,
$subject,
$body,
"From: $realName <$email>\r\n" .
"Reply-To: $realName <$email>\r\n");
echo("<p>Your message has been sent. Thank you!</p>\n");
}
?>
</body>
</html>
当我在MAMP上运行时,我收到了所有错误消息!我不知道我做错了什么,我把头发拉出来了!
答案 0 :(得分:0)
嗯,对于初学者,你要分配$ realFirstName和$ realSecondName,但是你正在使用你的正则表达式和一个名为$ realName的变量,我无法在你的代码中看到任何其他地方。
检查$ body时遇到同样的问题,因为$ _POST [&#39; body&#39;]不在您的表单中的任何位置。
由于同样的原因,您的脚本会出现许多未定义的变量错误。