【问题标题】:php form doesnt submit one of the fieldsphp表单不提交字段之一
【发布时间】:2010-04-04 08:30:24
【问题描述】:

我购买了一个内置联系表单的模板 问题是它提交了除“公司”名称之外的所有内容 我搞砸了,但无法开始工作。 如果你能指出一些解决方案,我会很高兴

提前致谢

这是 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 = "--------@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 all required fields, marked with *'; exit; }
        }
        $email_content .= $value.': '.$_POST[$value]."\n";
      }
    }

    if(@mail($your_email,$email_subject,$email_content)) {
        echo 'Thanks for your message, will contact you soon.'; 
    } else {
        echo 'ERROR!';
    }
}

【问题讨论】:

    标签: php email forms


    【解决方案1】:
    $values = array ('name','email','message');
    

    在 PHP 脚本中将 'company' 添加到此列表中。

    另外,我会将foreach 循环修改为如下所示,以获得预期的功能:

    foreach($values as $key => $value)
    {
        if(in_array($value,$required))
        {
            if(empty($_POST[$value]))
            {
                    echo 'Please fill in all required fields, marked with *';
                    exit;
            }
        }
        $email_content .= $value.': '.$_POST[$value]."\n";
    }
    

    这样,$required 数组中不存在的字段仍会添加到电子邮件中(如果它们存在)——它们只是不必通过空检查。

    【讨论】:

    • 我试过了,但是表单现在不会发送电子邮件,也添加到这个数组会使公司成为表单中的必填字段? $required = array('name','email','message');
    • 然后仅将其添加到 $values 中。这就是告诉脚本哪些字段需要放入电子邮件消息(如果是 foreach 循环)。
    • @Marc B 抱歉,这没有帮助,表单不会提交
    • 然后看看你之前发布的javascript。如果字段/值到达服务器,PHP 可以处理它。它不会选择性地忽略一个字段,因为它不喜欢你的古龙水之类的。
    • 从发布的代码来看,由于将字段附加到电子邮件内容数组的调用位于 if(in_array($value, $required)) 块内,因此它实际上仅包含电子邮件中所需的字段作为编码。我觉得这很奇怪。
    【解决方案2】:

    问题出在这一行:

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

    除非这条线,否则永远不会到达:

    if (in_array($value,$required)) {
    

    ...满意。这意味着,只有$required 中列出的字段才会附加到电子邮件中。如果没问题,那么只需更改以下几行:

    $required = array('name','email','message');
    $values = array ('name','email','message');
    

    阅读:

    $required = array ('name','email','message','company');
    $values = array ('name','email','message','company');
    

    如果不合适,请更改:

    foreach ($values as $key => $value) {
        if (in_array($value,$required)) {
            if ($key != 'subject' && $key != 'company') {
                if (empty($_POST[$value])) {
                    echo 'Please fill in all required fields, marked with *';
                    exit;
                }
            }
            $email_content .= $value.': '.$_POST[$value]."\n";
        }
    }
    

    看起来像这样:

    foreach ($values as $key => $value) {
        if (in_array($value,$required)) {
            if ($key != 'subject' && $key != 'company') {
                if (empty($_POST[$value])) {
                    echo 'Please fill in all required fields, marked with *';
                    exit;
                }
            }
        }
        $email_content .= $value.': '.$_POST[$value]."\n";
    }
    

    然后您可以将company$required 中取出,但将其保留在$values 中。我怀疑该模板在您开始时工作正常,因为所有字段都是必需的

    如果它仍然不起作用,请更改此(顶部):

    if(!$_POST) exit;
    

    看起来像这样:

    if(!$_POST) exit;
    print_r($_POST);
    

    ...并将附加输出粘贴到您的问题中。

    另外,考虑从其他供应商处购买模板 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-14
      • 2011-12-24
      • 1970-01-01
      • 1970-01-01
      • 2014-07-24
      • 2014-12-20
      • 1970-01-01
      相关资源
      最近更新 更多