Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

149
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda