在你阅读代码之前,我已经尝试将每个部分分成他们自己的php文件,只是使用require来获取代码,但是使用require或者在同一个文件中拥有所有代码我似乎得到了相同的错误,无论如何。我认为这可能与我正在使用的PHP版本有关。
我似乎在BACKEND部分第3行的提交时收到错误。是一个未定义的财产。
第二个是USER FEEDBACK部分中未定义的错误。
我之前使用过这个模板并且已成功运作。
我在Windows 8.1 Pro PC上使用WAMP运行PHP 5.4.12和Apache 2.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>";
}
?>
答案 0 :(得分:2)
您假设在访问该页面时有POST和GET变量。因此,当您实际提交表单时,$ _POST [&#39; submit&#39;]才有可能存在,否则您在首次访问该页面时会出现错误。
尝试这种情况:
if(isset($_POST['submit']) ) {
// now its safe to do something
}
在访问页面时,您永远不应该假设任何$ _POST或$ _GET变量可用。
也偏离主题: 在您的HTML中,您正在使用&#39;操作&#39;属性与您在此行访问的页面具有相同的网址:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="contactForm">
基本上,如果你只是将动作属性放在一起,它将具有相同的效果,并且它也具有语义。这是一种更好的方法,它具有相同的效果:
<form method="post" name="contactForm">
您可以查看之前的Stack Overflow问题以获得更好的解释:
Is it a good practice to use an empty URL for a HTML form's action attribute? (action="")