0

I am creating small search engine for job portal. I used following code for searching.

<form method="get" action="search.php">
 <input type="text" name="keyword">
 <input type="text" name="location">
</form>

when I search it sends input by url and displays the results. e.g : url: localhost/jobportal/search.php?keyword=php&location=India

But if I directly open the page by entering url : "localhost/jobportal/search.php" in browser, it shows following error.

Notice: Undefined index: keyword in C:\xampp\htdocs\jobportal\search\index.php on line 384
Notice: Undefined index: location in C:\xampp\htdocs\jobportal\search\index.php on line 385

I know why this error is occurring I only need to know how to avoid this error.

Omkar
  • 51
  • 1
  • 5

1 Answers1

0

Just add a check if they exists. Use empty. -

if(!empty($_GET['keyword'])) {
    // use value
}

if(!empty($_GET['location'])) {
    // use value
}

empty() will check if it is set and the value is not null or false.

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87