Reputation: 800
This might be a silly question but I've got brainfreeze right now..
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>
If someone select value "other" from the select I also want to make sure the textbox (own_text) not are NULL, with PHP.
Tried
if($_POST['text'] == "other" && $_POST['own_text'] != "") {
echo 'Go on';
} else {
echo 'You chose other but has not typed anything';
}
With this code I've got the error message (You chose other but has not typed anything) even if I chose "option1" or "option2", why? If I chose "option1" or "option2" it shall tell me the other message (Go on).
Thanks in advance! :)
Solution (thanks Olvathar)
if($_POST['text'] != "other" || $_POST['own_text'] != "") {
echo 'Go on';
} else {
echo 'You chose other but has not typed anything';
}
Upvotes: 0
Views: 3633
Reputation: 1
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';
}
Upvotes: 0
Reputation: 2548
Try this:
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';
}
Upvotes: 1
Reputation: 4099
try this
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
}
because if you just mention else it will also have condition where $_POST['text'] is not selected as other
Upvotes: 1
Reputation: 551
You are forcing select "other" to continue, try this
if($_POST['text'] != "other" || $_POST['own_text'] != "") {
echo 'Go on';
} else {
echo 'You chose other but has not typed anything';
}
Upvotes: 1
Reputation: 2526
Try this,
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 !';
}
}
Upvotes: 1