php 中带有 switch 语句的复选框
checkbox in php with switch statement
为什么当我选中两个复选框时取消它 return 2?当两个复选框都打开时,我如何 return 不同的响应?
html:
<form action="form3.php" method="POST">
<fieldset>
<legend> select your team </legend>
<input type="checkbox" name="punto3" value="1"> 1 </input>
<input type="checkbox" name="punto3" value="2"> 2 </input>
<p><input type="submit"></p>
</fieldset>
</form>
php:
<?php
if (isset($_REQUEST['punto3']))
{
$punto3 = $_REQUEST['punto3'];
$conn = new mysqli("localhost", "root", "", "progetto2");
if ($conn == false)
{
die("fail: " . $conn->connect_error);
}
switch ($punto3)
{
case '1': // 1 checkbox
break;
case '2': // 2 checkbox
break;
default: // if 1 and 2 is both checked
}
} else {
echo "Please, do at least one selection";
}
?>
有什么建议吗?
为每个复选框使用单独的名称属性值,并使用 if 条件而不是开关(或者如果您只需要一个值,则使用单选按钮)示例:
HTML:
<form action="form3.php" method="POST">
<fieldset>
<legend> select your team </legend>
<input type="checkbox" name="punto3_1" value="1"> 1 </input>
<input type="checkbox" name="punto3_2" value="2"> 2 </input>
<p><input type="submit"></p>
</fieldset>
</form>
PHP:
<?php
if (
(isset($_REQUEST['punto3_1']) && $_REQUEST['punto3_1'] != "")
|| (isset($_REQUEST['punto3_2']) && $_REQUEST['punto3_2'] != "")
)
{
$punto3_1 = $_REQUEST['punto3_1'];
$punto3_2 = $_REQUEST['punto3_2'];
$conn = new mysqli("localhost", "root", "", "progetto2");
if ($conn == false)
{
die("fail: " . $conn->connect_error);
}
if(isset($_REQUEST['punto3_1']) && $_REQUEST['punto3_1'] != ""){
//your logic for punto3_1 will goes here....
} elseif(isset($_REQUEST['punto3_2']) && $_REQUEST['punto3_2'] != ""){
//your logic for punto3_2 will goes here....
} else {
//your logic for punto3_1 and punto3_2 will goes here....
}
} else {
echo "Please, do at least one selection";
}
?>
为什么当我选中两个复选框时取消它 return 2?当两个复选框都打开时,我如何 return 不同的响应?
html:
<form action="form3.php" method="POST">
<fieldset>
<legend> select your team </legend>
<input type="checkbox" name="punto3" value="1"> 1 </input>
<input type="checkbox" name="punto3" value="2"> 2 </input>
<p><input type="submit"></p>
</fieldset>
</form>
php:
<?php
if (isset($_REQUEST['punto3']))
{
$punto3 = $_REQUEST['punto3'];
$conn = new mysqli("localhost", "root", "", "progetto2");
if ($conn == false)
{
die("fail: " . $conn->connect_error);
}
switch ($punto3)
{
case '1': // 1 checkbox
break;
case '2': // 2 checkbox
break;
default: // if 1 and 2 is both checked
}
} else {
echo "Please, do at least one selection";
}
?>
有什么建议吗?
为每个复选框使用单独的名称属性值,并使用 if 条件而不是开关(或者如果您只需要一个值,则使用单选按钮)示例:
HTML:
<form action="form3.php" method="POST">
<fieldset>
<legend> select your team </legend>
<input type="checkbox" name="punto3_1" value="1"> 1 </input>
<input type="checkbox" name="punto3_2" value="2"> 2 </input>
<p><input type="submit"></p>
</fieldset>
</form>
PHP:
<?php
if (
(isset($_REQUEST['punto3_1']) && $_REQUEST['punto3_1'] != "")
|| (isset($_REQUEST['punto3_2']) && $_REQUEST['punto3_2'] != "")
)
{
$punto3_1 = $_REQUEST['punto3_1'];
$punto3_2 = $_REQUEST['punto3_2'];
$conn = new mysqli("localhost", "root", "", "progetto2");
if ($conn == false)
{
die("fail: " . $conn->connect_error);
}
if(isset($_REQUEST['punto3_1']) && $_REQUEST['punto3_1'] != ""){
//your logic for punto3_1 will goes here....
} elseif(isset($_REQUEST['punto3_2']) && $_REQUEST['punto3_2'] != ""){
//your logic for punto3_2 will goes here....
} else {
//your logic for punto3_1 and punto3_2 will goes here....
}
} else {
echo "Please, do at least one selection";
}
?>