gpt4 book ai didi

javascript - 传递值将 Ajax 抛出到一个 php 文件

转载 作者:行者123 更新时间:2023-11-28 23:25:53 25 4
gpt4 key购买 nike

我正在努力解决这个问题,这可能很容易,但对我来说却非常复杂,因为这是我第一次使用 Javascript。所以基本上我有一个日历,我可以在其中选择日期和每天的不同时间范围。到现在为止,一切都很好。现在,我想将该值传递给一个 php 文件,这样我就可以将它们保存在 MySQL 数据库中。我正在尝试使用 Ajax 来做到这一点,所以这是我目前的代码:

function isSlotSelected($slot) { return $slot.is('[data-selected]'); 
function isSlotSelecting($slot) { return $slot.is('[data-selecting]'); }

/**
* Get the selected time slots given a starting and a ending slot
* @private
* @returns {Array} An array of selected time slots
*/
function getSelection(plugin, $a, $b) {
var $slots, small, large, temp;
if (!$a.hasClass('time-slot') || !$b.hasClass('time-slot') ||
($a.data('day') != $b.data('day'))) { return []; }
$slots = plugin.$el.find('.time-slot[data-day="' + $a.data('day') + '"]');
small = $slots.index($a); large = $slots.index($b);
if (small > large) { temp = small; small = large; large = temp; }
return $slots.slice(small, large + 1);
}

DayScheduleSelector.prototype.attachEvents = function () {
var plugin = this
, options = this.options
, $slots;

this.$el.on('click', '.time-slot', function () {
var day = $(this).data('day');
if (!plugin.isSelecting()) { // if we are not in selecting mode
if (isSlotSelected($(this))) { plugin.deselect($(this)); }
else { // then start selecting
plugin.$selectingStart = $(this);
$(this).attr('data-selecting', 'selecting');
plugin.$el.find('.time-slot').attr('data-disabled', 'disabled');
plugin.$el.find('.time-slot[data-day="' + day + '"]').removeAttr('data-disabled');
}
} else { // if we are in selecting mode
if (day == plugin.$selectingStart.data('day')) { // if clicking on the same day column
// then end of selection
plugin.$el.find('.time-slot[data-day="' + day + '"]').filter('[data-selecting]')
.attr('data-selected', 'selected').removeAttr('data-selecting');
plugin.$el.find('.time-slot').removeAttr('data-disabled');
plugin.$el.trigger('selected.artsy.dayScheduleSelector', [getSelection(plugin, plugin.$selectingStart, $(this))]);
plugin.$selectingStart = null;
}
}
});

this.$el.on('mouseover', '.time-slot', function () {
var $slots, day, start, end, temp;
if (plugin.isSelecting()) { // if we are in selecting mode
day = plugin.$selectingStart.data('day');
$slots = plugin.$el.find('.time-slot[data-day="' + day + '"]');
$slots.filter('[data-selecting]').removeAttr('data-selecting');
start = $slots.index(plugin.$selectingStart);
end = $slots.index(this);
if (end < 0) return; // not hovering on the same column
if (start > end) { temp = start; start = end; end = temp; }
$slots.slice(start, end + 1).attr('data-selecting', 'selecting');
}
console.log(day);
$.ajax({
url: "/Member/test.php",
dataType:"json",
type: "POST",
data: {
weekDay: 'day',
start: 'start',
end: 'end'
}
})
});

html 看起来像:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="style.css">
<link href='http://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
<style>
body { font-family:'roboto'; background-color:#ECF0F1; }
</style>
</head>
<body>
<h1 style="margin:150px auto 30px auto;"></h1>
<div id="day-schedule"></div>

<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="/Member/index.php"></script>
<script>
(function ($) {
$("#day-schedule").dayScheduleSelector({
});
$("#day-schedule").on('selected.artsy.dayScheduleSelector', function (e, selected) {
console.log(selected);
})
})($);
</script>
</body>
</html>

之后我想在控制台中看到这些值,但我没有,而且我真的不知道我做错了什么。

任何帮助将不胜感激。

版本:

enter image description here

最佳答案

您使用的是相当旧的 jQuery 版本。您可能想要升级。

你需要添加一个回调:

$.ajax({
url: "/Member/test.php",
dataType:"json",
type: "POST",
data: {
weekDay: 'day',
start: 'start',
end: 'end'
}
}).success( function( msg ) {
console.log( "success:", msg );
}).error( function( error ) {
console.log( "error:", error );
})

在您的 php 中,在文件末尾使用以下内容。

echo json_encode( $yourvariable );

您可能还需要将 header 设置为在文件顶部输出为 json:

header('Content-type: application/json');

更多例子: http://api.jquery.com/jquery.ajax/

关于javascript - 传递值将 Ajax 抛出到一个 php 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39354335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com