Sto cercando di fare l'inserimento di un campo nel db con ajax..però non mi funziona..vi posto il codice..
ajax.js
Questo è il form:codice:function assegnaXMLHttpRequest() { var XHR = null, browserUtente = navigator.userAgent.toUpperCase(); if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object") XHR = new XMLHttpRequest(); else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0) { if(browserUtente.indexOf("MSIE 5") < 0) XHR = new ActiveXObject("Msxml2.XMLHTTP"); else XHR = new ActiveXObject("Microsoft.XMLHTTP"); } return XHR; }; // funzione per prendere un elemento con id univoco function prendiElementoDaId(id_elemento) { var elemento; if(document.getElementById) elemento = document.getElementById(id_elemento); else elemento = document.all[id_elemento]; return elemento; }; function inserisci_marca(){ var parametri = "nome=" + escape(document.forms['ins_marca'].nome.value); ajax = assegnaXMLHttpRequest(); //Creo l'oggetto xmlhttp elemento = prendiElementoDaId('msg'); //Div nel quale inserirò la lista if(ajax){ ajax.open("POST", 'ins_marca.php', true); ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); ajax.setRequestHeader('Content-length',parametri.length); ajax.setRequestHeader('Connection', 'close'); if(ajax.readyState === readyState.COMPLETATO) { // verifica della risposta da parte del server if(statusText[ajax.status] === "OK") // operazione avvenuta con successo elemento.innerHTML = ajax.responseText; else { // errore di caricamento elemento.innerHTML = "Impossibile effettuare l'operazione richiesta. "; elemento.innerHTML += "Errore riscontrato: " + statusText[ajax.status]; } } ajax.send(parametri); } return false; }
il div id=messaggio conterrà il messaggio di buon fine..codice:<form id="ins_marca"> <table border="0" cellspacing="0" cellpadding="0" width="300"> <tr> <td width="300">Nome Marca</td> </tr> <tr> <td width="300"><input type="text" name="nome" size="25" style="width: 170; border: 1px solid #afb4b8; font-size:10px; font-family: Tahoma,Arial,sans-serif; color: black; font-weight: normal; margin-bottom:5px;" /></td> </tr> <tr> <td width="300"> <input type="button" name="invia" value="inserisci" onClick="javascript:inserisci_marca(); return false;" /> </td> </tr> </table> </form> <div id="messaggino"> </div>
per prova la pagina ins_marca.php ha solo la stampa del parametro passato tramite post
non c'è un errore vero e proprio.. il div non viene aggiornato...Codice PHP:<?
echo($_POST['nome']);
?>
credo non entri in questo if
if(statusText[ajax.status] === "OK")
// operazione avvenuta con successo
elemento.innerHTML = ajax.responseText;
else {
help please

Rispondi quotando