-1

I received the following error message when I go on searchproduct page. How may i fixed it?

Notice: Undefined index: dosageForm in C:\xampp\htdocs\prism\searchproduct.php on line 29

Notice: Undefined index: dosageForm in C:\xampp\htdocs\prism\searchproduct.php on line 32

Notice: Undefined index: strengths in C:\xampp\htdocs\prism\searchproduct.php on line 35

Notice: Undefined index: strengths in C:\xampp\htdocs\prism\searchproduct.php on line 38

if($_POST['dataType']!= "") {
  $dataType = $_POST['dataType']; 
}else{
  $dataType = $_GET['dataType']; 
}

if($_POST['drugCategory']!= ""){
    $drugCategory = $_POST['drugCategory']; 
}else{
    $drugCategory = $_GET['drugCategory']; 
}

if($_POST['productName']!= ""){
    $productName = $_POST['productName']; 
}else{
    $productName = $_GET['productName']; 
}

if($_POST['brandName']!= ""){
    $brandName = $_POST['brandName']; 
}else{
    $brandName = $_GET['brandName']; 
}

if($_POST['dosageForm']!= ""){
    $dosageForm = $_POST['dosageForm']; 
}else{
    $dosageForm = $_GET['dosageForm']; 
}

if($_POST['strengths']!= ""){
    $strengths = $_POST['strengths']; 
}else{
    $strengths = $_GET['strengths']; 
}

i am getting same type of error ,

please help me to resolve

Felix Feliciant
  • 185
  • 3
  • 14
Sohan Sonar
  • 111
  • 1
  • 3
  • 13
  • The Notice comes up because you're trying to access items in an array that don't exist. It's not a problem, but PHP will spawn a Notice on it, because it's not the intended way to do it. Check if something is set through isset or !empty. And on another note, you _could_ use $_REQUEST [http://php.net/manual/en/reserved.variables.request.php], which has a combined array of _POST, _GET, and _COOKIE. That way all your if else statements are unnecessary. – Pim Broens Sep 30 '16 at 12:20

1 Answers1

5

use isset()

if(isset($_POST['drugCategory']))

or in your case : empty()

 if(!empty($_POST['drugCategory']))

Update:

PHP7.1 allow this one too but it will may be forbidden by CodeStyle's rules

if($_POST['drugCategory'] ?? false)
Gectou4
  • 219
  • 1
  • 5