Need my HTML search form to search the selected options on the search form, then view my results?

advertisements

I have a website with 4 pages: Home Page , Sale Page , Rent Page and Contact Us Page.

On the Home Page is a Search Form that need to Search the Options they select. It is a Property website so if they click on the search button they need to be taken to the Sale Page or Rent Page what they have selected, for instance if they Select For Sale, House, 3 Bedrooms, 2 Bathrooms and then whatever amount on Min Price / Max Price then it needs to go to the Sale Page and retrieve the Houses on the Values they have Selected.

Here is a Image of the Search Form:

Can anyone help me to solve this please?

Here is the HTML Search Form coding:

<form id="advanced_search" action="search.php" class="clearfix" name="advanced_search" method="post">

<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="type"><b>Type</b></label>
<select>
  <option value="House">House</option>
  <option value="Apartment">Apartment</option>
  <option value="Flat">Flat</option>
  <option value="Townhouse">Townhouse</option>
  <option value="Plot">Plot</option>
  <option value="Farm">Farm</option>
</select>

</div>

<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="status"><b>Status</b></label>
<select>
  <option value="For Sale">For Sale</option>
  <option value="For Rent">For Rent</option>
</select>

</div>

<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="bedrooms"><b>Bedrooms</b></label>
<select>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
  <option value="8">8</option>
  <option value="9">9</option>
  <option value="10">10</option>
</select>

</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="baths"><b>Bathrooms</b></label>
<select>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
  <option value="8">8</option>
  <option value="9">9</option>
  <option value="10">10</option>
</select>

</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="min_price"><b>Min Price</b></label>
<select>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
  <option value="8">8</option>
  <option value="9">9</option>
  <option value="10">10</option>
</select>

</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="max_price"><b>Max Price</b></label>
<select>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
  <option value="8">8</option>
  <option value="9">9</option>
  <option value="10">10</option>
</select>

</div>

<div class="clearfix"></div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<button name="search" type="submit" class="btn btn-success">SEARCH PROPERTY</button>
</div>
</form>

Here is the PHP Coding that I am struggling with:

<?php
if(isset($_POST['search']))
{
$key=$_POST["search"];  //key=pattern to be searched
$con=mysqli_connect("localhost", "root", "");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result=mysqli_query($con,"SELECT * FROM properties WHERE");

while($row=mysqli_fetch_assoc($result))
{

}
}

?>


Try this:

<?php

$type = $_POST['type'];
$status = $_POST['status'];
$minPrice = $_POST['min_price'];
$maxPrice = $_POST['max_price'];
$bedrooms = $_POST['bedrooms'];
$baths = $_POST['baths'];

$con=mysqli_connect("localhost","root","","house");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM properties  WHERE tipo='$type' and baths = '$baths' and bedrooms = '$bedrooms' and status = '$status' and price between '$minPrice' and '$maxPrice'");

while($row = mysqli_fetch_array($result)) {
  echo $row['type'] . " " . $row['status'];
  echo "<br>";
}

mysqli_close($con);

I have created a table here and it is working. Do not forget to change on html code " tag and add on this the property "name"