PHP表单协助

时间:2014-04-22 00:50:47

标签: php html forms

第一件事 - My Career Page的工作申请表。

第二件事 - 链接到预先制作的AJAX表格FancyForm

现在,我还有一个表单工作正常的联系页面。我知道我的问题非常小,我似乎无法确定它。当我填写表单时,我收到错误消息:

  

消息字段太短或空了!

查看我的submit.php,我看到了:

if(!checkLen('message'))
  $err[]='The message field is too short or empty!';

查看表单的HTML,我没有任何消息的ID。对于我的生活,我无法弄清楚为什么当每个领域都完成后,它仍会返回此消息。

HTML:

<div id="main-container">
        <div id="form-container">
                            <h2>Please Fill Out</h2>

                            <form id="contact-form" name="contact-form" method="post" action="submit.php">
                              <table width="100%" border="0" cellspacing="0" cellpadding="5">
                                <tr>
                                  <td width="15%"><label for="name">Name</label></td>
                                  <td width="70%"><input type="text" class="validate[required,custom[onlyLetter]]" name="name" id="name" value="" /></td>
                                  <td width="15%" id="errOffset">&nbsp;</td>
                                </tr>
                                <tr>
                                  <td width="15%"><label for="address">Address</label></td>
                                  <td width="70%"><input type="text" class="validate[required,custom[a-zA-Z\d\s\-\,\#\.\+]+]" name="address" id="address" value="" /></td>
                                  <td width="15%" id="errOffset">&nbsp;</td>
                                </tr>
                                <tr>
                                  <td width="15%"><label for="telephone">Telephone</label></td>
                                  <td width="70%"><input type="text" class="validate[required,custom[onlyNumber]]" name="telephone" id="telephone" value="" /></td>
                                  <td width="15%" id="errOffset">&nbsp;</td>
                                </tr>
                                <tr>
                                  <td width="15%"><label for="email">Email</label></td>
                                  <td width="70%"><input type="text" class="validate[required,custom[email]]" name="email" id="email" value="" /></td>
                                  <td width="15%" id="errOffset">&nbsp;</td>
                                </tr>
                                <tr>
                                  <td width="15%"><label for="US Citizen">US Citizen</label></td>
                                  <td width="70%"><input type="checkbox" name="citizen" value="yes">Yes</td>
                                  <td width="15%" id="errOffset">&nbsp;</td>
                                </tr>
                                <tr>
                                  <td width="15%"><label for="Age Limit">At Least 18</label></td>
                                  <td width="70%"><input type="checkbox" name="age" value="yes">Yes</td>
                                  <td width="15%" id="errOffset">&nbsp;</td>
                                </tr>
                                <tr>
                                  <td width="15%"><label for="Age Limit">Have you worked<br> with us before?</label></td>
                                  <td width="70%"><input type="checkbox" name="previousexp" value="yes">Yes</td>
                                  <td width="15%" id="errOffset">&nbsp;</td>
                                </tr>
                                <tr>
                                  <td width="15%"><label for="subject">Experience</label></td>
                                  <td width="70%"><select name="subject" id="subject">
                                    <option value="" selected="selected"> - Choose -</option>
                                    <option value="0-5">0-5 Years</option>
                                    <option value="6-10">6-10 Years</option>
                                    <option value="11-15">11-15 Years</option>
                                    <option value="16-20">16-20 Years</option>
                                    </select></td>
                                  <td width="15%" id="errOffset">&nbsp;</td>
                                </tr>
                                <tr>
                              <td><label for="captcha"><?=$_SESSION['n1']?> + <?=$_SESSION['n2']?> =</label></td>
                              <td><input type="text" class="validate[required,custom[onlyNumber]]" name="captcha" id="captcha" /></td>
                              <td valign="top">&nbsp;</td>
                            </tr>
                            <tr>
                              <td valign="top">&nbsp;</td>
                              <td colspan="2"><input type="submit" name="button" id="button" value="Submit" />
                              <input type="reset" name="button2" id="button2" value="Reset" />

                              <?=$str?>          <img id="loading" src="img/ajax-load.gif" width="16" height="16" alt="loading" /></td>
                            </tr>
                          </table>
                          </form>
                          <?=$success?>
        </div>
    </div>

SUBMIT.PHP:

<?php

/* config start */

$emailAddress = 'coreymaret@gmail.com';

/* config end */


require "phpmailer/class.phpmailer.php";

session_name("fancyform");
session_start();



foreach($_POST as $k=>$v)
{
    if(ini_get('magic_quotes_gpc'))
    $_POST[$k]=stripslashes($_POST[$k]);

    $_POST[$k]=htmlspecialchars(strip_tags($_POST[$k]));
}


$err = array();

if(!checkLen('name'))
    $err[]='The name field is too short or empty!';

if(!checkLen('email'))
    $err[]='The email field is too short or empty!';
else if(!checkEmail($_POST['email']))
    $err[]='Your email is not valid!';

if(!checkLen('subject'))
    $err[]='You have not selected a subject!';

if(!checkLen('message'))
    $err[]='The message field is too short or empty!';

if((int)$_POST['captcha'] != $_SESSION['expect'])
    $err[]='The captcha code is wrong!';


if(count($err))
{
    if($_POST['ajax'])
    {
        echo '-1';
    }

    else if($_SERVER['HTTP_REFERER'])
    {
        $_SESSION['errStr'] = implode('<br />',$err);
        $_SESSION['post']=$_POST;

        header('Location: '.$_SERVER['HTTP_REFERER']);
    }

    exit;
}


$msg=
'Name:  '.$_POST['name'].'<br />
Email:  '.$_POST['email'].'<br />
IP: '.$_SERVER['REMOTE_ADDR'].'<br /><br />

Message:<br /><br />

'.nl2br($_POST['message']).'

';


$mail = new PHPMailer();
$mail->IsMail();

$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->AddAddress($emailAddress);
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = "A new ".mb_strtolower($_POST['subject'])." from ".$_POST['name']." | contact form feedback";

$mail->MsgHTML($msg);

$mail->Send();


unset($_SESSION['post']);

if($_POST['ajax'])
{
    echo '1';
}
else
{
    $_SESSION['sent']=1;

    if($_SERVER['HTTP_REFERER'])
        header('Location: '.$_SERVER['HTTP_REFERER']);

    exit;
}

function checkLen($str,$len=2)
{
    return isset($_POST[$str]) && mb_strlen(strip_tags($_POST[$str]),"utf-8") > $len;
}

function checkEmail($str)
{
    return preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $str);
}

?>

1 个答案:

答案 0 :(得分:2)

您看到错误消息的原因是您已经声明:他们没有名为message的表单字段(因此$_POST['message']不存在)。 checkLen()首先检查要验证的变量是否存在,如果不存在则返回false。然后检查checkLen()是否返回false,如果是,则会收到错误消息,因此您的代码工作正常。基本上,为了解决这个问题,您只需要删除该错误检查。