大家好有问题,在一个datapicker中插入值,返回null这是我的ajax代码
<script type="text/javascript">
// add a new post
$(document).on('click', '.add-modal', function() {
$('.modal-title').text('Add');
$('#addModal').modal('show');
});
$('.modal-footer').on('click', '.add', function() {
$.ajax({
type: 'POST',
url: 'posts',
data: {
'_token': $('input[name=_token]').val(),
'title': $('#title_add').val(),
'content': $('#content_add').val(),
'date':$('#fecha').val(),
'date2':$('#fecha').val()
}<script/>
和我的商店控制器
public function store(Request $request)
{
$validator = Validator::make(Input::all(), $this->rules);
if ($validator->fails()) {
return Response::json(array('errors' => $validator->getMessageBag()->toArray()));
} else {
$post = new Post();
$post->title = $request->title;
$post->content = $request->content;
$post->fechai = Carbon::createFromFormat('Y-m-d', $request->fecha);
$post->fechaf = $request->datepicker1;
$post->user_id = \Auth::user()->id;
$post->save();
return response()->json($post);
}
}
为什么两个datapicker返回null
视图
<div class="form-group" >
<label class="control-label col-sm-2" for="date2">Fecha 2:</label>
<div class="input-group">
<input type="text" class="form-control" id="datepicker1" name="fecha1">
<div class="input-group-addon">
<span class="glyphicon glyphicon-th"></span>
</div>
</div>
</div>
这是我的数据贴纸我瘦了我的ajax没有工作
答案 0 :(得分:1)
您有date
作为$ _POST键而不是fecha
$post->fechai = Carbon::createFromFormat('Y-m-d', $request->date);
您的ID不正确
'date':$('#datepicker1').val(),