FORM HTTP POST in PHP utilizzando AJAX
Questo esempio mostra HTTP POST FORM forma completa HTML al server e visualizzare la risposta utilizzando AJAX.
Pagina HTML
"text/javascript" > var time_variable ; function getXMLObject ( ) //XML OBJECT { var xmlHttp = false ; try { xmlHttp = new ActiveXObject ( "Msxml2.XMLHTTP" ) // For Old Microsoft Browsers } catch ( e ) { try { xmlHttp = new ActiveXObject ( "Microsoft.XMLHTTP" ) // For Microsoft IE 6.0+ } catch ( e2 ) { xmlHttp = false // No Browser accepts the XMLHTTP Object then false } } if ( ! xmlHttp && typeof XMLHttpRequest != 'undefined' ) { xmlHttp = new XMLHttpRequest ( ) ; //For Mozilla, Opera Browsers } return xmlHttp ; // Mandatory Statement returning the ajax object created } var xmlhttp = new getXMLObject ( ) ; //xmlhttp holds the ajax object function ajaxFunction ( ) { var getdate = new Date ( ) ; //Used to prevent caching during ajax call if ( xmlhttp ) { var txtname = document. getElementById ( "txtname" ) ; xmlhttp. open ( "POST" , "testing.php" , true ) ; //calling testing.php using POST method xmlhttp. onreadystatechange = handleServerResponse ; xmlhttp. setRequestHeader ( 'Content-Type' , 'application/x-www-form-urlencoded' ) ; xmlhttp. send ( "txtname=" + txtname. value ) ; //Posting txtname to PHP File } } function handleServerResponse ( ) { if ( xmlhttp. readyState == 4 ) { if ( xmlhttp. status == 200 ) { document. getElementById ( "message" ) . innerHTML = xmlhttp. responseText ; //Update the HTML Form element } else { alert ( "Error during AJAX call. Please try again" ) ; } } } </ script > <body> <form name="myForm"> <table> <tr> <td>Enter Name</td> <td><input type="text" name="txtname" id="txtname" /></td> </tr> <tr> <td colspan="2"><input type="button" value="Submit" onclick="ajaxFunction();" /></td> </tr> </table> <div id="message" name="message"></div> </form> </body> </head> </html> <html> <title> PHP utilizzando AJAX </ title> tipo di script <= "text / javascript" var time_variable>; getXMLObject function () / / oggetto XML (var XMLHTTP = false; try (XMLHTTP = new ActiveXObject ( "Msxml2.XMLHTTP") / / Per browser Microsoft Old) catch (e) (try (XMLHTTP nuovo ActiveXObject = ("Microsoft.XMLHTTP") / / Per Microsoft IE 6.0 +) catch e2) ((XMLHTTP = false / / No browser accetta l'oggetto XMLHTTP poi false)) if (! XMLHTTP & & typeof XMLHttpRequest! = 'undefined') (XMLHTTP = new XMLHttpRequest (); / / Per Mozilla, Opera Browser) XMLHTTP ritorno; / / Obbligatoria Dichiarazione resa dei ajax oggetto creato XMLHTTP var =) getXMLObject nuovo (); / / XMLHTTP detiene il Ajax ajaxFunction funzione dell'oggetto () (var getdate = new Date (); / / Usato per prevenire la cache durante la chiamata AJAX if (XMLHTTP) (var = txtName documento. getElementById ( "txtName"); XMLHTTP. open ("POST", "testing.php", true); / / testing.php chiama con XMLHTTP metodo POST. onreadystatechange = handleServerResponse; XMLHTTP. setRequestHeader ('Content-Type' applicazione, '/ x-www-form-urlencoded '); XMLHTTP. inviare ("txtName =" txtName +. value); / / Distacco txtName a PHP file)) handleServerResponse function () (if (xmlhttp. readyState == 4) (if ( XMLHTTP. status == 200) (document. getElementById ("messaggio"). innerHTML = XMLHTTP. responseText; / / Aggiorna l'elemento HTML form) else (alert ("Errore durante la chiamata AJAX. Riprova");))) </ script> <body> <form name="myForm"> <table> <tr> <td> Inserisci Nome </ td> <td> tipo di ingresso <= "nome di testo" = "txtName" id = "txtName" /> </ td> </ div> <td colspan="2"> <input type="button" value="Submit" onclick="ajaxFunction();" /> </ td> </ tr > </ table> <div id="message" name="message"> </ div> </ form> </ <body> </ head> </ html>
Codice PHP
<? $_POST [ 'txtname' ] ; $ A = $ _POST ['txtName']; . $a ; echo "Good Morning". $ a; ?>
Spiegazione per il codice AJAX
Qui i tre hanno dichiarato funzione JavaScript:
getXMLObject () - Responsabile per la creazione dell'oggetto AJAX a seconda del browser
ajaxFunction () - Responsabile per la chiamata pagina PHP tramite chiamata AJAX
handleServerResponse () - Responsabile per la visualizzazione dei dati recuperati dal server
Come funziona il codice AJAX Works:
- Quando la pagina viene caricata sto creando un oggetto AJAX chiamando il getXMLObject () e restituire l'oggetto creato nella variabile XMLHTTP
- Quando l'utente fa clic sul pulsante di input, ajaxFunction () viene chiamato per controlli che se l'oggetto Object Ajax è creata o non e in funzione che chiama lo script remoto. Ecco una handleServerResponse funzione di gestione è definita anche per recuperare il valore dal server
- Quando il ReadyState della chiamata AJAX raggiunge i 4 e lo stato di HTTP è di 200 si passa ai dati recuperati dal server nella casella di testo
Spiegazione per il codice PHP
- Chiedo $ _POST che recuperi il contenuto post inviati attraverso chiamata Ajax
- Infine chiedo eco per inviare la risposta torna alla pagina chiamante

Custom Search
Articoli popolari:
- Aumento limite di memoria di script PHP
- Tutorial PHP5 - __sleep () metodo magico
- Tutorial PHP5 - __clone metodo magico
- OOPS in PHP 5 Tutorial - Definizione di Costanti di classe
- Modulo di richiesta HTTP POST utilizzando AJAX e Servlet
- Programmazione Ajax
- Errori di programmazione AJAX comune
- Lettura di documenti Excel da applicazioni PHP
- OOPS in PHP 5 - operatore instanceof
- Genera SessionID Unico in PHP



































Good site I "Stumbledupon" oggi e ha dato un inciampo per voi .. ansioso di vedere cos'altro avete .. più tardi
Hai fatto un buon lavoro
Nice post, Grazie.
Ottimo tutorial pal .... Stavo cercando questo per lungo tempo, e finalmente trovato uno!
Grazie
Grazie tanto. Funziona perfetto!
ciao dove è
Che dire se si usa più di un ingresso?
Ciao,
Sì, è possibile fare chiamate AJAX con più di un input.
Dire se si dispone di due TextBox (TextBox1 e TextBox2) nel codice HTML che deve essere passato attraverso AJAX rispetto, inviare metodo avrà i seguenti dati:
xmlhttp.send ("txtName =" + textbox1.value + "& txtname1 =" + textbox2.value);
Grazie,
Hitesh Agarwal
1000000 volte grazie ... http://www.ghyoom.com/
delicato uno
u thnk molto Budy ... ... ... ... ... ....
dolce e pulita esempio ... buon lavoro!
Che dire se si usa più di una forma?
In questo caso si dovranno acquisire i dati delle forme utilizzando JavaScript e inviare i dati al metodo individuale chiamata AJAX.
Ottimo tutorial. Volevo solo dire grazie per il tempo di scriverlo!