比较复选框的值提交和使用php显示消息

时间:2018-06-25 05:41:26

标签: php

我想在用户单击“其他”复选框时显示该消息。也有其他复选框,因此我想比较复选框的值,如果选择了其他复选框,那么我想显示消息。这是我的代码:

function findGreaterNumberCount($array, $number) {
    $count = 0;
    foreach ($array as $value) {
        if ($value > $number) {
            $count++;
        }
    }
    return $count;
}

echo findGreaterNumberCount(array(2, 12, 23, 34, 36, 36, 36, 56), 30);

php部分:

<table id="associatedEmailsTable">...

此代码不起作用。我应该进行哪些更改?

3 个答案:

答案 0 :(得分:1)

subject[]是一个数组,

<input type="checkbox" name="subject[]" value="Others">

是数组类型。因此,您应按以下方式将subject字段验证为数组:

if (isset($_POST['submit'])) {
    if (!empty($_POST['subject']) && in_array("Others", $_POST['subject'])) {
        echo " <SCRIPT LANGUAGE='Javascript'>
                window.alert('You wish to select other combo apart from given in above list. Kindly contact incharge of this institute.');
                </SCRIPT>";
    }
}

答案 1 :(得分:0)

您可以尝试以下类似的方法。您可以自由使用in_array(),但是如果您想使用第二个条件,请像这样使用foreach。

if (in_array("Others", $_POST['subject'])) {
            echo 'selected ';
        }
        else{
            //do something
        }

OR

if(isset($_POST['submit']))
{
    $results=$_POST['subject'];
    foreach ($results as $result) {
        if ($result==='Others') {
            echo 'selected ';
        }
        else{
            //do something
        }
    }
}

答案 2 :(得分:0)

<?php 
if(isset($_POST['submit']) && $_POST['submit']=="Submit"){
    if(isset($_POST['subject'])){
        $subject = $_POST['subject'];
        if(in_array('Others', $subject)){
            echo "<script>alert('You wish to select other combo apart from given in above list. Kindly contact incharge of this institute.');</script>";
        }
    }else{
        echo "<script>alert('You wish to select other combo apart from given in above list. Kindly contact incharge of this institute.');</script>";
    }
}
?>
<form method="POST" action="">
<input type="checkbox" name="subject[]" value="Subject-1+Subject-2">Subject-1+Subject-2
<input type="checkbox" name="subject[]" value="Subject-1+Subject-2+Subject-3">Subject-1+Subject-2+Subject-3
<input type="checkbox" name="subject[]" value="Others">Others
<input type="submit" value="Submit"  name="submit" class="wpcf7-submit"><br><br>
</form>