為什么當我同時選中兩個復選框時它會回傳 2?當兩個復選框都打開時,如何回傳不同的回應?
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";
}
?>
有什么建議嗎?
uj5u.com熱心網友回復:
為每個復選框使用單獨的名稱屬性值,并使用 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";
}
?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/480948.html
