Dnyan
Dnyan

Reputation: 191

AJAX filter php MySQL results using checkboxes or radio button on same page

I have one search box on home page. when user enter the some value on home.php then that page goes to select.php. On select page i have filters with checkboxes, radio button & price range filter.

I have tried some jquery on click of radio button but its value are coming and sql query giving error. I want to filter out my result according to selection of radio button, checkboxes & price range. so please suggset any to me.

i have tried following code for this,

$("#localityid2").click(function () {   
				 		
			 
  var search_keyword = $("#localityid2").val();
  $.ajax({
    type: "POST",
    url: "select.php",
    data: search_keyword ,
    success: function () {
      window.location.href = "select.php?locality="+search_keyword;	 

    }
  });

  return false;
});
<table class="sbox">
  <tr>
    <td>
      <div class="radiobtn">
        <input type="radio" name="location" class="locality" id="localityid1" value="Aundh" style="float: none;">
      </div>
      <div class="text">Aundh</div>
    </td>
  </tr>
  <tr>
    <td>
      <div class="radiobtn">
        <input type="radio" name="location" class="locality" id="localityid2" value="Baner" style="float: none;">   </div>
      <div class="text">Baner</div>
    </td>
  </tr>
  <tr>
    <td>
      <div class="radiobtn"> 
        <input type="radio" name="location" class="locality" id="localityid3" value="Ravet" style="float: none;">
      </div>
      <div class="text">Ravet	</div>
    </td>
  </tr>
  <tr>
    <td>
      <div class="radiobtn">
        <input type="radio" name="location" class="locality" id="localityid4" value="Wakad" style="float: none;">
      </div>
      <div class="text">Wakad</div>
    </td>
  </tr>
</table> 

<h3 style="margin-top: 19px;">Price Range</h3>
<div class="slide-result">
  <div id="price-range"></div>
  <div class="slide-result">
    <input disabled class="amount1" type="text"	id="pr1" value="1000" />
    <input disabled class="amount2" type="text" id="pr2" value="600000" />
  </div>
  <div class="clear"></div>
</div>
<h3>AC/Non AC</h3>

<table class="sbox">
  <tr>
    <td>
      <div class="radiobtn">
        <input type="checkbox" name="actype[]" class="checkboxclass" value="&actype=1" style="float: none;" />
      </div>
      <div class="text">AC</div>
    </td>
  </tr>
  <tr>
    <td>
      <div class="radiobtn">
        <input type="checkbox" value="&actype=0" name="actype[]" class="checkboxclass" style="float: none;" />
      </div>
      <div class="text">Non Ac</div>
    </td>
  </tr>
</table>




include("db.php"); 

$city = $_POST['city'];

list($localitys, $citys) = explode(' - ', $city, 2); 

$_SESSION['city'] = $citys;

$_SESSION['localitys'] = $localitys;

$_SESSION['event'] = $_POST['events']; 

if(isset($_GET['locality']))
{  
$sql=  mysql_query("SELECT * FROM hall_search_data_1 WHERE city_name='".$_SESSION['city']."' AND hall_evnt_type LIKE '%".$_SESSION['event']."%' AND locality = '".$_GET['locality']."' ")or die(mysql_error());
}
else
{
$sql=  mysql_query("SELECT * FROM hall_search_data_1 WHERE city_name='".$_SESSION['city']."' AND hall_evnt_type LIKE '%".$_SESSION['event']."%' ")or die(mysql_error());
}



while($row=mysql_fetch_row($sql))
{
///here my content will print
}

here some screen shot my select.php page enter image description here

when i select any radio button then giving error like this enter image description here

Upvotes: 2

Views: 4591

Answers (1)

Suyog
Suyog

Reputation: 2482

From the comments, I can suggest following fixes to your code.

<?php

include("db.php"); 
$city = '';

if(isset($_POST['city']))
{
    $city = $_POST['city'];
    list($localitys, $citys) = explode(' - ', $city, 2); 
    $_SESSION['city'] = $citys;
}

if(isset($_GET['localitys']))
{
    $_SESSION['localitys'] = $localitys;
}

if(isset($_POST['events']))
{
    $_SESSION['event'] = $_POST['events']; 
}
$sql = "";
if(isset($_GET['locality']))
{  
    $sql .= "SELECT * FROM hall_search_data_1 WHERE 1 = 1 ";

    if(isset($_SESSION['city']) && !empty($_SESSION['city']))
        $sql .=  " AND city_name='".$_SESSION['city']."' ";

    if(isset($_SESSION['event']) && !empty($_SESSION['event']))
        $sql .=  " AND hall_evnt_type LIKE '%".$_SESSION['event']."%' ";

    if(isset($_GET['locality']) && !empty($_GET['locality']))
        $sql .=  " AND locality = '".$_GET['locality']."' ";

    mysql_query($sql) or die(mysql_error());
}
else
{
    $sql .= "SELECT * FROM hall_search_data_1 WHERE 1 = 1 ";

    if(isset($_SESSION['city']) && !empty($_SESSION['city']))
        $sql .=  " AND city_name='".$_SESSION['city']."' ";

    if(isset($_SESSION['event']) && !empty($_SESSION['event']))
        $sql .=  " AND hall_evnt_type LIKE '%".$_SESSION['event']."%' ";

    mysql_query($sql) or die(mysql_error());
}

?>

Also try printing $_POST and $_SESSION arrays so that you can come to know whether all parameters are passing/ passing correctly and all the sessions are setting up properly.

Upvotes: 1

Related Questions