Sto realizzando una sorta di bacheca usando php mysql e ajax
Questa e amico.php
Codice PHP:
<?php @include 'config2.php';
?>
<html>
<head>
<title>Shoutbox</title>
<script language="JavaScript" type="text/javascript" src="ajax.js">
</script>
</head>
<body>
<?
$query = "SELECT * FROM shoutbox ORDER BY id DESC";
$result = @mysql_query($query) or die (mysql_error());
if (mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
$nick = stripslashes($row['nick']);
$testo = stripslashes($row['testo']);
$data = $row['data'];
// formattiamo la data in "gg-mm-aaaa"
$data = preg_replace('/^(.{4})-(.{2})-(.{2})$/','$3-$2-$1', $data);
$sito_web = $row['sito_web'];
echo "$nick - $testo - $data
\r";
}
}
@mysql_close();
?>
<div id="modulo">
</div>
<form name="form_invio" method="POST" onsubmit="salva(); return false;">
Nick:
<input name="nick" type="text">
Sito Web:
<input name="sito_web" type="text">
Commento
<input name="testo" type="text">
<input type="submit" name="submit" value="Invia">
</form>
</body>
</html>
questo e invia5.php sarebbe il file di invio dati
Codice PHP:
<?php
@include 'config2.php';
if (isset($_POST['nick']) && isset($_POST['testo']) && isset($_POST['sito_web']))
{
$nick=addslashes($_POST['nick']);
$testo=addslashes($_POST['testo']);
$sito_web=addslashes($_POST['sito_web']);
$query = "INSERT into shoutbox (nick, testo, sito_web, data) VALUES ('$nick', '$testo', '$sito_web', now())";
$result = @mysql_query($query) or die (mysql_error());
@mysql_close();
}
?>
E questo sarebbe il file javascript
codice HTML:
// funzione per la chiamata dell'oggetto XMLHttpRequest
function ajax(){
var ajaxRequest;
try{
// controllo per i browser diversi da IE
ajaxRequest = new XMLHttpRequest();
}catch (e){
// contorollo per IE
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
// controllo per i browser che non supportano l'XMLHttpRequest
alert("Il browser non supporta questo ShoutBox");
return false;
}
}
}
return ajaxRequest;
}
// funzione per il salvataggio dei dati
function salva(){
htmlRequest = ajax();
if (htmlRequest==null){
alert ("Il browser non supporta richieste HTTP");
return;
}
// controlliamo i parametri obbligatori
if(document.form_invio.nick.value == "" || document.form_invio.nick.value == "NULL" || document.form_invio.testo.value == "" || document.form_invio.testo.value == "NULL")
{
alert('Inserisci sia il Nick che il testo');
return;
}
// inviamo i parametri al file per l'INSERT nel database
htmlRequest.open('POST', 'invia5.php');
htmlRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
htmlRequest.send('nick='+document.form_invio.nick.value+'&testo='+document.form_invio.testo.value+'&sito_web='+document.form_invio.sito_web.value);
// svuotiamo il modulo per il messaggio
document.form_invio.testo.value = '';
document.form_invio.testo.focus();
}
// funzione per mostrare i dati
function mostra() {
htmlRequest = ajax();
// controllo nel caso in cui non possa richiamato l'oggetto Xmlhttp
if (htmlRequest==null){
alert ("Il browser non supporta richieste HTTP");
return;
}
htmlRequest.onreadystatechange = function(){
// Restituisce lo stato della richiesta
if(htmlRequest.readyState == 4){
// Restituice il corpo della risposta come stringa
document.getElementById("modulo").innerHTML = htmlRequest.responseText;
}
}
// chiamata della pagina PHP che estrae i records
htmlRequest.open("GET", "amico.php", true);
htmlRequest.send(null);
}
// chiamata alla funzione
mostra();
// intevallo per l'aggiornamento della pagina
setInterval("mostra()",1000);
Il problema che nn ho capito come mostare a video in modo asincrono i commenti lasciati sulle bacheche degli altri utenti
Esempio
Mettiamo caso che io mi trovo sulla pagina amico.php?id=$3 devo fare in modo di mostrare solo i commenti relativi all id3
Nel codice javascript qui
codice HTML:
// chiamata della pagina PHP che estrae i records
htmlRequest.open("GET", "amico.php", true);
htmlRequest.send(null);
mi richiama la pagina amico.php ...praticamente io qui dovrei fargli capire che deve mostrarmi solo i commenti relativi all id3
C'è un modo per risolvere?Devo agire sul codice javascript?