PHP验证选择框数组的麻烦

时间:2015-01-05 01:59:15

标签: php arrays validation drop-down-menu form-submit

我正在为当前表单添加一些新的验证,我有三个单独的数组,我已经为选择框构建了,我需要确保用户至少进行了一个选择。此时,当用户填写表单时,它将提交并发送,但“必填”显示在表单中的空选择框旁边,即使通知说它已被发送,即使用户也会发送表单没有做出选择。我试着到处寻找解决方案,希望在这个问题上得到第二眼。提前感谢您的时间!

表单中的选择框 -

        <?php echo $monthMessage; ?>

<select name="mm" class="select-form-style">
<option> 
- Select Month -
</option>
<?php
foreach ($months as $month) {
$selected = (!empty($mm) && $mm == $month) ? 'selected="selected"' : '';
?>
<option value="<?php echo $month;?>" <?php echo $selected;?>>
<?php echo $month;?>
</option>
<?php
}
?>

PHP表单验证

<?php #Registration 

$months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 
'September', 'October', 'November', 'December');
$season = array('Fall 2014', 'Winter 2014', 'Spring 2015', 'Summer 2015');

$locations = array('Priest River', 'Sandpoint', 'Online');

function makeSafe($value) { 
    return stripslashes(trim($value)); 
}

//if the form was subitted
if (!empty($_POST['submit'])) {
//form submitted through post, do validation

$authorization = (!empty($_POST['authorization'])) ? makeSafe($_POST['authorization']) : '';
$signature = (!empty($_POST['signature'])) ? makeSafe($_POST['signature']) : '';    
$cc = makeSafe($_POST['cc']);
$ll = makeSafe($_POST['ll']);
$fname = makeSafe($_POST['fname']);
$mname = makeSafe($_POST['mname']);
$lname = makeSafe($_POST['lname']);
$mm = makeSafe($_POST['mm']);
$day = makeSafe($_POST['day']);
$year = makeSafe($_POST['year']);
$age = makeSafe($_POST['age']);
$school = makeSafe($_POST['school']);
$address = makeSafe($_POST['address']);
$city = makeSafe($_POST['city']);
$state = makeSafe($_POST['state']);
$zip = makeSafe($_POST['zip']);
$gender = makeSafe($_POST['gender']);
$email = makeSafe($_POST['email']);
$phone = makeSafe($_POST['phone']);
$altph = makeSafe($_POST['altph']);
$hp = makeSafe($_POST['hp']);
$packet = (!empty($_POST['signature'])) ? makeSafe($_POST['packet']) : '';

$message = '';
$problem = FALSE;

if(!empty($_POST['mm'])) 
{
    $monthMessage .= '<p class="errorClass">Required</p>';
}

    if(!empty($_POST['cc'])) 
{
    $seasonMessage .= '<p class="errorClass">Required</p>';
}

    if(!empty($_POST['ll'])) 
{
    $locationMessage .= '<p class="errorClass">Required</p>';
}

   if($_POST['authorizaton']!="1" AND $_POST['packet']!="1"){
    $problem = TRUE;
    $parentMessage .= '<p class="errorClass parentMsg">Please read and check the boxes below
</p>';
}

 if($_POST['signature']!="1"){
    $problem = TRUE;
    $signatureMessage .= '<p class="errorClass">Required</p>';
}

//Check First Name
if (!eregi ('^[[:alpha:]\.\' \-]{2,}$',$fname)) {
    $problem = TRUE;
    $fnameMessage .= '<p class="errorClass">Required</p>';
}

if (!eregi ('^[[:alpha:]\.\' \-]{2,}$',$lname)) {
    $problem = TRUE;
    $lnameMessage .= '<p class="errorClass">Required</p>';
}

if (!eregi ('^[[:alnum:]\.\' \-]{4,}$', $school)) {
    $problem = TRUE;
    $schoolMessage .= '<p class="errorClass">Required</p>';
}

if (!eregi ('^[[:alnum:]\.\' \#\-]{4,}$', $address)) {
    $problem = TRUE;
    $addyMessage .= '<p class="errorClass">Required</p>';
}

if (!eregi ('^[[:alpha:]\.\' \-]{4,}$', $city)) {
    $problem = TRUE;
    $cityMessage .= '<p class="errorClass">Required</p>';
}

if (!eregi ('^[[:alpha:]\.\' \-]{2,2}$', $state)) {
    $problem = TRUE;
    $stateMessage .= '<p class="errorClass">Required</p>';
}

if (!eregi ('^[[:alnum:]][a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$',$email)) {
    $problem = TRUE;
    $emailMessage .= '<p class="errorClass">Required</p>';
}

if (!eregi ('^[0-9]{5}(\-[0-9]{4})?$',$zip)) {
    $problem = TRUE;
    $zipMessage .= '<p class="errorClass">Required</p>';
}

if (!eregi ('^[0-9.\(\)\-\ ]{10,14}$', $phone)) {
    $problem = TRUE;
    $phoneMessage .= '<p class="errorClass">Required</p>';
}

if(!eregi('^[0-9]{2}$',$age)) {
    $problem = TRUE;
    $ageMessage .= '<p class="errorClass">Required</p>';
}

if(!eregi('^[0-9]{2,4}$',$year)) {
    $problem = TRUE;
    $yearMessage .= '<p class="errorClass">Required</p>';
}

if(!eregi('^[0-9]{1,2}$',$day)) {
    $problem = TRUE;
    $dayMessage .= '<p class="errorClass">Required</p>';
}

if ($problem == TRUE) {//Something went wrong
    echo $message;
    echo '';        
}
else{   
    $to = "webangel119@yahoo.com";
    $subject = "Buckle-Up New Student Registration";
    $headers = 'From: jen.byron83@gmail.com' . "\r\n" .'X-Mailer: PHP/' . phpversion();

    $msg .= "PLAIN TEXT EMAIL\n\n";
    $msg .= "Location: $ll      Class: $cc\n\n";
    $msg .= "Name: $fname $mname $lname\n\n";
    $msg .= "Birthday: $mm $day $year     Age: $age     Gender: $gender\n\n";
    $msg .= "School: $school\n\n";
    $msg .= "Address: $address\n\n";
    $msg .= "City: $city     State: $state     Zip Code: $zip\n\n";
    $msg .= "Phone: $phone     Alternate Contact: $altph\n\n";
    $msg .= "Email: $email\n\n";


    if(mail($to, $subject, $msg, $headers) == false){
        // Email failed
        echo '<p>An error occured while trying to process your request</p>';
    }
    else {
        echo '<p>Thank you for registering with Buckle-Up Driving School!</p>';
        $authorization = '';
        $ll = '';
        $cc = '';   
        $fname = '';
        $mname = '';
        $lname = '';
        $mm = '';
        $day = '';
        $year = '';
        $age = '';
        $school = '';
        $address = '';
        $city = '';
        $state = '';
        $zip = '';
        $gender = '';
        $email = '';
        $phone = '';
        $altph = '';
        $hp = '';
        $signature = '';
        $packet = '';
    }
}
}
?>

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解了这个问题,但如果我对此有所了解,我的答案将是:

你的错误在那里:

if(!empty($_POST['mm'])) 
{
    $monthMessage .= '<p class="errorClass">Required</p>';
}

因此,如果$_POST['mm'] 不是为空,则会显示“必需”。但是你有问题,这个值永远不会是空的:)

即使没有选择月份(“ - 选择月 - ”选中),导航器也会将选项标签的内容作为select的值发送;)

所以你有$_POST['mm'] = "- Select Month -"

你必须做这个测试:

// if mm sent and is not empty
if(isset($_POST['mm']) && !empty($_POST['mm']))
{
    // if mm is in $months array
    if(in_array($_POST['mm'], $months))
    {
        // this value is not accepted or "- Select Month -" selected
    }
    else
    {
        // seems a month is selected and into your $months list
    }
}
else
{
    // this case happens if don't send "mm" in post
    // or if the value of mm is empty
}

顺便说一下,用stripslashes(trim($val));照顾这个值永远不会确定。

Btw2,您可以通过以下方式将makeSafe()应用于整个$_POST数组:

foreach($_POST as $k => $v)
{
    $P[$k] = makeSafe($v);
}

然后调用您的数据,如$P['mm']

希望它有所帮助!