我試圖做一個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%肯定的是,它不喜歡這個工作,但我覺得我得到了它差不多吧。如果有人能夠至少請讓我知道我能做些什麼來修復代碼,我會很喜歡它。
謝謝!
OP,爲什麼你將6個變量設置爲相同的值? – Ohgodwhy
修正了它!謝謝:) – pufAmuf