我要在1个函数中执行2个Ajax调用。如果我的按钮被单击,我只希望if语句起作用。虽然我有另一个ajax调用,无论何时调用该函数,该调用始终会关闭。我想做的是列出项目。我希望我的项目列表在页面出现后立即出现。当我输入一个项目时,列表会刷新,更新后的列表包含我刚刚输入的项目。希望对我的英语谢谢你道歉。
JS
$(document).ready(function(){
refresh();
function refresh(){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
});
$.ajax({
url:"{{url('/spreadsheet/store')}}",
method:"POST",
success: function(result){
$('.test').html(result);
}
});
}
$('.btn-secondary').click(function(e){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
});
$.ajax({
url:"{{url('/spreadsheet/store')}}",
method:"POST",
data:{
spreadsheet_name:$('input[name=spreadsheet_name]').val()
},
success: function(result){
refresh();
}
});
});
});
PHP
public function store(Request $request)
{
// I want this to go off only if its the request from the click. This is to enter the item into the db
if ($request->exists()) {
$post = new Spreadsheet;
$post->user_id = auth()->user()->id;
$post->spreadsheet_name = $request->spreadsheet_name;
$post->save();
}
// I want this to go off everytime. This one is for the list to appear to my page from the db
$output="";
$post = new Spreadsheet;
$post->id = auth()->user()->id;
$spreadsheet = DB::table('Spreadsheets')->where('user_id', '=', $post->id)->get();
if ($spreadsheet) {
foreach ($spreadsheet as $key => $spreadsheet) {
$output.='<button class="btn-list" id="'.$spreadsheet->spreadsheet_name.'">'.$spreadsheet->spreadsheet_name.'</button>';
}
}
return Response($output);
}