如何从JS发送变量到我的Laravel控制器 [英] How to send a variable from JS to my Laravel Controller
问题描述
对不起.我已经研究了几个小时,但仍然没有成功.
I'm sorry. I have researched for several hours, several and I still do not reach success with this.
让我解释一下:
我在路线/events/1
上,所以我已经在该事件的详细信息之内了.
I'm on the route /events/1
, so I'm already inside the details of the event.
当我选择一个日期(这就是为什么它是变量"day_id")时,它将进行查询.但是每个事件都有不同的日子,这就是为什么我也需要传递事件ID来进行查询.
When I select a date (that is why it is the variable "day_id") it makes a query. But each event has different days, that is why I need to pass the event ID also to make the query.
$event_id = "Dynamic Value";
$data = Hour::where('selected_date', $day_id)->where('event_id', $event_id)->get();
我不知道自己是否能很好地解释自己,我还需要做的是ID是动态的,例如 day_id
.
I don't know if I explain myself well, what I need to do is also that the ID is dynamic, like the day_id
.
我已经在许多方面尝试了5天以上.
I've been trying for more than 5 days in many ways.
我正在尝试通过JavaScript获取事件"的ID,我想将其作为变量发送给控制器,但目前还不能.有人可以帮我吗?
I'm trying to get the ID of my "Event" via JavaScript and I want to send it as a variable to the controller but I have not been able to yet. Could anyone help me, please?
@section('scripts')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(document).on('change', '#day', function(){
var day_id=$(this).val();
var event_id = $('#event_id').val();
// console.log(day_id);
// console.log(event_id);
$.get('/hour?day_id=' + day_id, function(data){
$('#hours').empty();
$('#hours').append('<option value="0" disable selected="true"> Selecciona la hora</option>');
for (var i = 0; i < data.length; i++) {
currentDay = data[i].hour;
console.log(currentDay);
$('#hours').append('<option value="'+ data[i].hour +'">'+ data[i].hour +'</option>');
}
})
});
});
</script>
@endsection
使用此代码,我获取了日期的值,但是现在我需要根据事件的ID显示时间.这就是为什么我需要将变量 event_id
传递给控制器方法的原因.我正在使用 var event_id = $('#event_id').val();
With this code I'm getting the value of the date, but now I need to show the time depending on the ID of the event. That's why I need to pass the variable event_id
to the controller method. I'm using var event_id = $('#event_id').val();
这是控制器中的代码:
{
$day_id = Input::get('day_id');
$event_id = '2'; //I need this to be dynamic
$data = Hour::where('selected_date', $day_id)->where('event_id', $event_id)->get();
return response()->json($data);
}
Web.php
Route :: get('/hour','Events \ EventController @ getHours')-> name('ajax.hour');
再次抱歉.我已经研究了几个小时,但仍然没有成功.
Once again, I'm sorry. I have researched for several hours, several and I still do not reach success with this.
我需要知道如何将 event_id
变量传递给laravel控制器.谢谢!
What I need is to know how to how to pass event_id
variable to laravel controller. Thanks!
推荐答案
是的,您在这里获取事件ID:
So, yes, you're getting the event ID here:
var event_id = $('#event_id').val();
但是您没有对该变量做任何事情.如果您从不使用分配的变量,则表明您的代码有问题.一些编程语言甚至会因此而拒绝编译.
But you're not doing anything with that variable. If you never use a variable you've assigned, that's a major indication that you have a problem in your code. Some programming languages will even refuse to compile because of it.
将事件ID传递到GET请求的查询字符串中,就像处理日期ID一样:
Pass the event ID in the query string of your GET request, just like you're doing with day ID:
$.get('/hour?day_id=' + day_id + '&event_id=' + event_id, function(data){
然后以相同的方式从输入外观中检索它:
Then retrieve it the same way from the Input facade:
$event_id = Input::get('event_id');
这篇关于如何从JS发送变量到我的Laravel控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!