how to fix error when url is directly entered with no search keywords?

85 views Asked by At

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.

1

There are 1 answers

2
Sougata Bose On

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.