Ho un problema ad implementare Ajax con php5+mysql5.
Ho la mia funzione XHR Javascript, un file rpc.php per gestire le chiamate di javascript e una tabella da cui estraggo i dati i quali andranno messi dentro un campo di un form con innerHTML. Mi sono basato sul codice trovato su Mozilla.
Facendo delle prove (ora posto anche tutto il codice) succede che
1) su mozilla da problemi: non visualizza il risultato. Da anche un problema in console ma ho letto su mozdev che è normale per Firefox >1.5.
2) su iexplorer 6 invece visualizza l'alert con alert(http_request.responseText); ma poi quando clicco ok il risultato sparisce anche dalla pagina
3) se tolgo l'alert non riesco a capire come mai il risultato compaia per un millisecondo e poi sparisca improvvisamente. E' come se, venendo distrutto l'oggetto httpRequest, dopo la chiamata alla funzione facesse sparire anche l'output.
4) un'altra cosa che non capisco è come mai il codice javascript delle volte (con l'alert abilitato) non venga eseguito. SEMBRA che il codice javascript sia più veloce ad eseguirsi della chiamata al file php che si estrae i dati e quindi termina prima che ci siano effettivamente questi dati.
Ho riletto il codice ma: la funzione non dovrebbe aspettare l'output da php prima di andare avanti quando do http_request.open('GET', 'rpc.php?artista='+url, true);?? quando faccio http_request.open è come se NON ASPETTASSE il risultato ed andasse avanti da solo.
il codice è
JAVASCRIPT
_____________________
codice:
function makeRequest(url) {
var http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Giving up :( Non riesco a creare una istanza XMLHTTP');
return false;
}
http_request.onreadystatechange = function() { alertContents(http_request); };
http_request.open('GET', 'rpc.php?artista='+url, true);
http_request.send(null);
}
function alertContents(http_request) {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
document.getElementById("mio_artista").innerHTML = http_request.responseText;
alert(http_request.responseText);
http_request=null;
} else {
alert('Si è verificato un problema con la richiesta');
}
}
}
INDEX.PHP
_____________________
codice:
<?php
header( "Cache-Control: no-cache" );
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Cache-Control" content="no-cache">
<title>Documento senza titolo</title>
<script src="ajax.js"></script>
</head>
<body>
<form id="myform" name="myform" method="get">
<table width="434" border="0" cellpadding="0">
<tr>
<th width="43" scope="row"><label>artista</label></th>
<td width="339"><input type="text" name="artista" />
<script type="text/javascript">
function cerca_artista(){
var nome_artista_url = document.myform.artista.value;
makeRequest(nome_artista_url);
//document.write(nome_artista+"
");//DEBUG
}
</script>
<input type="submit" value="Cerca" onclick="cerca_artista()"/></td>
</tr>
<tr>
<th scope="row"></th>
<td>
<span id="mio_artista"><input name="artista_choose" type="radio" /></span>
</td>
</tr>
</table>
</form>
</body>
</html>
RPC.PHP
_________________
codice:
<?php
header( "Cache-Control: no-cache" );
include("db_setup.php");
$my_artist=isset($_GET['artista']) ? ($_GET['artista']) : "0";
//echo $my_artist;
$conn = mysql_connect($myServer,$myUser,$myPassword) or
die("Connessione non riuscita: " . mysql_error());
mysql_select_db("prova") or
die("non seleziona il database: " . mysql_error());
$my_query="SELECT * FROM artisti WHERE nome_artista LIKE '".$my_artist."%'";
//echo $my_query."
";
$result=mysql_query($my_query) or
die("non fa la query: " . mysql_error());
//echo $result."
";
while($row = mysql_fetch_assoc($result)){
echo "<input name=\"artista_choose\" type=\"radio\" />".$row["nome_artista"]."
";
}
mysql_free_result($result);
?>