PHP表单提交获取所有选项值?

时间:2022-09-26 10:48:18

Here i just want to discus about the following:

在这里,我只想讨论以下内容:

My HTML From like the fllowing code:

我的HTML来自fllowing代码:

<html>
<head>
<title>My Form</title>
</head>
<body>
<form id="sample" method="post" action="saveData.php">
Courses:
<input type="checkbox" name="check[]" Value="C++" />C++
<input type="checkbox" name="check[]" Value="PHP"/>PHP
<input type="checkbox" name="check[]" Value="MYSQL" />MYSQL
<input type="checkbox" name="check[]" Value=".Net"/>.Net
Gender:
<input type="radio" name="gen[]" Value="male"/>male
<input type="radio" name="gen[]" Value="female"/>Female
</form>
</body>
</html>

And I Want the OutPut Like The Following:

我希望OutPut喜欢以下内容:

  foreach ($_POST as $key => $val) {
        $actVal .= "'".strtolower($key)."|".strtolower($val)."',";
        $sqlin .= " ".strtolower($key)." VARCHAR(255) , ";
                }

But I got The Output like which one clicked in that options:

但我得到的输出就像点击了那个选项:

Like the following:

如下:

-----------------------------------------
male
C++

but I need it like the following:

但是我需要它如下:

male,female
C++,PHP,MYSQL,.Net

3 个解决方案

#1


1  

POST will send all values if you mark them as selected using javascript right before submitting. $_POST["check"] is an array. use a foreach and get all values from that array.

如果您在提交前使用javascript将其标记为已选中,POST将发送所有值。 $ _POST [“check”]是一个数组。使用foreach并从该数组中获取所有值。

#2


1  

As you're loop through post data which is going to be an array I believe that is why only one of their elements are being returned.

当你循环遍历将成为数组的post数据时,我相信这就是为什么只返回其中一个元素的原因。

You might want to try something like this:

您可能想尝试这样的事情:

foreach ($_POST as $key => $val) {
    if ($key == "check" || $key == "gen") { // If this is an array post field
        foreach ($val as $val2) { // We need to loop through again since they're array post fields
            $actVal .= "'" . strtolower($val2) . "'";
        }   
    } else {
        $actVal .= "'".strtolower($key)."|".strtolower($val)."',";
    }
    //$sqlin .= " ".strtolower($key)." VARCHAR(255) , "; // Worry about this separately, should be the same process
}

#3


1  

I can't figure out a way around that one. But there is an alternative:

我无法找到解决这个问题的方法。但还有另一种选择:

<input type="checkbox" name="check" value="php" />PHP
<input type="hidden" name="checklist" value="php" />

<input type="checkbox" name="check" value="MySQL" />MySQL
<input type="hidden" name="checklist" value="MySQL" />

The idea is to store the list of all the values of a checkbox/radio button, in a hidden input so that you get the list of those values server-side, when the form is submitted.

我们的想法是在隐藏的输入中存储复选框/单选按钮的所有值的列表,以便在提交表单时获取服务器端的值列表。

BTW, why do you need it anyway?

顺便说一句,你为什么还需要呢?

#1


1  

POST will send all values if you mark them as selected using javascript right before submitting. $_POST["check"] is an array. use a foreach and get all values from that array.

如果您在提交前使用javascript将其标记为已选中,POST将发送所有值。 $ _POST [“check”]是一个数组。使用foreach并从该数组中获取所有值。

#2


1  

As you're loop through post data which is going to be an array I believe that is why only one of their elements are being returned.

当你循环遍历将成为数组的post数据时,我相信这就是为什么只返回其中一个元素的原因。

You might want to try something like this:

您可能想尝试这样的事情:

foreach ($_POST as $key => $val) {
    if ($key == "check" || $key == "gen") { // If this is an array post field
        foreach ($val as $val2) { // We need to loop through again since they're array post fields
            $actVal .= "'" . strtolower($val2) . "'";
        }   
    } else {
        $actVal .= "'".strtolower($key)."|".strtolower($val)."',";
    }
    //$sqlin .= " ".strtolower($key)." VARCHAR(255) , "; // Worry about this separately, should be the same process
}

#3


1  

I can't figure out a way around that one. But there is an alternative:

我无法找到解决这个问题的方法。但还有另一种选择:

<input type="checkbox" name="check" value="php" />PHP
<input type="hidden" name="checklist" value="php" />

<input type="checkbox" name="check" value="MySQL" />MySQL
<input type="hidden" name="checklist" value="MySQL" />

The idea is to store the list of all the values of a checkbox/radio button, in a hidden input so that you get the list of those values server-side, when the form is submitted.

我们的想法是在隐藏的输入中存储复选框/单选按钮的所有值的列表,以便在提交表单时获取服务器端的值列表。

BTW, why do you need it anyway?

顺便说一句,你为什么还需要呢?