0

I'm sending data through a form to PHP code via AJAX but some of the hidden input fields are arrays.

How can I 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;
}
1

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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);
    });
});

3 Comments

and how can I insert in my ajax.js file that method? Sorry for my dummy question but I'm a bit confused
I tried to use it inside my ajax.js file, but I got an error: ReferenceError: $ is not defined . i put in a $(document).ready(function(){ )}; function.
now I imported jquery library and I have no error, but it seems not working. I see all form datas on the link bar after ? like risultati.php?val=1&rss=2%...... i think somethings wrong

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.