error: Notice: Undefined index: fk

116 views Asked by At

can someone assist me with fixing the error i'm receiving.

$search_key = '';
/*if(isset($_POST) && ($_POST["fk"])!=NULL){
    $search_key = $_POST["fk"];
}elseif(isset($_GET) && ($_GET["fk"]!=NULL)){
    $search_key = $_GET["fk"];
}
*/

if(!isset($_POST["fk"])){
    $search_key = $_POST['fk'];
}elseif(isset($_GET["fk"])){
    $search_key = $_GET["fk"];
}
3

There are 3 answers

1
Tseng On BEST ANSWER

You have a !isset in the first line of the code below

if(!isset($_POST["fk"])){
    $search_key = $_POST['fk'];
}elseif(isset($_GET["fk"])){
    $search_key = $_GET["fk"];
}

So if it $_POST["fk"] is not set, it will try to read it. Hence the error message. Just use isset instead of !isset.

Just a little side-note: if you want to have the value from either a $_POST or $_GET, you could simply use $_REQUEST['fk']; as $_REQUEST holds the values merged from $_COOKIE, $_POST and $_GET (order depends on your PHP configuration)

0
Mark Baker On

You're testing for NOT isset $_POST["fk"]

if(isset($_POST["fk"])){ 
    $search_key = $_POST['fk'];
0
FatherStorm On

if(array_key_exists("fk",$_POST)){
$search_key = $_POST['fk'];
}elseif(array_key_exists("fk",$_GET)){
$search_key = $_GET["fk"];
}