Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
How can I create variable that is array of values of variables?

I want to store values of name, selectedDay and selectedMovie into reservationData. Then, I want reservationData to pass in ajax as a data so I can get it in my Controller, read from them and insert them into my database

This is my code:

var reservationData = {"name": name, "selectedMovie": selectedMovie, "selectedDay": selectedDay};
console.log(reservationData);

var confirmReservation = $('.confirm-reservation');

confirmReservation.on('click', function(){
  
  $.ajax({
    url: '/drupal/movie-reservation',
    type: 'GET',
    cache: false,
    data:{result: JSON.stringify(reservationData)},
    success: function(data){
      alert(data);
    }
  });

Everytime my name, selectedMovie and selectedDay gain their value I want that value to be stored in reservationData as well. How can I do that? Thanks in advance!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Your current reservationData variable is an object, not an array. You will keep updating/overwriting the same properties. So you will need an array to push these reservationData onto.

about 4 years ago · Juan Pablo Isaza Report

0

You can achieve that by defining arrReservationData as an array containing objects.

Each click on .confirm-reservation will send a new objNewReservationData object via ajax to your controller. The returned result object will be pushed to arrReservationData.

var arrReservationData = [],
var objNewReservationData = {
  "name": name, 
  "selectedMovie": selectedMovie, 
  "selectedDay": selectedDay
},
confirmReservation = $('.confirm-reservation');

confirmReservation.on('click', function(){  
  $.ajax({
    url: '/drupal/movie-reservation',
    type: 'GET',
    cache: false,
    data: {result: JSON.stringify(objNewReservationData)},
    success: function(data){
      let objResponse = {
        "name": data.name, 
        "selectedMovie": data.selectedMovie, 
        "selectedDay": data.selectedDay
      };
      arrReservationData.push(objResponse);
      // do stuff with arrReservationData, like storing in a database
    }
  });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!