在您阅读代码之前,我尝试将每个部分分离到它们自己的php文件中,只使用requires来获取代码,但是使用requires或将所有代码都保存在同一个文件中,不管如何,我似乎都得到了相同的错误。我认为这可能与我使用的PHP版本有关。
在后端部分的第3行,submit似乎出现了错误。是一个未定义的属性。
第二个是用户反馈部分上未定义的错误。
我以前使用过这个模板,并且已经成功地工作过。
我正在我的Windows8.1 Pro PC上使用WAMP运行PHP 5.4.12和Apache2.4.4。
如能提供任何帮助,将不胜感激。
/** BACKEND **/
<?php
if($_POST['submit'])
{
$fName=$_POST['fName'];
$topic=$_POST['topic'];
$email=$_POST['email'];
$message=$_POST['message'];
function verify_email($email)
{
if(!preg_match('/^[_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*@[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,4})$/',$email))
{
return false;
}
else
{
return $email;
}
}
function verify_email_dns($email)
{
list($name, $domain) = split('@',$email);
if(!checkdnsrr($domain,'MX'))
{
return false;
}
else
{
return $email;
}
}
if(verify_email($email))
{
if(verify_email_dns($email))
{
if ($fName=='')
{
header('location:./contact.php?error=missing');
}
elseif ($email=='')
{
header('location:./contact.php?error=missing');
}
elseif ($message=='')
{
header('location:./contact.php?error=missing');
}
else
{
foreach ($myvars as $var)
{
if (isset($_POST[$var]))
{
$var=$_POST[$var];
}
}
$subject = "Email Submission for review";
$add.="test.email@gmail.com";
$msg.="First Name: \t$fName\n";
$msg.="Email: \t$email\n";
$msg.="Topic: \t$topic\n";
$msg.="Message: \t$message\n";
$mailheaders="From: $email\n";
$mailheaders.="Reply-To: $email\n";
mail("$add", "$subject", $msg, $mailheaders);
header('location:./contact.php?error=none');
}//end else
}//end inner-if
else
{
header('location:./contact.php?error=mx');
}
}// end outter-if
else
{
header('location:./contact.php?error=format');
}
}// end starting if
/** VIEW for form **/
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="contactForm">
<label for="fName" class="first-name">Name:</label>
<input type="text" name="fName" value="" id="fName">
<br><br>
<label for="email" class="email-name">Email:</label>
<input type="text" name="email" value="" id="email">
<br><br>
<label for="topic" class="subject-name">Subject:</label>
<input type="text" name="topic" value="" id="topicsubject">
<br><br>
<label for="message" class="message-name">Message:</label>
<textarea name="message" rows="5" cols="60" id="message"></textarea>
<br><br>
<input type="submit" name="submit" id="submit-btn" class="submit-btn" value="Email Me">
</form>
/** USER FEEDBACK if error occurs **/
<?php
$error=$_GET['error'];
switch ($error)
{
case "mx":
echo "<br><span class='red'>Your email address you entered is invalid. Please try again.</span><br>";
break;
case "format":
echo "<br><span class='red'>Your email address is not in the correct format, it should look like name@domain.com. Please try again.</span><br>";
break;
case "missing":
echo "<br><span class='red'>You seem to be missing a required field, please try again.</span><br>";
break;
case "none":
echo "<br>Your email was sent. I will get back to you as soon as I can. Thank you for your interest.<br>";
break;
default:
echo "<br><br>";
}
?>发布于 2014-06-03 20:33:53
您假设在访问页面时存在POST和GET变量。因此,$_POST' submit‘只有在实际提交表单时才存在,否则在第一次访问该页面时会出现错误。
试一试这个条件:
if(isset($_POST['submit']) ) {
// now its safe to do something
}您永远不应该假设在访问页面时有任何$_POST或$_GET变量可用。
还取消了主题:在中,您使用的是“action”属性,该属性与您在这里访问的页面具有相同的url:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="contactForm">基本上,如果您只是将action属性全部删除,那么它将具有相同的效果,并且它的语义也是如此。这是一种更好的做法,也有同样的效果:
<form method="post" name="contactForm">您可以查看以前的堆栈溢出问题,以更好地解释该问题:
Is it a good practice to use an empty URL for a HTML form's action attribute? (action="")
https://stackoverflow.com/questions/24024225
复制相似问题