简体   繁体   English

使用同一页面上的复选框或单选按钮的 AJAX 过滤 php MySQL 结果

[英]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 .当用户在home.php输入一些值,则该网页去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.我在单击单选按钮时尝试了一些 jquery,但它的值即将到来并且 sql 查询给出错误。 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这里有一些屏幕截图我的 select.php 页面在此处输入图片说明

when i select any radio button then giving error like this当我选择任何单选按钮然后给出这样的错误在此处输入图片说明

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.还可以尝试打印$_POST$_SESSION数组,以便您可以了解所有参数是否正确传递/传递以及所有会话是否正确设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM