I did a search feature that will enable users to search and see result from database.
First - I want to transfer the search query (What the user searched for) into the search action (searchact.php)
When A user fills the input field below (form) and hit search...
<form action="searchact.php" class=" form-inline" method="post">
<div class="form-group">
<input type="text" name="word" class="form-control" placeholder="House key word">
</div>
<div class="form-group">
<select name="location" class="form-control">
<option value="Bosso">Bosso Campus</option>
<option value="GK">Gidan Kwano Campus (GK)</option>
</select>
</div>
<div class="form-group">
<select name="price" class="form-control">
<option>10-49k</option>
<option>50-99k</option>
<option selected>100-149k</option>
<option>150-199k</option>
</select>
</div>
<input type="submit" class="submit" name="submit" value="Search...">
</form>
I want the next page url (searchact.php) to be something like
wwww.test.com/searchact.php?q=word&location=Bosso&price=10-49k
This is searchact.php
<?php
if($_POST["submit"])
{
$word = $_POST["word"];
$location = $_POST["location"];
$price = $_POST["price"];
$sql = "INSERT INTO search (word,location,price) VALUES (:word,:location,:price)";
$q = $connecDB->prepare($sql);
$q->execute(array(':word'=>$word, ':location'=>$location, ':price'=>$price));
if($q)
{
$query = "SELECT * FROM house WHERE location LIKE :location AND tag LIKE :info AND range LIKE :range order by id desc LIMIT 10";
$stmt = $connecDB->prepare($query);
$stmt->bindValue(':info', '%' . $word . '%', PDO::PARAM_INT);
$stmt->bindValue(':location', '%' . $location . '%', PDO::PARAM_INT);
$stmt->bindValue(':range', '%' . $price . '%', PDO::PARAM_INT);
$stmt->execute();
if ($stmt->rowCount() > 0) {
$result = $stmt->fetchAll();
foreach( $result as $row ) {
$hid=$row["id"];
$name=$row["name"];
?>
<!--Some Html-->
<?php
}}
else {
echo 'No result found';
}
}}
?>
2nd - How do I make sure that a user actually searched for something without manually visiting "searchact.php"
Like.. How do I redirect a user to an error page when they just visit the searchact.php without inputing any search query.