Home > AJAX , PHP > HTTP POST FORM in PHP utilizzando AJAX

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;


 ?> 

Il tuo indirizzo email:

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:

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • IndianPad
  • LinkedIn
  • Live
  • MySpace
  • Netvibes
  • RSS
  • Technorati
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • Reddit
  • Add to favorites
  • PDF
  • Twitter
Tags: , , Categorie: AJAX , PHP Tag: , ,
  1. 5 Aprile, 2008 alle 06:07 | # 1

    Good site I "Stumbledupon" oggi e ha dato un inciampo per voi .. ansioso di vedere cos'altro avete .. più tardi

  2. Ravi
    22 novembre 2008 alle 02:16 | # 2

    Hai fatto un buon lavoro

  3. Mehmet
    8 Maggio 2009 alle 04:13 | # 3

    Nice post, Grazie.

  4. 22 maggio 2009 a 12:34 | # 4

    Ottimo tutorial pal .... Stavo cercando questo per lungo tempo, e finalmente trovato uno!
    Grazie

  5. Yigit
    30 maggio 2009 alle 00:49 | # 5

    Grazie tanto. Funziona perfetto!

  6. 23 giugno 2009 alle 05:05 | # 6

    ciao dove è

  7. che dire?
    16 Agosto 2009 alle 20:50 | # 7

    Che dire se si usa più di un ingresso?

    • 18 agosto 2009 alle 02:45 | # 8

      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

  8. 22 Ottobre 2009 a 21:46 | # 9

    1000000 volte grazie ... http://www.ghyoom.com/

  9. 6 Dicembre 2009 a 10:29 | # 10

    delicato uno

  10. 8 Febbraio 2010 alle 07:04 | # 11

    u thnk molto Budy ... ... ... ... ... ....

  11. 17 febbraio 2010 alle 08:53 | # 12

    dolce e pulita esempio ... buon lavoro!

  12. 3 Aprile, 2010 a 18:52 | # 13

    Che dire se si usa più di una forma?

    • 8 aprile 2010 a 11:17 | # 14

      In questo caso si dovranno acquisire i dati delle forme utilizzando JavaScript e inviare i dati al metodo individuale chiamata AJAX.

  13. 3 giugno 2010 alle 22:18 | # 15

    Ottimo tutorial. Volevo solo dire grazie per il tempo di scriverlo! :)

  1. Nessun trackback ancora.