nick
nick

Reputation: 3

passing arrays from html form to php file by ajax

Hi I'm very new to ajax/jquery and I have a problem. I'm sending data throug a form to php code via AJAX but some of the hidden input fields are arrays. How can I*strong text* create a function that catch data from post and save in vars to give to php script? I followed and modified this examples:

//Funzione per la gestione asincrona AJAX
function xmlhttpPost(strURL) {
//Inizializzo l'oggetto xmlHttpReq
var xmlHttpReq = false;
var self = this;
// qui valutiamo la tipologia di browser utilizzato per selezionare la tipologia di oggetto da creare.
// Se sono in un browser Mozilla/Safari, utilizzo l'oggetto XMLHttpRequest per lo scambio di dati tra browser e server.
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// Se sono in un Browser di Microsoft (IE), utilizzo Microsoft.XMLHTTP
//che rappresenta la classe di riferimento per questo browser
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
//Apro il canale di connessione per regolare il tipo di richiesta.
//Passo come parametri il tipo di richiesta, url e se è o meno un operazione asincrona (isAsync)
self.xmlHttpReq.open('POST', strURL, true);

//setto l'header dell'oggetto
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

/* Passo alla richiesta i valori del form in modo da generare l'output desiderato*/
self.xmlHttpReq.send(recuperaValore());

/* Valuto lo stato della richiesta */
self.xmlHttpReq.onreadystatechange = function() {

/*Gli stai di una richiesta possono essere 5
* 0 - UNINITIALIZED
* 1 - LOADING
* 2 - LOADED
* 3 - INTERACTIVE
* 4 - COMPLETE*/

//Se lo stato è completo
if (self.xmlHttpReq.readyState == 4) {
/* Aggiorno la pagina con la risposta ritornata dalla precendete richiesta dal web server.Quando la richiesta è terminata il responso della richiesta è disponibie come responseText.*/
aggiornaPagina(self.xmlHttpReq.responseText);
}
}

}

// HERE IS THE PROBLEM. I TRIED TO INSERT MANY VAR THAT COMES FROM FORM:  id_p_vis (ARRAY), id_p_home (ARRAY), id, id_sub, data_gg, n_gg and I need to give to a php file where I will use by $_POST['id'], .....

/*Questa funzione recupera i dati dal form.*/
function recuperaValore() {
var form = document.forms['form'];
var id_p_vis = form.getElementById["idpvis"];
var id_p_home = form.getElementById["idphome"]; 
var id = form.id.value;
var id_sub = form.id_sub.value;
var data_gg = form.data_gg.sub.value;
var n_gg = form.n_gg.value; 

valore = 'id=' + escape(id);


return valore;
}
/*Questa funzione viene richiamata dall'oggetto xmlHttpReq per l'aggiornamento asincrono dell'elemento risultato*/
function aggiornaPagina(stringa){
document.getElementById("span12").innerHTML = stringa;

Could you help me to solve the problem?

Thank you bye

nick }

Upvotes: 0

Views: 888

Answers (2)

jeroen
jeroen

Reputation: 91792

If you are using jQuery you should definitely switch to jQuery's ajax functions.

You can also get all fields from the form in one go:

$('form').submit(function() {
  // serialize all form fields
  var data = $(this).serialize();

  // use jQuery ajax
  $.ajax({
      type: 'POST',
      url: strURL,
      data: data
    })
    .done(function(returned_data) {
      console.log(returned_data);
    });
});

Upvotes: 1

Adriano Carneiro
Adriano Carneiro

Reputation: 58645

You might want to take a look at jQuery's serialize() and serializeArray().

Upvotes: 1

Related Questions