-3

您好,我正在上传一个网站,并且联系表格工作正常(接收消息),但提交后的页面给了我一个错误

这是我的php代码

我只是在学习 HTML/CSS 我没有任何 PHP。修复这个错误会很好,请告诉我如何发布“感谢您的信息已收到”

提前感谢您的任何反馈,非常感谢您有美好的一天!

      <?php

       if(!$_POST) exit;

       $email = $_POST['email'];


       //$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i',                                              

    $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
   if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\ \.       

    [a-z]{2,}"."$",$email )){
    $error.="Invalid email address entered";
$errors=1;
    }
   if($errors==1) echo $error;
   else{
$values = array ('name','email','message');
$required = array('name','email','message');

$your_email = "xxxxxxxxxx@gmail.com";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:\n";

foreach($values as $key => $value){
  if(in_array($value,$required)){
    if ($key != 'subject' && $key != 'company') {
      if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit;      

    }
    }
    $email_content .= $value.': '.$_POST[$value]."\n";
  }
}

if(@mail($your_email,$email_subject,$email_content)) {
    echo 'Message sent!'; 
} else {
    echo 'ERROR!';
} 
     }
     ?>
4

1 回答 1

0

您的代码非常混乱。

无意冒犯,但你需要努力制作适当的缩进,这样你才能清楚地看到问题所在。

这是您的代码的更简洁版本。

$required = array('name','email','message');
$temp = '';
$err = array();
foreach ($required as $value) {
    if ($_POST[$value] == '') $err[] = 'Field '.$value.' is required';
    $temp .= $value.': '.$_POST[$value]."\n";
}

if (count($err) > 0) {
    echo implode('<br />',$err);
    exit;
}

$your_email = "xxxxxxxxx@gmail.com";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:\n".$temp;

if (mail($your_email,$email_subject,$email_content)) echo 'Message sent';
else echo 'Message not sent';
于 2013-03-10T02:17:14.097 回答