PHP:选中选择值和文本框!=无效的
这可能是一个愚蠢的问题,但我现在头脑僵住了 HTMLPHP:选中选择值和文本框!=无效的,php,html,if-statement,Php,Html,If Statement,这可能是一个愚蠢的问题,但我现在头脑僵住了 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">
<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>
使用此代码,即使我选择了“option1”或“option2”,也会收到错误消息(您选择了其他,但没有键入任何内容),为什么?如果我选择“选项1”或“选项2”,它将告诉我其他消息(继续)
提前感谢!:)
解决方案(感谢Olvathar)
试试这个
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
}
因为如果您只提到else,它也会有一个条件,即$\u POST['text']未被选择为其他
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
}
因为如果您只提到else,它也会有一个条件,即$\u POST['text']没有被选为其他试试这个
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 !';
}
}
试试这个
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 !';
}
}
您正在强制选择“其他”以继续,请尝试以下操作
if($_POST['text'] != "other" || $_POST['own_text'] != "") {
echo 'Go on';
} else {
echo 'You chose other but has not typed anything';
}
您正在强制选择“其他”以继续,请尝试以下操作
if($_POST['text'] != "other" || $_POST['own_text'] != "") {
echo 'Go on';
} else {
echo 'You chose other but has not typed anything';
}
试试这个:
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';
}
试试这个:
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';
}
如果选择
other
并将own\u text
保留为空,它将显示继续
。在我的情况下,这是最简单的解决方案。非常感谢你,伙计对不起。正如你刚才所说,乍一看,它看起来是错的。但它是完美的。@Treps,@Olvathar当你选择一个选项时=其他且文本框中没有值,结果会是什么…?当您选择与“其他”不同的选项时,结果将是“继续”。不管own_text值是多少@SherinJoseit将显示继续
,如果您选择其他
,并将own_text
保留为空,这是我的情况下最简单的解决方案。非常感谢你,伙计对不起。正如你刚才所说,乍一看,它看起来是错的。但它是完美的。@Treps,@Olvathar当你选择一个选项时=其他且文本框中没有值,结果会是什么…?当您选择与“其他”不同的选项时,结果将是“继续”。不管自己的文本值是多少@SherinJoseyou更改其他值You更改其他值