Prova questi. Dovrai rimpiazzare il codice di connessione al database ( include('class.db.001.php');
$connexion = DB::connect(); ) con il tuo.
Si puo' di sicuro fare di meglio ma un paio di ore fa non sapevo nemmeno che questo fosse possibile.
test520c.php usa ob_start quindi non deve esserci alcun carattere prima del primo <?php
File test520.php
Codice PHP:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script language="JavaScript" type="text/javascript">
<!--
//---- Carico la tabella
$( document ).ready(function() {
loadTable();
});
function loadTable() {
var queryString = 'rnd'+Math.random();
$.get('test520c.php', queryString, resetData);
} // function loadTable()
function resetData(data) {
document.getElementById('id').value = "";
document.getElementById('nome').value = "";
document.getElementById('cognome').value = "";
document.getElementById('stato').value = " ";
$('#container').html(data);
} // function fullfillContainer(data)
function getRecord(anId) {
//---- salvo l'id del record da modificare
document.getElementById('id').value = anId;
//---- leggo la tabella per ricavare i dati
var queryString = 'id=' +anId+'&rnd'+Math.random();
$.get('test520a.php', queryString, processResponse);
} // function getRecord(anId)
function processResponse(data) {
a = data.split('||');
document.getElementById('id').value = a[0];
document.getElementById('nome').value = a[1];
document.getElementById('cognome').value = a[2];
document.getElementById('stato').value = a[3];
} // function processResponse(data)
function update() {
//---- aggiorno la tabella
anId = document.getElementById('id').value;
stato = document.getElementById('stato').value;
if (anId == "") {
alert('Seleziona una riga !!!');
return;
} // if (stato == " ")
if (stato == " ") {
alert('Lo stato è obbligatorio');
return;
} // if (stato == " ")
var queryString = 'id=' +anId+'&stato='+stato+'&rnd'+Math.random();
$.get('test520b.php', queryString, processResponse2);
} // function update()
function processResponse2(data) {
//---- Ricarico la pagina
loadTable();
} // function processResponse2(data)
//-->
</script>
</head>
<body>
<div>
<input type="hidden" name="id" id="id" />
<table summary="">
<tr>
<td>Nome :</td><td><input type="text" name="nome" id="nome" value="" disabled="true" /></td>
</tr>
<tr>
<td>Cognome :</td><td><input type="text" name="cognome" id="cognome" value="" disabled="true" /></td>
</tr>
<tr>
<td>Stato :</td>
<td>
<select name="stato" id="stato">
<option value=" "><-- Seleziona --></option>
<option value="0">Aperta</option>
<option value="1">Chiusa</option>
<option value="2">Annullata</option>
</select>
</td>
</tr>
<tr>
<td colspan="3" align="center"><hr/><input type="button" value="Modifica" onclick="update()"/></td>
</tr>
</table>
</div>
<hr/>
<div id="container"></div>
<script language="JavaScript" type="text/javascript">
<!--
//-->
</script>
</body>
</html>
File test520a.php - lettura di un record
Codice PHP:
<?php
// script a scopo informativo - nessuna sicurezza anti hacking
include('class.db.001.php');
$connexion = DB::connect();
$id = $_GET['id'];
//---- Record
$queryString = "select id,nome,cognome,stato from tabella001 where id='$id'";
$result = mysqli_query($connexion,$queryString) or die(mysqli_error($connexion));
list($id,$nome,$cognome,$stato) = mysqli_fetch_array($result);
$nome = stripslashes($nome);
$cognome = stripslashes($cognome);
echo "$id||$nome||$cognome||$stato";
?>
File test520b.php - aggiornamento record
Codice PHP:
<?php
// script a scopo informativo - nessuna sicurezza anti hacking
include('class.db.001.php');
$connexion = DB::connect();
$id = $_GET['id'];
$stato = $_GET['stato'];
//---- Record
$queryString = "update tabella001 set stato='$stato' where id='$id'";
$result = mysqli_query($connexion,$queryString) or die(mysqli_error($connexion));
echo "$id || $stato";
?>
File test520c.php - Tabella dei dati
Codice PHP:
<?php
include('class.db.001.php');
$connexion = DB::connect();
//---- Pages
$queryString = 'select id,nome,cognome,stato from tabella001 order by cognome, nome';
$result = mysqli_query($connexion,$queryString) or die(mysqli_error($connexion));
ob_start();?>
<table summary=""><?php
while (list($id,$nome,$cognome,$stato) = mysqli_fetch_array($result)) {
$nome = stripslashes($nome);
$cognome = stripslashes($cognome); ?>
<tr>
<td><?php print $nome ?></td>
<td><?php print $cognome ?></td>
<td>
<select value="<?php print $stato ?>" disabled="true">
<option value=" "><-- Seleziona --></option>
<option value="0" <?php print $stato == '0' ? 'selected' : ''; ?>>Aperta</option>
<option value="1" <?php print $stato == '1' ? 'selected' : ''; ?>>Chiusa</option>
<option value="2" <?php print $stato == '2' ? 'selected' : ''; ?>>Annullata</option>
</select>
</td>
<td>
<input type="button" name="sel" value="Seleziona" onclick="getRecord(<?php print $id ?>)" />
</td>
</tr><?php
} ?>
</table><?php
$content = ob_get_contents();
ob_end_clean();
echo $content;?>
Struttura tabella e dati
Codice PHP:
--
-- Structure de la table `tabella001`
--
CREATE TABLE IF NOT EXISTS `tabella001` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nome` varchar(50) NOT NULL,
`cognome` varchar(50) NOT NULL,
`stato` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
--
-- Contenu de la table `tabella001`
--
INSERT INTO `tabella001` (`id`, `nome`, `cognome`, `stato`) VALUES
(1, 'Pippo', 'Rossi', 0),
(2, 'Caio', 'Bianchi', 1),
(3, 'Pluto', 'Verdi', 1),
(4, 'Gaio', 'Gatti', 0);