Visualizzazione dei risultati da 1 a 5 su 5

Discussione: [AJAX] form & array

  1. #1

    [AJAX] form & array

    codice:
    function refreshPreview(inputField,outputField,httpUrl) 
    {
    	var myText = inputField+"="+escape(document.getElementById(inputField).value);
    	
    	if(window.XMLHttpRequest) {
    		xmlhttp = new XMLHttpRequest();
    	} else if(window.ActiveXObject) {
    		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    	} else {
    		return false;
    	}
    	xmlhttp.open("POST", httpUrl, true);
    	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    	xmlhttp.setRequestHeader("Content-length",myText.length);
    	xmlhttp.setRequestHeader("Connection","close");
    	xmlhttp.onreadystatechange=function() {
    		if (xmlhttp.readyState == 4) { 
    
    			document.getElementById(outputField).innerHTML = xmlhttp.responseText;
    		}
    	}
    	
    	xmlhttp.send(myText);
    }
    utilizzo questa funzione per mandare ad uno script php il valore di una texarea di un form.

    inputField: 'nome_id_della_texarea'
    outputField: 'nome_id_del_div_che_visualizzerà_output'
    httpUrl: 'url_script'

    se volessi passare tutto il form (tutti i suoi campi)
    come potrei fare?

    avevo pensato ad un array.... ma non saprei gestirlo nella funzione

    help
    There are 10 types of people in the world: Those who understand binary, and those who don't.

  2. #2
    Frontend samurai L'avatar di fcaldera
    Registrato dal
    Feb 2003
    Messaggi
    12,924
    il post di un array è abbastanza semplice, basta concatenare una stringa del tipo

    var myText = "inputField[]=dato1&inputField[]=dato2...";

    ovviamente lato server devi leggere i dati aspettando di ricevere un array

    Vuoi aiutare la riforestazione responsabile?

    Iscriviti a Ecologi e inizia a rimuovere la tua impronta ecologica (30 alberi extra usando il referral)

  3. #3
    Originariamente inviato da fcaldera
    il post di un array è abbastanza semplice, basta concatenare una stringa del tipo

    var myText = "inputField[]=dato1&inputField[]=dato2...";

    ovviamente lato server devi leggere i dati aspettando di ricevere un array

    nella pagina della form richiamo la funzione così:
    codice:
    onclick="refreshPreview('nome_capo','preview','preview.php');return false;"
    ovviamente al posto di 'nome_campo' dovrei passargli un array
    dove ogni elemento è il nome del campo della form giusto?
    quindi:
    codice:
    onclick="refreshPreview(nome_form = new array('campo1','campo2'),'preview','preview.php');return false;"
    e poi nella funzione refreshPreview come gestisco l'array? per associare ad ogni campo il rispettivo valore?

    :master:
    There are 10 types of people in the world: Those who understand binary, and those who don't.

  4. #4
    Originariamente inviato da fcaldera
    il post di un array è abbastanza semplice, basta concatenare una stringa del tipo

    var myText = "inputField[]=dato1&inputField[]=dato2...";

    ovviamente lato server devi leggere i dati aspettando di ricevere un array

    potresti spiegarmelo meglio?
    tnx
    There are 10 types of people in the world: Those who understand binary, and those who don't.

  5. #5
    ok ho risolto, ora funge!

    inputField=nome_del_form
    codice:
    function refreshPreview(inputField,outputField,httpUrl) 
    {
    	var myText = ""; 
    	for (i=0;i<document.getElementById(inputField).length;i++)
     	  {
    		 myText = myText+document.getElementById(inputField).elements[i].name+"="+escape(document.getElementById(inputField).elements[i].value);
    	  	 if (i<document.getElementById(inputField).length) {myText = myText+"&";}
    	  }
    	
    	if(window.XMLHttpRequest) {
    		xmlhttp = new XMLHttpRequest();
    	} else if(window.ActiveXObject) {
    		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    	} else {
    		return false;
    	}
    	xmlhttp.open("POST", httpUrl, true);
    	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    	xmlhttp.setRequestHeader("Content-length",myText.length);
    	xmlhttp.setRequestHeader("Connection","close");
    	xmlhttp.onreadystatechange=function() {
    		if (xmlhttp.readyState == 4) { 
    
    			document.getElementById(outputField).innerHTML = xmlhttp.responseText;
    		}
    	}
    	
    	xmlhttp.send(myText);
    }
    There are 10 types of people in the world: Those who understand binary, and those who don't.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.