如何搜索值必填字段和可选字段

时间:2016-12-01 09:39:00

标签: php ajax html5 jquery-ajaxq

我有一个搜索表单,我有4个表单字段

  1. 属性
  2. 最低价格
  3. 最高价格
  4. 位置
  5. 这里属性字段只是强制性的,其余3个字段是可选的,现在我将告诉我的要求用户选择属性只表示我想显示详细信息(从db获取匹配的记录).suppose用户选择属性和最小价格意味着我想要显示(从db获取匹配的记录)的详细信息,对于剩余的两个文件同样如此,我正在尝试这样但我无法得到,请参阅此处我的数据库结构click here

    $(document).ready(function(){
        $('#buy_btn').click(function(){
            if($('#buyForm').valid()){
                $.ajax({
                    type:'POST',
                    url :"select_buy_properties.php",
                    data: $('form#buyForm').serialize(),
                    success: function(data) {
                        console.log(data);
                        
                    },
                    error:function(exception){
                        alert('Exeption:'+exception);
                    }
                });
                return false;
            }
        });
    });
    <form id="buyForm" method="POST">
        <div class="col-md-11 col-sm-11 col-xs-11" >
            <div class="search1">					 
                <div class="col-md-2 col-sm-3 col-xs-3" style="padding-left:0px;padding-right: 2px;">
                    <!---->
                    <select class="ui search dropdown" style=" width: 100%; height: 45px;padding-left:10px;font-size: 17px !important;    font-family: inherit;" data-msg-required="Select Propery" required="" aria-required="true" name="property"> 
                        <option value="">Property</option>
                        <?php
                        $p = mysql_query("SELECT * FROM properties WHERE status !='1'");
                        while($pp = mysql_fetch_assoc($p))
                        {
                            echo '<option value="'.$pp["id"].'">'.$pp["properties"].'</option>';
                        } 
                        ?>
                        
                    </select>
                    <!---->
                </div> 
                
                <div class="col-md-2 col-sm-3 col-xs-3" style="padding-left:0px;padding-right: 2px;">
                    <!---->
                    <select class="ui search dropdown" style=" width: 100%; height: 45px;padding-left:10px;font-size: 17px !important;    font-family: inherit;" name="min_price"> 
                        <option value="">Min Price</option>
                        <option value="500000-1000000">5-10 Lacs</option>
                        <option value="1000000-2000000">10-20 Lacs</option>
                        <option value="2000000-3000000">20-30 Lacs</option>
                        <option value="3000000-4000000">30-40 Lacs</option>
                        <option value="4000000-5000000">40-50 Lacs</option>
                        <option value="5000000-6000000">50-60 Lacs</option>
                        <option value="6000000-7000000">60-70 Lacs</option>
                        <option value="7000000-8000000">70-80 Lacs</option>
                        <option value="8000000-9000000">80-90 Lacs</option>
                        <option value="10000000-100000000">100+ Lacs</option>
                        
                    </select>
                    <!---->
                </div>
                
                <div class="col-md-2 col-sm-3 col-xs-3" style="padding-left:0px;padding-right: 2px;">
                    <!---->
                    <select class="ui search dropdown" style=" width: 100%; height: 45px;padding-left:10px;font-size: 17px !important;    font-family: inherit;" name="max_price"> 
                        <option value="">Max Price</option>
                        <option value="1000000-2000000">10-20 Lacs</option>
                        <option value="2000000-3000000">20-30 Lacs</option>
                        <option value="3000000-4000000">30-40 Lacs</option>
                        <option value="4000000-5000000">40-50 Lacs</option>
                        <option value="5000000-6000000">50-60 Lacs</option>
                        <option value="6000000-7000000">60-70 Lacs</option>
                        <option value="7000000-8000000">70-80 Lacs</option>
                        <option value="8000000-9000000">80-90 Lacs</option>
                        <option value="10000000-100000000">100+ Lacs</option>
                    </select>
                    <!---->
                </div>
                
                
                <div class="col-md-4 col-sm-4 col-xs-4" style="padding-left:0px;padding-right: 2px;">
                    <!---->
                    <select class="ui search dropdown" style=" width: 100%; height: 45px;padding-left:10px;font-size: 17px !important;    font-family: inherit;" name="location"> 
                        <option value="">Location</option>
                        <?php
                        $l = mysql_query("SELECT * FROM location WHERE status !='1'");
                        while($ll = mysql_fetch_assoc($l))
                        {
                            echo '<option value="'.$ll["id"].'">'.$ll["location"].'</option>';
                        } 
                        ?>
                    </select>
                    <!---->
                </div>
                
                <div class="col-md-2 col-sm-2 col-xs-2" style="padding-left:0px;padding-right: 2px; !important;padding-right:0px !important;">
                    <button type="button" class="btn btn-primary btn-lg" style="background-color: #e85700 !important;
                    border-color: #e85700 !important; font-size: 14px !important;height: 44px; width:100%; padding-left:11px; " id="buy_btn">SEARCH<!--<span class="glyphicon glyphicon-search" aria-hidden="true"></span>--></button>					  
                </div>
            </div>
        </div>
    </form>

    select_buy_properties.php

    include("admin/dbconfig.php");
    $property = $_POST['property'];
    $min_price = $_POST['min_price'];//5000000-1000000
    $min_explode = explode("-",$min_price);
    $min1= $min_explode[0] ; //5000000 5 lacs
    $min2= $min_explode[1] ; //1000000 10 lacs
    $max_price = $_POST['max_price'];
    
    $max_explode = explode("-",$min_price);
    $max1= $max_explode[0] ; //10,00,000 ten lacs
    $max2= $max_explode[1] ; //20,00,000 twenty lacs
    
    $location = $_POST['location'];
    $query="SELECT * FROM add_projects WHERE property ='$property' AND status!=''"; 
    if($min_price!='')
    {
        $query.="AND ( total_price BETWEEN '600000' AND '700000')";
    }
    if($max_price!='')
    {
        $query.="AND ( total_price BETWEEN '700000' AND '1000000')";
    }
    if($location!='')
    {
        $query.=" AND project_location='$location'";
    }
    $sql=mysql_query($query);
    while($rlt=mysql_fetch_assoc($sql)){
        echo $rlt['project_title'];
    }
    

1 个答案:

答案 0 :(得分:1)

我在你的代码上工作它有致命的错误。 我测试并纠正了错误

尝试以下代码,将文件命名为 select_buy_properties.php 并简单地在代码中放置数据库配置设置,例如主机,用户,密码变量 $ host,$ user,$ password 。 数据库名称可以在命令mysql_select_db(' DatabaseName ',$ conn)中更改,并且 表名称

add_projects 属性位置

<html>

<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
    <script>
    $(document).ready(function(){
    $('#buy_btn').click(function(){
        alert('hai')
    if($('#buyForm').valid()){
           $.ajax({
             type:'POST',
             url :"select_buy_properties.php",
             data: $('form#buyForm').serialize(),
             success: function(data) {
             console.log(data);

           },
         error:function(exception){
         alert('Exeption:'+exception);
        }
      });
    return false;
    }
    });
    });
    </script>
    </head>
    <body>

    <?php
    ini_set('display_errors', 1);
    $host ="localhost";
    $user ="root";
    $password="root";

    $conn = mysql_connect($host,$user,$password);
    if(!$conn){
        die('Could not connect: '.mysql_error());
    }


    if (!mysql_select_db('DatabaseName', $conn)) {
        echo 'Could not select database';
        exit;
    }

    $query="";

    $property = '';
    if (isset($_POST['property']) && !empty($_POST['property'])) 
    {
        $property = $_POST['property'];
        $query
        ="SELECT * FROM add_projects WHERE status != '1' AND property = '".$property."'";
    }
    $min_price = '';
    $min1 = '';
    $min2 = '';
    if (isset($_POST['min_price']) && !empty($_POST['min_price'])) 
    {
        $min_price = $_POST['min_price'];
        $min_explode = explode("-",$min_price);
        $min1= $min_explode[0] ; //5000000 5 lacs
        $min2= $min_explode[1] ; //1000000 10 lacs
        if($min_price!='')
        {
         $query.=" AND ( total_price BETWEEN '600000' AND '700000')";
        }
    }

    $max_price = '';
    $max1 = '';
    $max2 = '';
    if (isset($_POST['max_price']) && !empty($_POST['max_price'])) 
    {
        $max_price = $_POST['max_price'];
        $max_explode = explode("-",$max_price);
        $max1= $max_explode[0] ; //10,00,000 ten lacs
        $max2= $max_explode[1] ; //20,00,000 twenty lacs
        if($max_price!='')
        {
          $query.=" AND ( total_price BETWEEN '700000' AND '1000000')";
        }
    }
    $location = '';
    if (isset($_POST['location']) && !empty($_POST['location'])) 
    {
        $location = $_POST['location'];
        if($location!='')
        {
         $query.=" AND project_location='$location'";
        }
    }


    if($query != '')
    {
        $sql=mysql_query($query);

        while($rlt = mysql_fetch_array($sql)){
            echo $rlt['project_title'];
        }
    }
    ?>
    <form id="buyForm" method="POST">
     <div class="col-md-11 col-sm-11 col-xs-11" >
       <div class="search1">                     
        <div class="col-md-2 col-sm-3 col-xs-3" style="padding-left:0px;padding-right: 2px;">
        <!---->
        <select class="ui search dropdown" style=" width: 100%; height: 45px;padding-left:10px;font-size: 17px !important;    font-family: inherit;" data-msg-required="Select Propery" required="" aria-required="true" name="property"> 
         <option value="">Property</option>
            <?php
            $p = mysql_query("SELECT * FROM properties WHERE status !='1'");
            while($pp = mysql_fetch_assoc($p))
            {
                echo '<option value="'.$pp["id"].'">'.$pp["properties"].'</option>';
            } 
            ?>

        </select>
        <!---->
        </div> 

      <div class="col-md-2 col-sm-3 col-xs-3" style="padding-left:0px;padding-right: 2px;">
        <!---->
        <select class="ui search dropdown" style=" width: 100%; height: 45px;padding-left:10px;font-size: 17px !important;    font-family: inherit;" name="min_price"> 
                <option value="">Min Price</option>
                <option value="500000-1000000">5-10 Lacs</option>
                <option value="1000000-2000000">10-20 Lacs</option>
                <option value="2000000-3000000">20-30 Lacs</option>
                <option value="3000000-4000000">30-40 Lacs</option>
                <option value="4000000-5000000">40-50 Lacs</option>
                <option value="5000000-6000000">50-60 Lacs</option>
                <option value="6000000-7000000">60-70 Lacs</option>
                <option value="7000000-8000000">70-80 Lacs</option>
                <option value="8000000-9000000">80-90 Lacs</option>
                <option value="10000000-100000000">100+ Lacs</option>

        </select>
        <!---->
        </div>

      <div class="col-md-2 col-sm-3 col-xs-3" style="padding-left:0px;padding-right: 2px;">
        <!---->
        <select class="ui search dropdown" style=" width: 100%; height: 45px;padding-left:10px;font-size: 17px !important;    font-family: inherit;" name="max_price"> 
                <option value="">Max Price</option>
                <option value="1000000-2000000">10-20 Lacs</option>
                <option value="2000000-3000000">20-30 Lacs</option>
                <option value="3000000-4000000">30-40 Lacs</option>
                <option value="4000000-5000000">40-50 Lacs</option>
                <option value="5000000-6000000">50-60 Lacs</option>
                <option value="6000000-7000000">60-70 Lacs</option>
                <option value="7000000-8000000">70-80 Lacs</option>
                <option value="8000000-9000000">80-90 Lacs</option>
                <option value="10000000-100000000">100+ Lacs</option>
        </select>
        <!---->
        </div>


        <div class="col-md-4 col-sm-4 col-xs-4" style="padding-left:0px;padding-right: 2px;">
        <!---->
        <select class="ui search dropdown" style=" width: 100%; height: 45px;padding-left:10px;font-size: 17px !important;    font-family: inherit;" name="location"> 
         <option value="">Location</option>
            <?php
            $l = mysql_query("SELECT * FROM location WHERE status !='1'");
            while($ll = mysql_fetch_assoc($l))
            {
                echo '<option value="'.$ll["id"].'">'.$ll["location"].'</option>';
            } 
            ?>
        </select>
        <!---->
        </div>

     <div class="col-md-2 col-sm-2 col-xs-2" style="padding-left:0px;padding-right: 2px; !important;padding-right:0px !important;">
      <button type="submit" class="btn btn-primary btn-lg" style="background-color: #e85700 !important;
    border-color: #e85700 !important; font-size: 14px !important;height: 44px; width:100%; padding-left:11px; " id="buy_btn">SEARCH<!--<span class="glyphicon glyphicon-search" aria-hidden="true"></span>--></button>                    
    </div>
    </div>
    </div>
    </form>


    </body>
    <?php
        mysql_close($conn);
    ?>

    </html>

我在数据库屏幕截图中观察到了表名add_projects1。但是你在代码中写了表名add_projects。请在数据库中重命名为add_projects。