Utilizing Bootstrap Datepicker, combining Javascript and Laravel query to navigate to a specified URL

In my application, there are 3 interconnected tables: Events, Spots, and Bookings. Each Event can have multiple Spots, and each Spot can have multiple Bookings. My goal is to display the list of bookings for a specific spot using a Bootstrap calendar picker.

To achieve this, I have implemented a Bootstrap Calendar along with 3 hidden input fields (Vendor_id, Event_id, and the selected Date obtained through the changeDate event).

The challenge lies in passing these 3 inputs to retrieve the spot_id and eventually redirect to the corresponding link.

Here is a snippet of my code:

 <div class="calendar">
    <h2>Calendar</h2>
    <div id="datepicker" data-date-format="yyyy-mm-dd" data-date="today">
    </div>
    <input type="hidden" id="my_hidden_input">
    <input type="hidden" id="event_id" value="{{ $event->id }}">
    <input type="hidden" id="vendor_id" value="{{ $event->vendor_id }}">
 </div>


//JavaScript
<script type="text/javascript">
   $('#datepicker').datepicker();
   $('#datepicker').on("changeDate", function() {
   $('#my_hidden_input').val(
   $('#datepicker').datepicker('getFormattedDate')
   );
   var caldate = $('#my_hidden_input').val();
   var event_id = $('#event_id').val();
   var vendor_id = $('#vendor_id').val();
   //console.log(abc);
   //console.log(event_id);
   $.get("/"+vendor_id+"/events/"+event_id +"/"+caldate, function( data ) 
   {
        console.log(data);//I get this as UNDEFINED
        window.location.href="/"+vendor_id+"/events/"+event_id +"/"+data;
   });

   });
</script>

Controller Method
public function getspotid($vendor_id,$event_id,$date)
{

    $spotid = Spot::select('id')
    ->where('event_id','=',$event_id)
    ->where('event_date','=',$date)
    ->first();

    return response()->json(array('spotid' => $spotid));
}

While the controller and datepicker functionalities are working as expected, I am facing the challenge of returning the value back to the Javascript and redirecting to the URL: /{vendor_id}/events/{event_id}/{spot_id}.

Answer №1

display the data using console.log( data ); instead of console.log(value.id);

Answer №2

After some trial and error, I finally managed to make it function properly. The key was to extract the value from the object itself. Here's the solution I came up with: $.get("/"+vendor_id+"/events/"+event_id +"/"+caldate, function( data ) {

  console.log(data);
  window.location.href="/"+vendor_id+"/events/"+event_id +"/"+data.spotid.id;

});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Find all Mondays occurring within a specified date range using Moment.js

I need to extract all Mondays within a specific date range. let start = moment(this.absence.FromDate); let end = moment(this.absence.ToDate); The user has the option to deactivate certain weekdays during this period by setting booleans. monday = true; t ...

The disappearing act of the Show/Hide Button in Sencha Touch 2.3.1: what's the

I'm running into an issue with my sencha touch app. Here is the container I have defined: { xtype: 'container', text: 'SOMETHING', height: '15%', width: '15%', ...

What is the functionality of named function expressions?

After coming across an intriguing example in the book labeled as a "named function expression," I was curious to delve into its mechanics. While the authors mentioned it's not commonly seen, I found it fascinating. The process of declaring the functi ...

Does an href and click events both happen simultaneously?

JavaScript Purpose: Implement a series of loops and create anchor links with the 'href' attribute <a class="mui-control-item" v-for="(item, index) in dai" v-on:click ="abc(item)" :href="'#item'+(index+1)+ 'mobile'" ...

When using jQuery's .each method, only the final JavaScript object element is added to the divs

I have a unique set of dynamically-created divs, each containing a Title. When a div is clicked, a modal opens (which is cleared upon click), and the Title is displayed again in the modal. My goal is to add the category descriptions into these modals, but ...

Is it possible to pass the image source to a Vue.js component through a

I am encountering an issue while trying to display an image in the designated location within display.vue. Even though {{ someText }} returns the correct file path (../assets/city.png), the image is not being output correctly. Here is how my code looks: ...

Transform js into a more dynamic format to avoid redundancy when displaying items upon click

I came across a simple lightbox code snippet, but it's based on IDs and I plan to use it for more than 20 items. I don't want to manually write out 20 JavaScript lines when there could be a more efficient way to handle it dynamically. My JS skill ...

Invoke function within bxslider callback

I am attempting to utilize a custom function within a bxslider callback, but unfortunately, the function is not being recognized (specifically: Uncaught Reference Error: nextSlideCustom() is not a function) The current code snippet I have is as follows: ...

What is the reason behind being able to assign unidentified properties to a literal object in TypeScript?

type ExpectedType = Array<{ name: number, gender?: string }> function go1(p: ExpectedType) { } function f() { const a = [{name: 1, age: 2}] go1(a) // no error shown go1([{name: 1, age: 2}]) // error displayed ...

Asynchronous data fetching with React Hook useEffect does not properly populate the tooltip in Material UI component

After using useEffect to fetch data, I encountered a problem in passing the data to my component. Here is some of my code: User Data Types (UPDATED) export interface IUser { display_name: string; id: string; images: Image[]; } expo ...

Retrieve data from a variable that is located within a function that is also

<script> const tally ={ total: 0, increase: function(){ total++; console.log(total); } } const selectBtn = document.getElementsByTagName('button& ...

Enabling clients to access all static files from a Node.js + Express server

My index.js file serves as a node.js server : var express = require('express'); var app = express(); const PORT = process.env.PORT || 5000; var serv = require('http').Server(app); app.get('/', function(req, res) { res.sen ...

Using PHP to create multiple div elements and then concealing them all

I'm having an issue with the hide feature not working. My intention is to hide each dynamically generated div and gain control over them, but the problem seems to lie in the div id incrementing. Any assistance would be greatly appreciated! <?php $ ...

Stopping multiple queries in Node.js can be achieved by using proper error handling

Upon verifying the existence of the name, the program continues to the subsequent query and proceeds with inserting data, which results in an error due to multiple responses being sent. How can I modify it to halt execution after checking if (results.row ...

Attempting to manipulate the selected tab material using a custom directive with two-way data binding

I've been struggling to change the tab using code and can't seem to figure out what's causing the error. It works fine when I use the controller to change the variable, but when trying to bind it through a directive, it breaks. var app = an ...

Issue: Unable to create the restangular module because: the variable '_' is not defined

Upon integrating Restangular into an existing website, I encountered a JavaScript error that stated: Failed to instantiate module restangular due to: '_' is undefined I'm unsure of what this means. Can anyone clarify why '_' is ...

When a user clicks on a button, AJAX and jQuery work together to initiate a setInterval function that continually

Currently, I have two scripts in place. The first script is responsible for fetching a specific set of child nodes from an XML file through AJAX and using them to create a menu displayed as a list of buttons within #loadMe. What's remarkable about thi ...

Is there a way to stop text from wrapping between words and punctuation marks using CSS or jQuery?

There is a paragraph containing some text. One issue I am facing is that punctuation at the end of a word may get wrapped to the next line, as shown here: This is the sentence, This is a new line Is there a way to fix this using CSS or jQuery? ...

A guide to testing redux API using Node.js

I'm diving into the world of nodejs and redux as a beginner. I managed to install node v7.10 on my computer. While reading through the Redux documentation, it mentioned that I should be able to test the action, reducer, and store API without a user in ...

I'm having trouble understanding why my Javascript validation suddenly stopped functioning. Can anyone assist me in troubleshooting this issue?

I have been working on this webpage for a school project for a few days, and it was running smoothly until about 10 minutes ago. The only change I made was adding an extra JavaScript validation. Now, when I try to register by clicking the "register" butt ...