发送PHP联系表单时出错

时间:2014-10-27 21:07:55

标签: php forms

希望你能帮助我。首先让我明确一点,我的PHP知识很少。我知道如何编辑php,所以我可以添加我的信息(如联系表格的电子邮件)但不是真正的PHP编码器。我尊重像你这样的专家。我希望你能帮助我。

我正在使用一个简单的php联系表单来验证输入的信息(以确保在字段中输入了某些内容)。到目前为止,它完美地运行 - 验证输入的信息...如果没有,它会提供一条错误消息,告诉您必须输入某种信息。

我遇到的错误是,当输入所有字段时&你点击了提交按钮...它发送信息的页面没有加载&仍然是一个空白页面。我不确定是什么错误是由&它只发生在要发送消息时(验证输入字段时没有错误发生)。

请参阅下面的代码。感谢。

<?php // Initialize variables to null.
$name =""; // Sender Name
$email =""; // Sender's email ID
$phone =""; // Subject of mail
$message =""; // Sender's Message
$nameError ="";
$emailError ="";
$phoneError ="";
$messageError ="";
$successMessage =""; // On submitting form below function will execute.
if(isset($_POST['submit'])) { // Checking null values in message.
if (empty($_POST["name"])){
$nameError = "Name is required";
}
else
 {
$name = test_input($_POST["name"]); // check name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name))
{
$nameError = "Only letters and white space allowed";
}
} // Checking null values in the message.
if (empty($_POST["email"]))
{
$emailError = "Email is required";
}
else
 {
$email = test_input($_POST["email"]);
} // Checking null values in message.
if (empty($_POST["phone"]))
{
$phoneError = "Your Phone Number is required";
}
else
{
$phone = test_input($_POST["phone"]);
} // Checking null values in message.
if (empty($_POST["message"]))
{
$messageError = "Message is required";
}
else
 {
$message = test_input($_POST["message"]);
} // Checking null values in the message.
if( !($name=='') && !($email=='') && !($phone=='') && !($message=='') )
{ // Checking valid email.
if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$header = $name."<". $email .">";
$headers = "ab@abc.com"; /* Let's prepare the message for the e-mail */
$msg = "Hello! $name... Thank You For Contacting us.
Name: $name
E-mail: $email
Phone: $phone
Message: $message
We Will be in contact with You as soon as possible (usually 24 Hours or Less).";
$msg1 = " $name Contacted Us. Here is some information about $name.
Name: $name
E-mail: $email
Phone: $phone
Message: $message "; /* Send the message using mail() function */
if(mail($email, $headers, $msg ) && mail("abc@abc.com", $header, $msg1 ))
{
$successMessage = "Message sent successfully.......";
}
}
else
{ $emailError = "Invalid Email";
 }
 }
} // Function for filtering input values. function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

一切顺利, 安东

0 个答案:

没有答案