这可能是一个愚蠢的问题,但我现在已经有了脑力......
HTML
<b>Comment</b><br>
<select name="text" id="text">
<option value="">Select a option</option>
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="other">Other</option>
</select>
<div class="other box">
<b>Comment 2</b><br>
<input type="text" name="own_text">
</div>
如果有人从选择中选择“other”值,我也想确保文本框(own_text)不是NULL,而不是PHP。
试过
if($_POST['text'] == "other" && $_POST['own_text'] != "") {
echo 'Go on';
} else {
echo 'You chose other but has not typed anything';
}
使用此代码,我收到了错误消息(您选择了其他但未输入任何内容),即使我选择了“option1”或“option2”,为什么?如果我选择“option1”或“option2”,它会告诉我另一条消息(继续)。
提前致谢! :)
解决方案(感谢Olvathar)
if($_POST['text'] != "other" || $_POST['own_text'] != "") {
echo 'Go on';
} else {
echo 'You chose other but has not typed anything';
}
答案 0 :(得分:1)
试试这个
if($_POST['text'] == "other" && $_POST['own_text'] != "") {
echo 'Go on';
} else if($_POST['text'] == "other" && $_POST['own_text'] == "") {{
echo 'You chose other but has not typed anything';
}else if($_POST['text'] != "other")
{
// do other things
}
因为如果你只是提到其他它也会有条件,其中$ _POST ['text']未被选为其他
答案 1 :(得分:1)
试试这个,
if($_POST['text'] == "other"){
if($_POST['own_text'] != "") {
echo 'Go on';
} else {
echo 'You chose other but has not typed anything';
}
}
else{
if($_POST['text']!=''){
echo 'Go on';
}
else{
echo 'You must select an option !';
}
}
答案 2 :(得分:1)
您正在强制选择“其他”继续,请尝试此
if($_POST['text'] != "other" || $_POST['own_text'] != "") {
echo 'Go on';
} else {
echo 'You chose other but has not typed anything';
}
答案 3 :(得分:1)
试试这个:
if( ($_POST['text'] == "other" || empty($_POST['text'])) && $_POST['own_text'] != "") {
echo 'Go on';
} else if ($_POST['text'] != "other") {
echo 'You chose other but has not typed anything';
}
答案 4 :(得分:0)
if($_POST['text'] == "other" && $_POST['own_text'] == "") {
echo 'You chose other but has not typed anything';
} else if($_POST['text'] == "other" && $_POST['own_text'] != "") {
echo 'You chose other and typed something';
} else {
echo 'go on';
}