PHP 表单处理顺序优先级

发布于 2024-10-31 05:58:43 字数 3482 浏览 5 评论 0原文

我是开发领域的新手,刚刚开始学习 PHP。我有基本表单,尝试验证用户已选择或检查的复选框。我的代码如下。我的问题是,为什么当我的表单顺序如下时,表单不传递值 NET、PHP 或 RUBY,而持续传递的值是 no。

--- 表单代码不起作用 ---

<form name="checkboxes" method="post" action="form_sample_checkboxes.php">
        <input type="checkbox" name="ch1" value="net" <?php print $ch1status ?>>.NET
            <input type="hidden" name="ch1" value="no">


        <input type="checkbox" name="ch2" value="php" <?php print $ch2status ?>>PHP
        <input type="hidden" name="ch2" value="no">


        <input type="checkbox" name="ch3" value="ruby" <?php print $ch3status ?>>Ruby on Rails
        <input type="hidden" name="ch3" value="no">

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

但是,如果我的代码如下;

<form name="checkboxes" method="post" action="form_sample_checkboxes.php">
        <input type="hidden" name="ch1" value="no">
        <input type="checkbox" name="ch1" value="net" <?php print $ch1status ?>>.NET

        <input type="hidden" name="ch2" value="no">
        <input type="checkbox" name="ch2" value="php" <?php print $ch2status ?>>PHP

        <input type="hidden" name="ch3" value="no">
        <input type="checkbox" name="ch3" value="ruby" <?php print $ch3status ?>>Ruby on Rails

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

这些框显示为已选中。完整代码如下。

<?php

$ch1status = "unchecked";
$ch2status = "unchecked";
$ch3status = "unchecked";

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

        if(isset($_POST["ch1"])) {
            if($_POST["ch1"] == "net") {
                $ch1status = "checked";
            }
        }

        if(isset($_POST["ch2"])) {
            if($_POST["ch2"] == "php") {
                $ch2status = "checked";
            }
        }

        if(isset($_POST["ch3"])) {
            if($_POST["ch3"] == "ruby") {
                $ch3status = "checked";
            }
        }

        if ($_POST["ch1"] == "no" && $_POST["ch2"] == "no" && $_POST["ch3"] == "no") {
            print "There is no such choice";
        }

}

?>
<html>
<head>
    <title>Sample form checkbxoes</title>
</head>
<body>
    <form name="checkboxes" method="post" action="form_sample_checkboxes.php">
        <input type="hidden" name="ch1" value="no">
        <input type="checkbox" name="ch1" value="net" <?php print $ch1status ?>>.NET

        <input type="hidden" name="ch2" value="no">
        <input type="checkbox" name="ch2" value="php" <?php print $ch2status ?>>PHP

        <input type="hidden" name="ch3" value="no">
        <input type="checkbox" name="ch3" value="ruby" <?php print $ch3status ?>>Ruby on Rails

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

<?php

if(isset($_POST["submit"])) {
    if(isset($_POST["ch1"])) {
        print $_POST["ch1"];
        print $ch1status;
    }

    if(isset($_POST["ch2"])) {
        print $_POST["ch2"];
        print $ch2status;
    }

    if(isset($_POST["ch3"])) {
        print $_POST["ch3"];
        print $ch3status;
    }

}

echo "<pre>";
print_r($_POST);
echo "</pre>";

?>

</body>
</html>
        </form>

除了使用隐藏表单字段之外,还有其他方法可以验证用户是否未选择任何复选框。

Am new to the world of development and am just starting to pick up PHP. I have basic form that attempts to validate the checkboxes the user has selected or checked. My code is below. The question I have is why is that when I have the order of my form as follows, the form does not pass the value NET, PHP or RUBY and the values that are costantly passed are no.

--- Form code that does not work ---

<form name="checkboxes" method="post" action="form_sample_checkboxes.php">
        <input type="checkbox" name="ch1" value="net" <?php print $ch1status ?>>.NET
            <input type="hidden" name="ch1" value="no">


        <input type="checkbox" name="ch2" value="php" <?php print $ch2status ?>>PHP
        <input type="hidden" name="ch2" value="no">


        <input type="checkbox" name="ch3" value="ruby" <?php print $ch3status ?>>Ruby on Rails
        <input type="hidden" name="ch3" value="no">

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

However if my code is as follows;

<form name="checkboxes" method="post" action="form_sample_checkboxes.php">
        <input type="hidden" name="ch1" value="no">
        <input type="checkbox" name="ch1" value="net" <?php print $ch1status ?>>.NET

        <input type="hidden" name="ch2" value="no">
        <input type="checkbox" name="ch2" value="php" <?php print $ch2status ?>>PHP

        <input type="hidden" name="ch3" value="no">
        <input type="checkbox" name="ch3" value="ruby" <?php print $ch3status ?>>Ruby on Rails

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

The boxes appear checked. The entire code below.

<?php

$ch1status = "unchecked";
$ch2status = "unchecked";
$ch3status = "unchecked";

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

        if(isset($_POST["ch1"])) {
            if($_POST["ch1"] == "net") {
                $ch1status = "checked";
            }
        }

        if(isset($_POST["ch2"])) {
            if($_POST["ch2"] == "php") {
                $ch2status = "checked";
            }
        }

        if(isset($_POST["ch3"])) {
            if($_POST["ch3"] == "ruby") {
                $ch3status = "checked";
            }
        }

        if ($_POST["ch1"] == "no" && $_POST["ch2"] == "no" && $_POST["ch3"] == "no") {
            print "There is no such choice";
        }

}

?>
<html>
<head>
    <title>Sample form checkbxoes</title>
</head>
<body>
    <form name="checkboxes" method="post" action="form_sample_checkboxes.php">
        <input type="hidden" name="ch1" value="no">
        <input type="checkbox" name="ch1" value="net" <?php print $ch1status ?>>.NET

        <input type="hidden" name="ch2" value="no">
        <input type="checkbox" name="ch2" value="php" <?php print $ch2status ?>>PHP

        <input type="hidden" name="ch3" value="no">
        <input type="checkbox" name="ch3" value="ruby" <?php print $ch3status ?>>Ruby on Rails

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

<?php

if(isset($_POST["submit"])) {
    if(isset($_POST["ch1"])) {
        print $_POST["ch1"];
        print $ch1status;
    }

    if(isset($_POST["ch2"])) {
        print $_POST["ch2"];
        print $ch2status;
    }

    if(isset($_POST["ch3"])) {
        print $_POST["ch3"];
        print $ch3status;
    }

}

echo "<pre>";
print_r($_POST);
echo "</pre>";

?>

</body>
</html>
        </form>

Also is there any other way of validating if the user has not selected any checkboxes as opposed to using hidden form fields.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清晰传感 2024-11-07 05:58:43

这只是一个浏览器问题,而且非常简单:元素具有相同的名称,后面的元素会覆盖第一个元素。

验证复选框是否未选中的另一种方法是检查它是否在 $POST 数组中设置。如果丢失,则视为“未检查”。

Its just a browser-issue and its quite simple: The elements have the same name and the later element overwrites the first one.

Another way of validating, if a checkbox is not checked is to check, if its set in the $POST-array. If its missing, its treated like "not checked".

梦里梦着梦中梦 2024-11-07 05:58:43

未定义索引:

这是因为只有选中复选框才会发送它们。您可以做的一件事是在使用变量之前始终使用 isset 检查变量(例如 isset($_POST['ch1']));另一种方法是将复选框命名为相同的名称,并在名称后面添加 [] (例如 name="languages[]"),然后执行如下操作:

// Create a list of languages that are OK (remember, some users are malicious)
$languages = array('net','php','ruby');
// Compile a list of the answers the user picked; force it to be an 
// array by either explicitly casting to an array, or using an empty array
//  if none chosen
$picked = isset($_POST['languages']) ? (array)$_POST['languages'] : array();

// first, use array_intersect to remove entries present in one and not the other
// i.e. invalid entries from the client or entries not picked from the whole list
// then, "flip" the array so that the values become keys, 
// because isset is faster than in_array
$valid_langs = array_flip(array_intersect($languages, $picked));

// check on languages
if (isset($valid_langs['php'])) { /* PHP picked */ }
if (isset($valid_langs['net'])) { /* NET picked */ }
if (isset($valid_langs['ruby'])) { /* Ruby picked */ }

更简单的解决方案:

<form>
    <input type="checkbox" name="php" value="yes" />
    <input type="checkbox" name="net" value="yes" />
    <input type="checkbox" name="ruby" value="yes" />
</form>

<?php
$php = $net = $ruby = 'unchecked';
if (!isset($_POST['php'],$_POST['net'],$_POST['ruby'])) {
    echo 'There is no such choice';
}
else {
    if (isset($_POST['php']) && $_POST['php'] == 'yes') {
        $php = 'checked';
    }
    if (isset($_POST['net']) && $_POST['new'] == 'yes') {
        $net = 'checked';
    }
    if (isset($_POST['ruby']) && $_POST['ruby'] == 'yes') {
        $ruby = 'checked';
    }
}
// ... snip ...

有很多方法可以做到这一点。希望您有兴趣学习其中的许多内容。

UNDEFINED INDEXES:

This is because checkboxes are only sent if they are checked. One thing you can do is always check the variable with isset (e.g. isset($_POST['ch1'])) before using them; another is to name your checkboxes the same thing with a [] following the name (e.g. name="languages[]") and then do something like this:

// Create a list of languages that are OK (remember, some users are malicious)
$languages = array('net','php','ruby');
// Compile a list of the answers the user picked; force it to be an 
// array by either explicitly casting to an array, or using an empty array
//  if none chosen
$picked = isset($_POST['languages']) ? (array)$_POST['languages'] : array();

// first, use array_intersect to remove entries present in one and not the other
// i.e. invalid entries from the client or entries not picked from the whole list
// then, "flip" the array so that the values become keys, 
// because isset is faster than in_array
$valid_langs = array_flip(array_intersect($languages, $picked));

// check on languages
if (isset($valid_langs['php'])) { /* PHP picked */ }
if (isset($valid_langs['net'])) { /* NET picked */ }
if (isset($valid_langs['ruby'])) { /* Ruby picked */ }

Simpler Solution:

<form>
    <input type="checkbox" name="php" value="yes" />
    <input type="checkbox" name="net" value="yes" />
    <input type="checkbox" name="ruby" value="yes" />
</form>

<?php
$php = $net = $ruby = 'unchecked';
if (!isset($_POST['php'],$_POST['net'],$_POST['ruby'])) {
    echo 'There is no such choice';
}
else {
    if (isset($_POST['php']) && $_POST['php'] == 'yes') {
        $php = 'checked';
    }
    if (isset($_POST['net']) && $_POST['new'] == 'yes') {
        $net = 'checked';
    }
    if (isset($_POST['ruby']) && $_POST['ruby'] == 'yes') {
        $ruby = 'checked';
    }
}
// ... snip ...

There are a great many ways to do this. Hopefully you will be interested in learning many of them.

几度春秋 2024-11-07 05:58:43

Php 都是服务器端的,因此为了阻止它们提交,您需要客户端验证。最简单的客户端验证是使用 javascript 或 jQuery 的验证插件(如果您已经在使用 jQuery)如果您计划在任何时候使用 AJAX,您就应该这样做)。

是的,您可以摆脱那些隐藏的输入。

Php is all server-side, so in order to keep them from submitting you'll need client-side validation. Easiest client-side validation is with javascript, or jQuery's Validation Plugin if you're already using jQuery (which you should be if you plan on using AJAX at any point).

And yes, you can get rid of those hidden inputs.

烟─花易冷 2024-11-07 05:58:43

您不需要那些隐藏字段。删除它们,它应该可以工作。

编辑:
看看这个修改

    $ch1status = "unchecked";
    $ch2status = "unchecked";
    $ch3status = "unchecked";

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

        if(@$_POST["ch1"] != "") {
            $ch1status = "checked";
        }

        if(@$_POST["ch2"] != "") {
            $ch2status = "checked";
        }

        if(@$_POST["ch3"] != "") {
            $ch3status = "checked";
        }

        if (@$_POST["ch1"] . @$_POST["ch2"] . @$_POST["ch3"] == "") {
            print "There is no such choice";
        }

    }

?>
<html>
    <head>
        <title>Sample form checkbxoes</title>
    </head>
    <body>
        <form name="checkboxes" method="post" action="form_sample_checkboxes.php">
            <input type="checkbox" name="ch1" value="net" <?php echo $ch1status; ?>>.NET

            <input type="checkbox" name="ch2" value="php" <?php echo $ch2status; ?>>PHP

            <input type="checkbox" name="ch3" value="ruby" <?php echo $ch3status; ?>>Ruby on Rails

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

        <?php

            if(isset($_POST["submit"])) {
                if(isset($_POST["ch1"])) {
                    print $_POST["ch1"];
                    print $ch1status;
                }

                if(isset($_POST["ch2"])) {
                    print $_POST["ch2"];
                    print $ch2status;
                }

                if(isset($_POST["ch3"])) {
                    print $_POST["ch3"];
                    print $ch3status;
                }

            }

            echo "<pre>";
            print_r($_POST);
            echo "</pre>";

        ?>

    </body>
</html>

You do not need those hidden fields. Remove them and it should work.

EDIT:
Check out this modification

    $ch1status = "unchecked";
    $ch2status = "unchecked";
    $ch3status = "unchecked";

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

        if(@$_POST["ch1"] != "") {
            $ch1status = "checked";
        }

        if(@$_POST["ch2"] != "") {
            $ch2status = "checked";
        }

        if(@$_POST["ch3"] != "") {
            $ch3status = "checked";
        }

        if (@$_POST["ch1"] . @$_POST["ch2"] . @$_POST["ch3"] == "") {
            print "There is no such choice";
        }

    }

?>
<html>
    <head>
        <title>Sample form checkbxoes</title>
    </head>
    <body>
        <form name="checkboxes" method="post" action="form_sample_checkboxes.php">
            <input type="checkbox" name="ch1" value="net" <?php echo $ch1status; ?>>.NET

            <input type="checkbox" name="ch2" value="php" <?php echo $ch2status; ?>>PHP

            <input type="checkbox" name="ch3" value="ruby" <?php echo $ch3status; ?>>Ruby on Rails

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

        <?php

            if(isset($_POST["submit"])) {
                if(isset($_POST["ch1"])) {
                    print $_POST["ch1"];
                    print $ch1status;
                }

                if(isset($_POST["ch2"])) {
                    print $_POST["ch2"];
                    print $ch2status;
                }

                if(isset($_POST["ch3"])) {
                    print $_POST["ch3"];
                    print $ch3status;
                }

            }

            echo "<pre>";
            print_r($_POST);
            echo "</pre>";

        ?>

    </body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文