HTML:
<script>
$(function () {
var min = new Date(2017, 03, 01);
var max = new Date(2017, 05, 30);
$("#datepicker").multiDatesPicker({
maxPicks: 21, numberOfMonths: [1, 3], dateFormat: 'd.m.y', defaultDate: '1.4.17', minDate: min, maxDate: max, onSelect: function (dateText, inst) {
$("#save").text("Speichern").removeClass("btn-success").addClass("btn-default")
}
});
$("#datepicker").addClass("center");
});
</script>
<script>
function speichern() {
var url;
var success;
var data;
var error;
var dataType = "text";
$("#save").text("Wird gespeichert...");
success = function (ergebnis) {
$("#save").text("Gespeichert").removeClass("btn-default").addClass("btn-success");
};
url = "saver/save";
data = {'dates[]': $("#datepicker").multiDatesPicker("getDates")};
error = function (event, jqXHR, thrownError) {
alert(thrownError.toLocaleString());
}
$.post({url: url, success: success, data: data, error: error, dataType: dataType});
}
</script><p>
<div id="datepicker"></div></p>
<button id="save" class="btn btn-default" style="float:right;margin-bottom: 10px" onclick="speichern()">Speichern</button>
.PHP:
<?php
class saver extends CI_Controller
{
function save()
{
$dates = $this->input->post['dates[]'];
var_dump($dates);
echo $dates;
//echo "erfolgreich gespeichert";
}
}
$dates是空的,但我真的不知道为什么!还尝试了"数据","日期","数据"...对于帖子['日期[]'],但没有任何效果。data 是一个对象(出于某种原因?),包含具有选定日期的数组。来自 PHP 脚本的响应为 NULL。发送参数例如:日期[]:"12.6.17"
编辑:它必须是一个CodeIgniter Bug,因为$ _POST给了我想要的东西!
在javascript数组中创建为:
var dates = [] OR var dates = new Array();
因此,请按如下方式更改脚本代码:
<script>
function speichern() {
var url;
var success;
var data;
var error;
dates = [];
dates = $("#datepicker").multiDatesPicker("getDates");
var dataType = "text";
$("#save").text("Wird gespeichert...");
success = function (ergebnis) {
$("#save").text("Gespeichert").removeClass("btn-default").addClass("btn-success");
};
url = "saver/save";
data = {'dates':dates};
error = function (event, jqXHR, thrownError) {
alert(thrownError.toLocaleString());
}
$.post({url: url, success: success, data: data, error: error, dataType: dataType});
}
</script>
在 php 中:
$date = $this->input->post('dates');
print_r($date);
希望它能帮助你。