Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    [AJAX] problema inafferrabile dopo invio tramite post

    Ciao a tutti,
    dato che è da un po' che su un problema veramente odioso, mi sono arreso e ho voluto chiedere consulto a qualcun altro con la speranza che riesca a trovare l'inghippo...

    in poche parole: io tramite questo form
    codice:
    numero di ore: <input type="text" size="3" maxlength="2" name="quantita" id="quantita"> [img]images/add.png[/img]
    quando clicco sull'immagine mi dovrebbe richiamare questa funzione in JS

    codice:
    function submitquantita()
    {
    	var status = document.getElementById('form');
    	var quantita_form = document.getElementById('quantita');		
    	var http = new createXMLHttpRequest();
    	http.open("post","/admin/server.php?function=submitQuantita&<?=$sn?>=<?=$s?>",true);
    	http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    	http.send("quantita="+quantita_form.value);
    	if (http.readyState != 4)
    		document.getElementById('caricamento').innerHTML = "[img]/images/loading.gif[/img]";
    	http.onreadystatechange = function() 
    	{
    		var risultato = http.responseText;
    		aggiungiriga(risultato);
    		document.getElementById('caricamento').innerHTML = '';
    	}
    }
    che, come si può ben capire, invia il dato inserito nel campo del form e riceve (deve ricevere) un messaggio indietro da server.php...dopo dovrebbe chiamare aggiungiriga() per aggiungere la riga con i dati appena inseriti in una tabella...

    codice:
    function aggiungiriga(risultato)
    {
    	alert(risultato);
    	var parametri = risultato.split("|"); // 0 => data, 1 => quantita
    	var quantita = parametri[1];
    	var data = parametri[0];
    	var oTr=document.createElement("TR");
    	var oTd1=document.createElement("TD");
    	oTd1.setAttribute("CLASS","font")
    	var oTd2=document.createElement("TD");
    	oTd2.setAttribute("CLASS","font")
    
    	var oText1=document.createTextNode(data);
    	var oText2=document.createTextNode(quantita);
    
    	// appendi al relativo padre
    
    	oTd1.appendChild(oText1);
    	oTd2.appendChild(oText2);
    
    	oTr.appendChild(oTd1);
    	oTr.appendChild(oTd2);
    	var riga_totale = document.getElementById('riga-totale');
    	document.getElementById('contore').getElementsByTagName('TBODY')[0].insertBefore(oTr,riga_totale);
    }
    l'alert che ho messo è a scopo di debug...infatti mi ero accorto che stranamente la funzione veniva "attivata" 3 volte:
    1 volta non riceve nulla
    1 altra volta riceve il dato giusto
    1 altra volta riceve il dato di prima

    ovviamente il 1° e il 3° caso non dovrebbero accadere, ma tant'è...infatti quando stampo il valore in ingresso con alert(risultato); mi vengono fuori 3 pop-up che visualizzano appunto il valore della variabile...

    cosa sbaglio quindi?

  2. #2
    non l'ho provata ma dovrebbe fungere...
    chiami la funzione in tutti gli stati tranne in quello che ti interessa....
    codice:
    function submitquantita()
    {
    	var status = document.getElementById('form');
    	var quantita_form = document.getElementById('quantita');		
    	var http = new createXMLHttpRequest();
    	http.open("post","/admin/server.php?function=submitQuantita&<?=$sn?>=<?=$s?>",true);
    	http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    	xmlhttp.onreadystatechange=function() {	
    		if (xmlhttp.readyState == 1) {
    			document.getElementById('caricamento').innerHTML = "[img]/images/loading.gif[/img]";
    		}
    		if (xmlhttp.readyState == 4) { 
    			var risultato = http.responseText;
    			aggiungiriga(risultato);
    			document.getElementById('caricamento').innerHTML = '';
    		 }	
    	}
    	http.send("quantita="+quantita_form.value);
    }
    fammi sapere ciao
    There are 10 types of people in the world: Those who understand binary, and those who don't.

  3. #3
    esatto, mo funziona thanx

  4. #4
    codice:
    function submitquantita()
    {
    	var status = document.getElementById('form');
    	var quantita_form = document.getElementById('quantita');		
    	var http = new createXMLHttpRequest();
    	http.open("post","/admin/server.php?function=submitQuantita&<?=$sn?>=<?=$s?>",true);
    	http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    	xmlhttp.onreadystatechange=function() {	
    		if (xmlhttp.readyState == 1) {
    			document.getElementById('caricamento').innerHTML = "[img]/images/loading.gif[/img]";
    		}
    		if (xmlhttp.readyState == 4) { 
    			if (req.status == 200) {
    				var risultato = http.responseText;
    				aggiungiriga(risultato);
    				document.getElementById('caricamento').innerHTML = '';
    			}
    			else {
    				alert ( "Errore: Non è stato possibile ricevere una risposta da server" );
    				document.getElementById('caricamento').innerHTML = "Errore :"+request.status ;
    			}
    		 }	
    	}
    	http.send("quantita="+quantita_form.value);
    }
    volendo
    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 © 2024 vBulletin Solutions, Inc. All rights reserved.