2012-12-16 77 views
0

我試圖做一個jQuery的Ajax搜索與多個條件,這是我第一次。我做了一些研究,並找到了將數據發送到php文件的方法,但只有一個變量。我不知道如何執行我的6個變量到data: data在POST搜索中有多個變量?

在這裏,他們是:

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%肯定的是,它不喜歡這個工作,但我覺得我得到了它差不多吧。如果有人能夠至少請讓我知道我能做些什麼來修復代碼,我會很喜歡它。

謝謝!

+0

OP,爲什麼你將6個變量設置爲相同的值? – Ohgodwhy

+0

修正了它!謝謝:) – pufAmuf

回答

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'] 

而且,你並不需要所有這些變量在一開始,只是在對象中直接使用它們。

+0

啊,讓我試試:) – pufAmuf

+0

哪一個我不需要在開始?這些'var dataFromDate ='dataFromDate ='+ FromDate;'? – pufAmuf

+1

是的!你不需要創建這樣的字符串,只需直接使用該變量,這將是你在服務器端收到的值。 – adeneo

0

試着連接你的字符串和","並在服務器上分解它們。