Ajax in tilt

joestorm212

Nuovo Utente
23 Mar 2010
6
0
0
Salve a tutti ho un problemino con ajax e php, l'obbiettivo del mio esercizio erea di catturare dei dati attraverso un form html e indirizzarli a uno script php attraverso javascript avanzato(ajax). Lo script php elabora i dati acquisiti, li converte in xml e inoltre gestisce gli errori vi posto qui di seguito gli script vedete un pò voi dove sbaglio xkè io nn sò + dove vedere :dipser::dipser::dipser::

1) il file html che si chiama morephp.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>Ajax : il passaggio di parametri e la gestione di errori</title>
<script type="text/javascript" src="morephp.js"></script>
</head>
<body>
Chiediamo al server di dividere
<input type="text" id="firstNumber" />
per
<input type="text" id="secondNumber" />
<input type="button" value="Send" onclick="process()" />
<div id="myDivElement"/>
</body>
</html>

2) il file in js morephp.js

//l'istanza XMLHttpRequest
//creo l'istanza dell'oggetto XMLHttpRequest
var xmlHttp = createXMLHttpRequestObject();

function createXMLHttpRequestObject()
{
//memorizza il riferimento dell'oggetto XMLHttpRequest
var xmlHttp;
//x tutti i browser eccetto IE6
try
{
xmlHttp = new XMLHttpRequest();
}
catch( e )
{
var xmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
"MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0",
"MSXML2.XMLHTTP","Microsoft.XMLHTTP");
//adesso provo tutti gli elementi dell'array finchè nn ne trovo quello giusto
for (var i=0; i < xmlHttpVersions.length && !xmlHttp; i ++)
{
try
{
xmlHttp = new ActiveXObject(xmlHttpVersions);
}
catch( e ) {}

}
}
if( !xmlHttp )
alert("Err!!!!! during the creation XMLHttpRequestObject");
else
return xmlHttp;
}
function process()
{
if( xmlHttp )
{
try
{
var firstNumber = document.getElementById("firstNumber").value;
var secondNumber = document.getElementById("secondNumber").value;
var params = "firstNumber="+firstNumber + "&secondNumber="+secondNumber;

//iniziamo la richiesta http asincrona
xmlHttp.open('GET', 'morephp.php?',+ params , true );
xmlHttp.onreadystatechange = handleRequestStateChange;
xmlHttp.send( null );
}
catch( e )
{
alert("Err!!!! collegamento al server:\n" +e.toString());
}
}
}

function handleRequestStateChange()
{
//quando readystate è in posizione 4 siamo pronti per leggere la risposta
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)//TEST
{
try
{
handleServerResponse();

}
catch( e )
{
alert("errore durante la lettura della risposta:"+ e.toString());
}
}
else
{
alert('Si sta verificando un errore sul server nella ricezione dei dati:\n'+ xmlHttp.StatusText );
}
}
}
//gestione risposta ricevuta dal server
function handleServerResponse()
{
//recupera la risposta del server sotto forma di oggetto XML DOM
var xmlResponse = xmlHttp.responseXML;
// catturiamo eventuali errori con IE e Opera
if (!xmlResponse || !xmlResponse.documentElement )
throw("Struttura xml non valida:\n" + xmlHttp.responseText );

//catturiamo eventuali errori con ff
var rootNodeName = xmlResponse.documentElement.nodeName;
if (rootNodeName == "parsererror")
throw("Struttura xml non valida:\n" + xmlHttp.responseText );
xmlRoot = xmlResponse.documentElement;
if (rootNodeName != "response"|| !xmlRoot.firstChild )
throw("Struttura xml non valida:\n" + xmlHttp.responseText );
//IL VALORE CHE DOBBIAMO MOSTRARE È IL FIGLIO DELL'ELEMENTO ROOT DI RESPONSR
responseText = xmlRoot.firstChild.data;
//mostriamo il messaggio all'utente
myDiv = document.getElementById("myDivElement");
myDiv.innetHTML = "il server dice che la risposta è:" + reponseText;
}

3) il file in php morephp.php
<?php

//carichiamo il modulo per la gestione degli errori
require_once('error_handler.php');

//inviamo l'output come xml
header ('Content-Type: text/xml');

//calcoliamo il risultato
$firstNumber = $_GET['firstNumber'];
$secondNumber = $_GET['secondNumber'];
$result = $firstNumber / $secondNumber;

//creiamo il nuovo documento XML
$dom = new DOMDocument();

//creiamo l'elemento radice response
$response = $dom -> createElement('response');
$dom -> appendChild($response);

// aggiungiamo l'elemento calcolato al response
$responseText = $dom ->createTextNode($result);
$response -> appendChild($responseText);

//costruiamo la struttura XML in una variabile stringa

$xmlString = $dom -> saveXML(); //N.B.
//restituiamo la variabile stringa
echo $xmlString;
?>
4) il file di gestione errori in php error_handler.php

<?php
//impostiamo error_handler come metodo per la gestione degli errori
set_error_handler('error_handler', E_ALL);

//funzione per la gestione degli errori
function error_handler($errNo, $errStr, $errFile, $errLine)
{
//cancelliamo qualunque output che è stato generato
if(ob_get_length()) od_clean();
//restituiamo il messaggio di errore
$error_message = 'ERRNO:' . $errNo.chr(10). 'TEXT:'. $errStr.chr(10). 'LOCATION:' . $errFile . ', line' . $errLine;
echo $error_message;
//evitiamo che venga processato il resto dello script PHP
exit;
}
?>

Vi chiedo cortesemente di aiutarmi io nn so + ke fare!!!!!! Grazie
 
Discussioni simili
Autore Titolo Forum Risposte Data
I salvare con ajax user_id name msg PHP 0
R valore value di un id da passare in chiamata ajax Ajax 3
R jquery che cambia css di un elemento non mi funziona sulla pagina caricata da ajax Ajax 5
M Fullcalendar in Codeigniter, un aiuto per la chiamata $ajax ? jQuery 0
R Aggiornare record mysql con Ajax, jQuery e php Ajax 2
P Funzione jQuery Ajax invio file a php jQuery 1
E Php select option e ajax PHP 23
Emix Select concatenate php sql ajax PHP 2
MarcoGrazia Valori di ritorno json via ajax non visti. jQuery 1
felino ASP.net MVC: Exception e chiamata AJAX ASP.NET 1
motleyrulez Ricerca filtro con Ajax PHP 1
max1974 Grafico Ajax Javascript 4
max1974 Struttura $.ajax Ajax 7
C la chiamata ajax non ritorna alcun dato Ajax 1
max1974 Lettura Risultato $.ajax Javascript 1
motleyrulez Chiamata ajax per tabella php PHP 3
max1974 [Javascript] Grafico chartjs con dati da J.ajax Javascript 3
O [PHP] inviare dati da form e script ajax PHP 0
F limit show datatable ajax Ajax 1
Domenico_Falco1 Rendere dinamico un sito web con chiamate ajax e php e variabili json PHP 12
G Eseguire codice solo al termine della chiamata ajax Ajax 1
L Problema jQuery validation AJAX (PHP 7) PHP 6
max1974 [Javascript] Grafico ajax non funziona Javascript 0
WorldWideWeb Ajax POST con risposta JSON Ajax 2
M leggere con jquery/ajax in una function javascript record di database sql server Javascript 0
A [Javascript] Ajax, Jquery e PHP Javascript 1
D [Javascript] pulsanti per comandi shell con php e ajax Ajax 7
X Problema con jquery e ajax jQuery 2
B [PHP] variabili globali in chiamate ajax PHP 0
B DEBUG - PHP+JS+AJAX PHP 10
M Inserimento dati checkbox multipli in db da ajax a php PHP 1
Axis18 Creare una barra di avanzamento con $.ajax Ajax 7
otto9due Chiamata ajax su due url è possibile? Ajax 0
G [Javascript] Problema parametro passato con ajax Javascript 4
G Chiamata ajax restituisce errore random Ajax 1
paloppa [PHP] paginazione con ajax PHP 1
filomeni Ajax e https Ajax 4
bubino8 Ajax con risultato si/no Ajax 16
A redirect da pagina php chiamata da ajax PHP 2
L Aggiungere contenuto con ajax durante lo scroll jQuery 1
M Ajax funziona in alcuni siti, in altri no! Ajax 2
otto9due Risposta ajax -> json con php Ajax 3
bubino8 [PHP] split con ajax non funziona PHP 7
C Problema chiamata Ajax Ajax 2
A Visualizzare div quando le immagini sono state uploadate con successo (Ajax) Ajax 0
S Select Concatenate Ajax, php, sql Presentati al Forum 16
otto9due Error anomalo durante invio dati $.ajax Ajax 20
F Interazione tra i form html ajax e php PHP 3
D Come salvare scelta della select "dinamica" ajax-php? Come dato php o attributo value tag option? Ajax 5
D Sono disperato: Menu select dinamici con Ajax e PHP PHP 1

Discussioni simili