这是我的HTML代码
<form action="sort.php" method="post" >
<select name="option">
<option value="1">Select a category!</option>
<option value="2">Category 1<option>
<option value="3">Category 2</option>
</select>
<select name="option">
<option value="a">Select a 2nd category!(Optional)</option>
<option value="b">Category 1</option>
<option value="c">Category 2</option>
</select>
这是我的PHP代码:
<?php
if (isset($_POST['option']))
{
if ($_POST['option'] == '1') { header('Location: /website'); }
elseif ($_POST['option'] == '2') { header('Location: https://example.com/2'); }
elseif ($_POST['option'] == '2') { header('Location: https://example.com/2'); }
elseif ($_POST['option'] == '2') { header('Location: https://example.com/2'); }
elseif ($_POST['option'] == '3') { header('Location: https://example.com/3'); }
elseif ($_POST['option'] == '4') { header('Location: https://example.com/4'); }
elseif ($_POST['option'] == '5') { header('Location: https://example.com/5'); }
elseif ($_POST['option'] == '6') { header('Location: https://example.com/6'); }
}
?>
如果有人选择价值&#34; 1&#34;我想这样做。 &安培;价值&#34; a&#34;然后X链接将打开。我想让第二个按钮可选。如果有人将其留空,则不会产生任何影响。我怎么能这样做?
答案 0 :(得分:0)
无需mutiple elseif
$arr =array(2,3,4,5,6);
if ($_POST['option'] == '1')
{
header('Location: /website');
}
elseif($_POST['option'] != '' && in_array($_POST['option'],$arr) )
{
header('Location: example.com/'.$_POST['option']);
}
else
{
echo "may be empty";
}
注意:更改input name
并将required
设置为第一个
<select name="option" required>
<option value="">Select a category!</option>
<option value="1">Category 1<option>
<option value="2">Category 2</option>
<option value="3">Category 3</option>
<option value="4">Category 4</option>
<option value="5">Category 5</option>
<option value="6">Category 6</option>
</select>
<select name="option1">
<option values="">Select a 2nd category!(Optional)</option>
<option value="1">Category 1<option>
<option value="2">Category 2</option>
<option value="3">Category 3</option>
<option value="4">Category 4</option>
<option value="5">Category 5</option>
<option value="6">Category 6</option>
</select>