在POST搜索中有多个变量?

时间:2012-12-16 20:01:55

标签: php jquery mysql

我正在尝试使用多个条件进行Jquery Ajax搜索,这是我第一次。我做了一些研究,并找到了将数据发送到php文件的方法,但它只有一个变量。我不确定如何将我的所有6个变量都实现到data: data

这里是:

var FromDate
var ToDate
var MusicStyles
var Locations
var FromPrice
var ToPrice

现在我遇到了困难,我应该发布一些数据。当我有多个变量时,我可以data: dataFromDate, dataToDate, dataMusicStyles,吗?

$("#SearchButton").click(function() {

var dataFromDate            = 'dataFromDate='+ FromDate;
var dataToDate              = 'dataToDate='+ ToDate;
var dataMusicStyles         = 'dataMusicStyles='+ MusicStyles;
var dataLocations           = 'dataLocations='+ Locations;
var dataFromPrice           = 'dataFromPrice='+ FromPrice;
var dataToPrice             = 'dataToPrice='+ ToPrice;


            $.ajax({
                type: "POST",
                url: "do_search.php",
                data: dataFromDate, dataToDate, dataMusicStyles, dataLocations, dataFromPrice, dataToPrice,
                beforeSend: function(html) { // this happens before actual call
                    $("#results").html('');
                    $("#searchresults").show();
                    $(".word").html(searchString);
               },
               success: function(html){ // this happens after we get results
                    $("#results").show();
                    $("#results").append(html);
              }
            });   
});

MySQL看起来像这样:

<?php
//if we got something through $_POST
if (isset($_POST['dataFromDate'])) {
    include('db.php');
    $db = new db();
    // never trust what user wrote! We must ALWAYS sanitize user input
    $word = mysql_real_escape_string($_POST['search']);
    $word = htmlentities($word);
    // build your search query to the database

    $sql = "SELECT
    events.ID,
    events.EVENT_NAME,
    events.start_datetime,
    events.end_datetime,
    events.VENUE_LOCATION,
    events.ENTRANCE_PRICE, 
    venues.VENUE_NAME,
    GROUP_CONCAT(music_styles.MUSIC_STYLE_NAME) AS MUSIC_STYLE_NAME
    FROM events
    INNER JOIN venues 
    ON events.VENUE_LOCATION = venues.ID
    INNER JOIN events_music_styles
    ON events.ID = events_music_styles.event_id
    INNER JOIN music_styles
    ON events_music_styles.music_style_id = music_styles.id
    WHERE start_datetime >= '$phpFromDate'
    AND end_datetime <= '$phpToDate' 
    AND ENTRANCE_PRICE >= '$phpFromPrice'
    AND ENTRANCE_PRICE <= '$phpToPrice' 
    GROUP BY events.ID";

    // get results
    $row = $db->select_list($sql);
    if(count($row)) {
        $end_result = '';
        foreach($row as $r) {
            $result         = $r['title'];
            // we will use this to bold the search word in result
            $bold           = '<span class="found">' . $word . '</span>';   
            $end_result     .= '<li>' . str_ireplace($word, $bold, $result) . '</li>';           
        }
        echo $end_result;
    } else {
        echo '<li>No results found</li>';
    }
}
?>

我100%肯定它不会像这样工作,但我认为我几乎是正确的。我很乐意,如果有人能至少让我知道我能做些什么来修复代码。

谢谢!

2 个答案:

答案 0 :(得分:3)

您将其作为对象发送,如下所示:

$.ajax({
    type: "POST",
    url: "do_search.php",
    data: {dataFromDate    : FromDate,
           dataToDate      : ToDate,
           dataMusicStyles : MusicStyles,
           dataLocations   : Locations,
           dataFromPrice   : FromPrice,
           dataToPrice     : ToPrice
    },
    beforeSend: function(html) { // this happens before actual call
        $("#results").html('');
        $("#searchresults").show();
        $(".word").html(searchString);
    },
    success: function(html) { // this happens after we get results
        $("#results").show();
        $("#results").append(html);
    }
});​

如果第一个值是键,第二个是值,那么{key: value}将在服务器上以$_POST['key']的形式访问,您的值将以您希望的方式访问:

$_POST['dataFromDate']

此外,您不需要在开始时使用所有这些变量,只需在对象中直接使用它们。

答案 1 :(得分:0)

尝试将您的字符串与","连接起来并在服务器上展开它们。