Need my HTML Search Form to search the Options Selected on the Search Form and then show my results?

StackOverflow https://stackoverflow.com/questions/23619840

Вопрос

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:

enter image description here

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"

Другие советы

There are a lot of things wrong, first you didn't named your html form tags, and secondly you didn't use the $key variable in your php code to modify your query. You need learn about basic form interactions. I also strongly suggest you to look into MVC, having a sql connection just 10 lines above the template code is really bad.

You have to change this: $result=mysqli_query($con,"SELECT * FROM properties WHERE");

first question, are the options: "max price, type and etc..." on the same table?

you can do something like that:

$type = $_POST['type'];

$status = $_POST['status'];

$minPrice = $_POST['minPrice'];

$maxPrice = $_POST['maxPrice'];

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

to the same for another fields, should work

try this in your query:

$type = $_POST['$type'];
$status = $_POST['$status'];
$bedrooms = $_POST['bedrooms'];
$bathrooms = $_POST['bathrooms'];
$minPrice = $_POST['minPrice'];
$maxPrice = $_POST['maxPrice'];

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

it will work if your code above your $result is ok

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top