-2

我有一个简单的复选框示例,它记住在提交表单后选择的内容并且出现错误。

这部分效果很好......但是,我想将生成的“选中”框发布到我的表单邮件功能。

我现在只报告选中或未选中的内容,我希望在没有未选中框甚至注册的情况下拥有选中框的“值”。

<?php

$CB_1 = 'unchecked';
$CB_2 = 'unchecked';
$CB_3 = 'unchecked';
$CB_4 = 'unchecked';
$CB_5 = 'unchecked';

if (isset($_POST['submit'])) {

if (isset($_POST['CB_1'])) {$CB_1 = $_POST['CB_1'];
if ($CB_1 == 'item_01') {$CB_1 = 'checked';}
}

if (isset($_POST['CB_2'])) {$CB_2 = $_POST['CB_2'];
if ($CB_2 == 'item_02') {$CB_2 = 'checked';}
}

if (isset($_POST['CB_3'])) {$CB_3 = $_POST['CB_3'];
if ($CB_3 == 'item_03') {$CB_3 = 'checked';}
}

if (isset($_POST['CB_4'])) {$CB_4 = $_POST['CB_4'];
if ($CB_4 == 'item_04') {$CB_4 = 'checked';}
}

if (isset($_POST['CB_5'])) {$CB_5 = $_POST['CB_5'];
if ($CB_5 == 'item_05') {$CB_5 = 'checked';}
}
}

if (isset($_POST['submit'])) {
$error  = "";

    if (!empty($_POST['email'])) {
    $email = $_POST['email'];

    if (!preg_match("/^[a-z0-9]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){ 
    $error .= "E-mail address not valid.";
    }

    } else {

    $error .= "E-mail address is required.";
    }

    if (empty($error)) {

    $from = 'From: '. @TEST .' <'. $email .'>';
    $to = "someone@company.com";
    $subject = "CHECKBOX TEST";
    $content = "

    checkbox selections: 
                        check box 01: $CB_1
                        check box 02: $CB_2
                        check box 03: $CB_3
                        check box 04: $CB_4
                        check box 05: $CB_5

    ";

    $success = mail($to,$subject,$content,$from);
    }
    }

?>



<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">

<?php if (!empty($error)) echo $error ?>

<br><br><br>

e-mail: <input type="text" name="email" value="<?php if (isset ($_POST {'email'})) { echo $_POST['email']; } ?>" />

<P>

<input type="checkbox" name="CB_1" value="item_01" <?PHP echo $CB_1; ?> /> Item 01 

<input type="checkbox" name="CB_2" value="item_02" <?PHP echo $CB_2; ?> /> Item 02 

<input type="checkbox" name="CB_3" value="item_03" <?PHP echo $CB_3; ?> /> Item 03 

<input type="checkbox" name="CB_4" value="item_04" <?PHP echo $CB_4; ?> /> Item 04 

<input type="checkbox" name="CB_5" value="item_05" <?PHP echo $CB_5; ?> /> Item 05 

<P>

<input type="submit" name="submit" value="Submit"></input>

</form>
4

3 回答 3

0

我找到了我想要的东西,我认为这是一个数组,我认为“redelman431”试图让我去做,但当时对我来说没有任何意义。

然后,我偶然发现了“David Bélanger”的另一个数组,由于某种原因,它对我来说更有意义,所以我将它用于我的复选框,并且效果非常好。

新问题是我无法获得已检查的复选框,以记住如果页面上的其他地方有错误,这是一场灾难,因为我的最终表单有很多。

有任何想法吗?

<?php

    #   Default Vars
    $_group_01 = '';
    if(isset($group_01) === TRUE){
    #   Is Array ?
    if(is_array($group_01) === TRUE){
    #   Count
    $c = count($group_01);
    #   Loop
    for($i=0; $i < $c; $i++){
    $_group_01.= (isset($group_01[$i]) === TRUE ? $group_01[$i] : '').($i == ($c-1) ? '' : ($i == $c-2 ? ' and ' : ', '));
    }
    }
    }

if (isset($_POST['submit'])) {
$error  = "";

    if (!empty($_POST['email'])) {
    $email = $_POST['email'];

    if (!preg_match("/^[a-z0-9]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){ 
    $error .= "E-mail address not valid.";
    }

    } else {

    $error .= "E-mail address is required.";
    }

    if (empty($error)) {

    $from = 'From: '. TEST .' <'. $email .'>';
    $to = "someone@company.com";
    $subject = "CHECKBOX TEST";
    $content = "

checkbox selections: 

Items Checked: $_group_01

    ";

    $success = mail($to,$subject,$content,$from);
    }
    }

?>



<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">

<span style="color: red;"><?php if (!empty($error)) echo $error ?></span>

<br><br><br>

e-mail: <input type="text" name="email" value="<?php if (isset ($_POST {'email'})) { echo $_POST['email']; } ?>" />



<P>

<input type="checkbox" name="group_01[]" value="Item 01" 
<?php if(isset($_POST['group_01'])) { echo 'checked'; } ?> />Item 01

<input type="checkbox" name="group_01[]" value="Item 02" 
<?php if(isset($_POST['group_01'])) { echo 'checked'; } ?> />Item 02

<input type="checkbox" name="group_01[]" value="Item 03" 
<?php if(isset($_POST['group_01'])) { echo 'checked'; } ?> />Item 03

<input type="checkbox" name="group_01[]" value="Item 04" 
<?php if(isset($_POST['group_01'])) { echo 'checked'; } ?> />Item 04

<input type="checkbox" name="group_01[]" value="Item 05" 
<?php if(isset($_POST['group_01'])) { echo 'checked'; } ?> />Item 05

<P>



<input type="submit" name="submit" value="Submit"></input>

</form>
于 2012-12-13T13:47:36.473 回答
0

okay, I found a way to do what I wanted but I'm fairly sure it's not the way someone who has real experience with PHP would do it. Any suggestions?

redelman431, thanks for your suggestion but it was really too advanced for my skill level.

<?php

        if(isset($_POST['item_01'])) {$item_01 = 'Item 01';}
        if(isset($_POST['item_02'])) {$item_02 = 'Item 02';}
        if(isset($_POST['item_03'])) {$item_03 = 'Item 03';}
        if(isset($_POST['item_04'])) {$item_04 = 'Item 04';}
        if(isset($_POST['item_05'])) {$item_05 = 'Item 05';}

if (isset($_POST['submit'])) {
$error  = "";

    if (!empty($_POST['email'])) {
    $email = $_POST['email'];

    if (!preg_match("/^[a-z0-9]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){ 
    $error .= "E-mail address not valid.";
    }

    } else {

    $error .= "E-mail address is required.";
    }

    if (empty($error)) {

    $from = 'From: '. @TEST .' <'. $email .'>';
    $to = "someone@company.com";
    $subject = "CHECKBOX TEST";
    $content = "

    checkbox selections: 

                        checked box's: $item_01 $item_02 $item_03 $item_04 $item_05





    ";

    $success = mail($to,$subject,$content,$from);
    }
    }

?>



<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">

<span style="color: red;"><?php if (!empty($error)) echo $error ?></span>

<br><br><br>

e-mail: <input type="text" name="email" value="<?php if (isset ($_POST {'email'})) { echo $_POST['email']; } ?>" />

<P>



<input type="checkbox" name="item_01" value="Item 01" 
<?php if(isset($_POST['item_01'])) { echo 'checked'; } ?> /> Item 01

<input type="checkbox" name="item_02" value="Item 02" 
<?php if(isset($_POST['item_02'])) { echo 'checked'; } ?> /> Item 02

<input type="checkbox" name="item_03" value="Item 03" 
<?php if(isset($_POST['item_03'])) { echo 'checked'; } ?> /> Item 03

<input type="checkbox" name="item_04" value="Item 04" 
<?php if(isset($_POST['item_04'])) { echo 'checked'; } ?> /> Item 04

<input type="checkbox" name="item_05" value="Item 05" 
<?php if(isset($_POST['item_05'])) { echo 'checked'; } ?> /> Item 05



<P>

<input type="submit" name="submit" value="Submit"></input>

</form>
于 2012-12-13T09:06:47.377 回答
0

我知道这可能不一定能回答这个问题,但可以让你的代码更高效的一种方法就是这样做。

for($i=1; $i<=5;$i++)
{

     if(isset($_POST['submit'])&& isset($_POST['CB_'.$i]) && $CB_.$i=='item_0'.$i)
     {
        $CB_.$i = $_POST['CB_'.$i];
        $CB_.$i = 'checked';
     }
}

如果您以这种方式修复它,它也可能使我们更容易调试。

于 2012-12-12T17:06:14.010 回答