У меня есть этот скрипт на моей главной странице. Есть немало строк, которые не имеют прямого отношения к Ajax-запросу, но я оставил их на случай нежелательного эффекта.
Код: Выделить всё
$(document).ready(function () {
//Stellarnav menu bar
jQuery('.stellarnav').stellarNav();
//EU cookie message
//$.cookieBar();
$(".predictIcon").click(function(e){
e.preventDefault();
$("#resultForm").slideDown();
var event = $(this).data("event");
document.getElementById("result_event").innerHTML = event;
document.getElementById("input_event").value = event;
var event_id = $(this).data("event_id");
document.getElementById("input_event_id").value = event_id;
var thirdclass = $(this).data("thirdclass");
document.getElementById("first").innerHTML = thirdclass + ' First Place';
document.getElementById("second").innerHTML = thirdclass + ' Second Place';
document.getElementById("third").innerHTML = thirdclass + ' Third Place';
$.ajax({
type:"POST",
url:"results_thirdclass_trialgp.php",
data:{thirdclass:"thirdclass"},
contentType: "application/json;",
dataType:'text',
success:function(result){
$(".third").html(result);
console.log(result);
}
});
});
});
Код: Выделить всё
$thirdclass = $_POST['thirdclass'];
//$thirdclass = 'Trial3';
if ($thirdclass == 'TrialGP Women') {
$thirdclass = '3TrialGP Women';
}else if ($thirdclass == 'Trial3') {
$thirdclass = '4Trial3';
}
//Mysqli query to get list of riders
$query_list = "SELECT * FROM riders_trialgp WHERE class LIKE '$thirdclass' ORDER BY rider_id ASC";
$list = $db->query($query_list) or die($db->error.__LINE__);
$thirdclass_options = "Select...";
while ($row_list = $list->fetch_assoc()) {
$thirdclass_options .= "".$row_list['ridername']."";
};
echo $thirdclass_options;
Поскольку вторая строка раскомментирована, скрипт работает независимо от PreventDefault, contentType и dataType, но я добавил их в ответ на советы из Интернета (в основном другие сообщения о переполнении стека). Я также попробовал «html» для типа данных.
Что я делаю не так?
EDIT: с кодом jQuery возникли две проблемы:
Двойные кавычки в строке «данные» означали, что он передавал «третий класс» вместо значения переменной Thirdclass.
"contentType" был неправильным. Я просто удалил строку.
Подробнее здесь: https://stackoverflow.com/questions/798 ... -pass-data
Мобильная версия