Io ho 'imparato' a farlo giusto oggi...
ti posto il codice...
pagina: ajax.js
codice:
//Creazione dell'oggetto xmlhttp
function assegnaXMLHttpRequest() {
var
XHR = null,
browserUtente = navigator.userAgent.toUpperCase();
if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
XHR = new XMLHttpRequest();
else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0) {
if(browserUtente.indexOf("MSIE 5") < 0)
XHR = new ActiveXObject("Msxml2.XMLHTTP");
else
XHR = new ActiveXObject("Microsoft.XMLHTTP");
}
return XHR;
};
// funzione per prendere un elemento con id univoco
function prendiElementoDaId(id_elemento) {
var elemento;
if(document.getElementById)
elemento = document.getElementById(id_elemento);
else
elemento = document.all[id_elemento];
return elemento;
};
//questa è la funzione che richiama la pagina che inserisce il record
function add(tabella){
var
http = assegnaXMLHttpRequest();
elemento = prendiElementoDaId('messaggino');
if(http){
//alert("ins_marca.php?nome" + escape(document.forms['ins_marca'].nome.value));
http.open("get","include/nuova_"+tabella+".php?nome=" + escape(document.forms['ins_marca'].nome.value),true);
http.setRequestHeader("connection", "close");
http.onreadystatechange = function() {
// verifica dello stato
if(http.readyState === readyState.COMPLETATO) {
// verifica della risposta da parte del server
if(statusText[http.status] === "OK")
// operazione avvenuta con successo
elemento.innerHTML = http.responseText;
else {
// errore di caricamento
elemento.innerHTML = "Impossibile effettuare l'operazione richiesta.
";
elemento.innerHTML += "Errore riscontrato: " + statusText[http.status];
}
}
}
// invio richiesta
http.send(null);
}
carica(tabella);
return true;
}
nuova_marca.php (la pagina che effettua l'inserimento)
Codice PHP:
<?
include_once('../../conn.php');
if($_SESSION['admin'] == 1){
if(strlen($_GET['nome']) < 1){
echo("Inserire un valore corretto");
} else {
$sql = "INSERT INTO tbl_marche (descrizione) VALUES('" . $_GET['nome'] . "')";
mysql_query($sql) or die("Si è verificato un errore");
echo("Record inserito con successo:
Dati inseriti: [b]" . $_GET['nome'] . "[/b]");
}
} else {
echo("La sessione non esiste");
}
?>
questo invece è il form che richiama la funzione add
codice:
<form id="ins_marca" runat="server">
<table border="0" cellspacing="0" cellpadding="0" width="300">
<tr>
<td width="300">Nome Marca</td>
</tr>
<tr>
<td width="300"><input type="text" name="nome" size="25" style="width: 170; border: 1px solid #afb4b8; font-size:10px; font-family: Tahoma,Arial,sans-serif; color: black; font-weight: normal; margin-bottom:5px;" /></td>
</tr>
<tr>
<td width="300">
<input type="button" name="invia" value="inserisci" onClick="javascript:add('marca');" />
</td>
</tr>
</table>
</form>
spero che il tutto ti sia utile... io devo imparare ancora molto (diciamo)