Sistemi 3

Php e Ajax

 

Lettura di una tabella MySQL con php + Ajax

 

index.html

<html>
<head>
<script src="clienthint.js"></script>
</head>
<body>
<form>
Nazione:<br />
<input type="text" id="txt1" onKeyUp="showHint(this.value)" WIDTH="300" STYLE="width: 300px">
<div id="txtHint"></div></p>
</form>
</body>
</html>

clienthint.js (codice Javascript)

var xmlHttp

function showHint(str)
{
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="gethint.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function showUser(str)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var cambia = document.getElementById('txt1');
cambia.value = str;
var url="getcustomer.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

gethint.php (pagina Php)

<?php
// Nomi delle nazioni
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db1", $con);
$sql="SELECT distinct paese FROM clienti";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$a[]= $row['paese'];
}
mysql_close($con);
//parametro q dall'URL
$q=$_GET["q"];
//cerca tutte le occorrenze
if (strlen($q) > 0)
{
$quanti = 0;
$hint="<select NAME=\"scelta\" onChange=\"showUser(this.value)\" WIDTH=\"300\" STYLE=\"width: 300px\" size=\"10\">";
for($i=0; $i<count($a); $i++)
{
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
{
$hint=$hint."<option value=$a[$i]>$a[$i]</option>";
$quanti +=1;
}
}
$hint=$hint."</select>";
}

//output
if ($quanti>0)
{
$response=$hint;
echo $response;
}
?>

getcustomer.php (pagina Php)

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("db1", $con);

$sql="SELECT * FROM clienti WHERE paese = '".$q."'";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Codice</th>
<th>Nome</th>
<th>Paese</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['codice'] . "</td>";
echo "<td>" . $row['nome'] . "</td>";
echo "<td>" . $row['paese'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);
?>







> Tecnologia Ajax
> Database di prova per MySQL
(dal sito dev.mysql.com)
da importare in MySQL con il comando
SOURCE nomefile.sql
Paesi-dati
Paesi-schema
database Sakila con film, attori, ecc (schema + dati)
database World completo come quello dei paesi (schema + dati)
> Manuali in linea
Manuale php
Manuale mysql