donljt2606 2014-10-27 21:07
浏览 7

发送PHP联系表单时出错

Hope you can help me. First let me be clear that I have very little php knowledge. I know how to edit php so I can add my information (like an email for a contact form) but aren't really a php coder. My respect goes out to experts like you & I hope you can help me.

I am using a simple php contact form that validates the information entered (to make sure something has been entered into the fields). Up to this point it works perfectly - validating that information has been entered... if not it provides an error message letting you know that you have to enter some sort of information.

The error I am having... is that when all of the fields are entered & you hit the submit button... the page it goes to send the information doesn't load & remains a blank page. I am not sure what the error is being caused from & it only happens when the message is to be sent (no error happens when validating the fields have been entered).

Please see the code below. Thanks.

<?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;
}
?>

All the best, Anton

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 mmocr的训练错误,结果全为0
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀