php中联系人表单功能的问题
在您阅读代码之前,我已经尝试将每个部分划分为各自的php文件,并仅使用requires获取代码,但是使用requires或将所有代码放在同一个文件中,我似乎都会遇到相同的错误。我认为这可能和我使用的PHP版本有关 我似乎在后端部分第3行的提交时出错。属于未定义的属性 第二个错误是用户反馈部分中未定义的错误 我以前使用过此模板,并已成功使用 我正在Windows 8.1 Pro PC上使用WAMP运行PHP5.4.12和Apache 2.4.4 任何帮助都将不胜感激php中联系人表单功能的问题,php,forms,web,contact,Php,Forms,Web,Contact,在您阅读代码之前,我已经尝试将每个部分划分为各自的php文件,并仅使用requires获取代码,但是使用requires或将所有代码放在同一个文件中,我似乎都会遇到相同的错误。我认为这可能和我使用的PHP版本有关 我似乎在后端部分第3行的提交时出错。属于未定义的属性 第二个错误是用户反馈部分中未定义的错误 我以前使用过此模板,并已成功使用 我正在Windows 8.1 Pro PC上使用WAMP运行PHP5.4.12和Apache 2.4.4 任何帮助都将不胜感激 /** BACKEND **/
/** 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>";
}
?>
/**后端**/
您假设在访问页面时存在POST和GET变量。因此$_POST['submit']可能只在您实际提交表单时存在,否则您在第一次访问该页面时会出现错误
请尝试以下条件:
if(isset($_POST['submit']) ) {
// now its safe to do something
}
在访问页面时,决不能假设任何$\u POST或$\u GET变量都可用
也离题:
在HTML中,您使用的“action”属性的url与您在此行中访问的页面的url相同:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="contactForm">
为了避免未定义的偏移错误,请在尝试访问偏移之前尝试检查是否存在偏移。这里的例子:我完全没有想到,这是一个简单而简单的解决方案。非常感谢你。
<form method="post" name="contactForm">