Salve a tutti vi incollo il codice e poi vi spiego il problema.
Pagina form.php
Pagina form.jscodice:<div id='boxForm'> <form method='post' id='fmIndex' onsubmit='return handleServerResponse()' > Nome <input type='text' name='txtNome' id='txtNome' /><br /> Cognome <input type='text' name='txtCognome' id='txtCognome' /><br /> Email <input type='text' name='txtEmail' id='txtEmail' /><br /> <input type='submit' value='Invia' /> </form> <div>
Pagina data.phpcodice:var xmlhttp = createXmlHttpRequestObject(); var I_UNINITIALIZED = 0; var I_LOADING = 1; var I_LOADED = 2; var I_INTERACTIVE = 3; var I_COMPLETE = 4; // ### RESTITUISCE L'OGGETTO XMLHttpRequest function createXmlHttpRequestObject(){ var xmlHttp; // ### SE SI USA INTERNET EXPLORER if (window.ActiveXObject){ try{ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }catch (e){ xmlHttp = false; } // ### SE SI USANO ALTRI BROWSER }else{ try{ xmlHttp = new XMLHttpRequest(); }catch (e){ xmlHttp = false; } } if (!xmlHttp){ alert("Errore durante la creazione dell'oggetto XMLHttpRequest"); }else{ return xmlHttp; } } function handleServerResponse(){ var strNome = document.getElementById("txtNome").value; var strCognome = document.getElementById("txtCognome").value; var strEmail = document.getElementById("txtEmail").value; var strData = ""; var strData = "txtNome=" + strNome + "&txtCognome=" + strCognome + "&txtEmail=" + strEmail; xmlhttp.open("POST", "data.php", true); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState==4){ if (xmlhttp.status==200) { alert(xmlhttp.readyState + "\n" + xmlhttp.status + "\n" + xmlhttp.responseText); var boxId=document.getElementById("boxForm"); boxId.innerHTML=xmlhttp.responseText; } } } //Send the proper header information along with the request xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.setRequestHeader("Content-length", strData.length); xmlhttp.setRequestHeader("Connection", "close"); xmlhttp.send(strData); }
Questo codice non funziona. All'invio del form la pagina resta uguale a prima con i campi svuotati.codice:<?php echo "ecco i dati"; print_r($_POST); ?>
Se invece metto un alert() nella funzione interna (quella assegnata a xmlhttp.onreadystatechange) compare una finestra alert() e poi al posto del form viene stampato il risultato della pagina php. Quale è il problema? Grazie a chi mi risponderà


Rispondi quotando

usi il codice che t'ho consigliato sostituendo la chiamata da GET a POST (vedi codice precedente)
