Ron Paulfan Ron Paulfan - 6 months ago 9
PHP Question

PHP - Determine if one more more options are selected

I'm trying to use PHP to determine if one or more options are selected.

I have this code that works for a singular item:

if (isset($_POST['option43'])) {
if (isset($_POST['option41'])) {
$sql .= " mysql string";
} else {
$sql .= " my other sql string";
}
}


But when I try to add || to include another item, the PHP fails to load:

if (isset($_POST['option43'])) {
if (isset($_POST['option41'] || isset($_POST['option42']))) {
$sql .= " mysql string";
} else {
$sql .= " my other sql string";
}
}


Can anyone help me understand how to make this work?

Here is the HTML:


<tr>
<td><input type="checkbox" name="option41" value="bm" />bm </td>
<td><input type="checkbox" name="option42" value="Tv" />tv </td>
</tr>
<tr>
<td><input type="checkbox" name="option43" value="hp" />hp </td>
</tr>


Basically I'm trying to write:

IF option 43 is checked, THIS is the sql
but if option 41 or 42 is checked, THIS is the sql

thanks

Answer

I got your issues you did mistake in bracket complete in if condition please change your if condition block with below given code may it is helpful.

if (isset($_POST['option43'])) {
  if (isset($_POST['option41']) || isset($_POST['option42'])) {
    $sql .= " mysql string";
  } else {  
    $sql .= " my other sql string";
}
}

your mistake is at in if you checking for $_POST['option41'] but you have not complete your isset function bracket after check one condition in same your are checking for other option. so please try above code i hope i will solve you problem.

Comments