I have a page where I'm trying to get my search box to perform actions. When there's a search I have 3 sites: Google, wiki, and duckduckgo. When I upload page to the server I'm getting syntax errors. I have never done an if statement in PHP before. I looked at tutorials in w3schools and I am still confused. I have tried colons and curly brackets and I am still confused.
Can someone give me a tip in the right direction?
<form action="search.php" method="get">
<?php
$site = substr(filter_input(INPUT_GET, 'site', FILTER_SANITIZE_STRING), 0, 8);
$terms = substr(filter_input(INPUT_GET, 'terms', FILTER_SANITIZE_STRING), 0, 25);
if ($site=="google") header('Location:https://www.google.com/#q=' . $terms); {
}else{
if ($site=="Wikipedia")header ('Location:https://www.wikipedia.org/#q=' . $terms); {
} else ($site=="DuckDuckGo")header ('Location:https://duckduckgo.com/#q=' .$terms);
endif;
}
}
?>
You have used the if else syntax terribly wrong. Always the task to be done if an statement is correct should come inside parentheses.
Substitute
if ($site=="google") header('Location:https://www.google.com/#q=' . $terms);{
}else{
if ($site=="Wikipedia")header ('Location:https://www.wikipedia.org/#q=' . $terms);{
} else ($site=="DuckDuckGo")header ('Location:https://duckduckgo.com/#q=' .$terms);
endif;
}
with the following. It is the correct syntax
if ($site=="google"){
header('Location:https://www.google.com/#q=' . $terms);
}elseif ($site=="Wikipedia"){
header ('Location:https://www.wikipedia.org/#q=' . $terms);
}elseif ($site=="DuckDuckGo"){
header ('Location:https://duckduckgo.com/#q=' .$terms);
}else{
//if $site doesn't match any of the above
}
try like this. if you have one line in below the if. then ignore the curly {} brace
if ($site=="google")
header('Location:https://www.google.com/#q=' . $terms);
else if ($site=="Wikipedia")
header ('Location:https://www.wikipedia.org/#q=' . $terms);
else if($site=="DuckDuckGo")
header ('Location:https://duckduckgo.com/#q=' .$terms);
else
//if your site == google not work