Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda