【问题标题】:PHP form sending error [closed]PHP表单发送错误[关闭]
【发布时间】:2013-02-27 22:28:31
【问题描述】:

首先我会说我不是编码员,我真的很想得到一些帮助。

我的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>

【问题讨论】:

标签: php regex forms email contact-form


【解决方案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" />

【讨论】:

  • 我试过这个并收到错误消息 Invalid email address....
  • @user1489550 在运行上述代码并在此处发布输出之前执行此操作var_dump($email);
  • 对不起,我需要一些帮助,我在哪里放置代码?它应该在contact.php 上吗?如果有,具体在哪里?
  • 放在 if 语句之前
  • @LukasBijamines 是的,但是我在 interwebz 上看到了一些奇怪的代码,人们在表单中错误地同时使用 POST 和 GET,所以在我的调试中只是一般,因为没有 HTML 表单代码,它是我们的所有猜测。
【解决方案2】:

改变你的行,就像

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'])

【讨论】:

  • 您好,感谢您的回复。我尝试更改 preg 匹配编码,但在第 9 行收到 Parse error: syntax error, unexpected contact.php。编码更改在第 9 行。这是我替换的行。然后我将第 9 行改回原来的,并进行了您建议的第二次更改,并再次收到 Invalid email address entered 错误...您还有其他想法吗?谢谢。
  • 我做了一个小改动,请立即尝试
  • 您不应该为此使用正则表达式,因为 PHP 具有对验证电子邮件地址的内置支持。不要使用低效和不准确的方法重新发明轮子。
  • @crypticツ 我同意你的解决方案
  • 我已经尝试了正则表达式,但我仍然收到相同的错误消息。您还有其他建议吗?
猜你喜欢
  • 2016-11-11
  • 1970-01-01
  • 2014-02-27
  • 2013-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多