Hey I searched a lot about this problem but everything I tried did not work. :(
My code has 3 dropdowns and they should work like search-filters but every time I select an option from the dropdown i get the undified index error for 3 lines & if I change another dropdown the other two filters get ignored... :(
This only the code for the first dropdown:
<?php
//when the filter changes, this php is called
$output = '';
if(isset($_POST["businessUnit"]))
{
if($_POST["businessUnit"] != '')
{
if($_POST["productGroup"] != '') //first undefined index
{
$sql = "SELECT * FROM item WHERE businessUnit = '".$_POST["businessUnit"]."' and productGroup = '".$_POST["productGroup"]."'";
}
else if($_POST["deviceType"] != '') //second undefined index
{
$sql = "SELECT * FROM item WHERE businessUnit =
'".$_POST["businessUnit"]."' and productGroup = '".$_POST["deviceType"]."'";
}
else if($_POST["productGroup"] != '' && $_POST["deviceType"] != '') //third undefined index error
{
$sql = "SELECT * FROM item WHERE businessUnit = '".$_POST["businessUnit"]."' and productGroup = '".$_POST["productGroup"]."' and deviceType = '".$_POST["deviceType"]."'";
}
else
{
$sql = "SELECT * FROM item WHERE businessUnit = '".$_POST["businessUnit"]."'";
}
}
else
{
$sql = "SELECT * FROM item";
}
$result = sqlsrv_query($connect, $sql);
while($row = sqlsrv_fetch_array($result))
{
$output .= "<tr><td>".
$row['businessUnit']."</td><td>".
$row['productGroup']."</td><td>".
$row['deviceType']."</td><td>".
$row['serialNumber']."</td><td>".
$row['location']."</td><td>".
$row['condition']."</td><td>".
$row['itemDescription']."</td><td>
<input type='checkbox'></input></td></tr>";
}
echo $output;
}
else{
$_POST["businessUnit"] = "";
}
?>
to handle undefined index
error, $array[$key] != ""
won't work, you have to use isset()
first before not equal to blank check.
See the solution below, it may works.
if (isset($_POST["businessUnit"]))
{
if ($_POST["businessUnit"] != '')
{
if (isset($_POST["productGroup"]) && $_POST["productGroup"] != '') //first undefined index
{
$sql = "SELECT * FROM item WHERE businessUnit = '" . $_POST["businessUnit"] . "' and productGroup = '" . $_POST["productGroup"] . "'";
} else if (isset($_POST["deviceType"]) && $_POST["deviceType"] != '') //second undefined index
{
$sql = "SELECT * FROM item WHERE businessUnit =
'" . $_POST["businessUnit"] . "' and productGroup = '" . $_POST["deviceType"] . "'";
} else if (isset($_POST["productGroup"]) && $_POST["productGroup"] != '' && isset($_POST["deviceType"]) && $_POST["deviceType"] != '') //third undefined index error
{
$sql = "SELECT * FROM item WHERE businessUnit = '" . $_POST["businessUnit"] . "' and productGroup = '" . $_POST["productGroup"] . "' and deviceType = '" . $_POST["deviceType"] . "'";
} else
{
$sql = "SELECT * FROM item WHERE businessUnit = '" . $_POST["businessUnit"] . "'";
}
} else
{
$sql = "SELECT * FROM item";
}
$result = sqlsrv_query($connect, $sql);
while ($row = sqlsrv_fetch_array($result))
{
$output .= "<tr><td>" .
$row['businessUnit'] . "</td><td>" .
$row['productGroup'] . "</td><td>" .
$row['deviceType'] . "</td><td>" .
$row['serialNumber'] . "</td><td>" .
$row['location'] . "</td><td>" .
$row['condition'] . "</td><td>" .
$row['itemDescription'] . "</td><td>
<input type='checkbox'></input></td></tr>";
}
echo $output;
} else
{
$_POST["businessUnit"] = "";
}
}