-2

我将首先说我不是编码员,并且真的需要一些帮助。

我的contact.php表单不断返回此错误消息:

Invalid email address entered 

当我在服务器上测试它时。

我已将此contact.php表格用于其他网站,并且效果很好。我读到 php 代码现在可能已被弃用,但我不确定如何修复它。

这是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(!preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i',$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 = "dimasir@yahoo.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!';
}
}
?>

编辑原始注释****这是我的 HTML 代码:

                <form method="post" action="contact.php"> 
                  <input type="hidden" name="send" value="1" />
                    <fieldset>                      
                        <label>First Name </label>
                        <input name="" type="text" class="filed1" />
                        <label>Last Name</label>
                        <input name="" type="text" class="filed1" />
                            <label>Your Email  </label>
                        <input name="" type="text" class="filed1" />
                        <label>Company</label>
                        <input name="" type="text" class="filed1" />
                        <label>Message </label>
                        <textarea name="" cols="" rows="" ></textarea>
                        <input type="image" src="images/send.jpg" />

                    </fieldset>
              </form>
            </div>
            <!--  \ CONTACT BOX / -->

          </div>

4

2 回答 2

1

改变你的线路,就像

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

进入

if(!preg_match('/^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)* @[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$/',$email )){.

这应该可以解决问题。

如果这不起作用,请尝试更改$email = $_POST['email'];$email = urldecode($_POST['email']);

于 2013-02-27T22:34:45.823 回答
1
if(!preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i',$email )){
    $error.="Invalid email address entered";
    $errors=1;
}

应该是这样的:

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $error .= "Invalid email address entered";
    $errors = 1;
}

PHP 有一个内置函数filter_var()来验证电子邮件地址,这比使用正则表达式更快、更准确。

使用此 HTML 代码(确保填写其余属性)

<label>First Name </label>
<input name="f_name" type="text" class="filed1" />
<label>Last Name</label>
<input name="l_name" type="text" class="filed1" />
    <label>Your Email  </label>
<input name="email" type="text" class="filed1" />
<label>Company</label>
<input name="company" type="text" class="filed1" />
<label>Message </label>
<textarea name="message" cols="" rows="" ></textarea>
<input type="image" src="images/send.jpg" />
于 2013-02-27T23:03:31.053 回答