Update dinamico informazioni scritte in una pagina php

elpirata

Utente Attivo
18 Mar 2009
264
7
18
Buongiorno a tutti,
ho realizzato una pagina in php che verifica in un database mysql se nel mese corrente ci sono dei veicoli che hanno la revisione in scadenza e mostra a video i risultati ottenuti. Ho poi aggiunto accanto ad ogni record estratto un campo denominato "Note" che viene utilizzato dal personale addetto al contatto con il cliente per riportare l'esito della telefonata.

Ora il mio problema è che non so come assegnare al button che ho inserito a fondo pagina una funzione che permetta al click del mouse l'update nel database delle informazioni inserite dall'utente nel campo "Note"

Questo il codice della pagina php:
Codice:
<form method="POST" action="scadenza_revisione.php" id="inseriscinote" >
<?php
date_default_timezone_set('Europe/Rome');
require_once('function/date_day.php');

$veicolo='UNDEF';
$targa='UNDEF';
$cliente='UNDEF';
$tel='UNDEF';
$dtscrev='UNDEF';
$noterev='UNDEF';
$where='';

//var_dump($_GET['tipo']);
//exit;

if(isset($_GET['tipo']))$tipo = trim(strip_tags(stripslashes($_GET['tipo'])));
else echo 'Attenzione è stato riscontrato un errore contattare amministratore';

if ($tipo == "daeseguire"){
$where = "WHERE YEAR(dtscrev) = YEAR(NOW()) AND MONTH(dtscrev)= MONTH(NOW()) AND DAY(dtscrev) >= DAY(NOW())";

}elseif($tipo == "scadute"){
$where = "WHERE dtscrev < NOW()-1 AND dtscrev <> '0000-00-00'";
}

$query = "SELECT * FROM commesse $where GROUP BY targa ORDER BY dtscrev ASC LIMIT 20";
$oggetto =$mysqli->query($query);
$count = mysqli_num_rows($oggetto);

if ($count == 0) {

echo "<div class = 'container-ricerca-nulla'>";
echo "<div class ='row'><p class='btn btn-danger center-button'>Non sono presenti revisioni da eseguire</p></div>";
echo "</div>";

}else{

echo "<div class ='container-appuntamento'>";
echo "<table class='table table-striped table-responsive'>";
echo "<tr>";
echo "<th style='color:green'>Veicolo</th>";
echo "<th style='color:green'>Targa</th>";
echo "<th style='color:green'>Cliente</th>";
echo "<th style='color:green'>Telefono</th>";
echo "<th style='color:green'>Data Scadenza<br>Revisione</th>";
echo "<th style='color:red'>Note</th>";
echo "</tr>";


while($scorri_oggetto=$oggetto->fetch_assoc()){
$veicolo = mysqli_real_escape_string($mysqli, $scorri_oggetto['veicolo']);
$targa = mysqli_real_escape_string($mysqli, $scorri_oggetto['targa']);
$cliente = mysqli_real_escape_string($mysqli, $scorri_oggetto['cliente']);
$tel = mysqli_real_escape_string($mysqli, $scorri_oggetto['tel']);
$dtscrev = mysqli_real_escape_string($mysqli, $scorri_oggetto['dtscrev']);
$noterev = mysqli_real_escape_string($mysqli, $scorri_oggetto['noterev']);


/* FILTRI e CONVERSIONI SULLE VARIABILI POST */
$cliente = trim(strip_tags(stripslashes($cliente)));
$cliente = str_replace('\r\n' , '<br>', $cliente);
$cliente = stripslashes($cliente);
$cliente = wordwrap($cliente,18,"<br>"); //MANDO A CAPO OGNI 18 CARATTERI

$noterev = trim(strip_tags(stripslashes($noterev)));
$noterev = str_replace('\r\n' , '<br>', $noterev);
$noterev = stripslashes($noterev);


$veicolo = trim(strip_tags(stripslashes($veicolo)));
$veicolo = str_replace('\r\n' , '<br>', $veicolo);

$dtscrev = strtotime($dtscrev);
$dtscrev = date('d/m/Y', $dtscrev);
?>

<tr>
<td style="text-align:left;"><?php echo $veicolo; ?></td>
<td style="text-align:left;"><?php echo $targa; ?></td>
<td style="text-align:left;"><?php echo $cliente; ?></td>
<td style="text-align:left;"><?php echo $tel; ?></td>
<td><?php echo data_it($dtscrev);?></td>
<td><input name="noterev" type="text" value="<?php echo $scorri_oggetto['noterev']; ?>" size="63" maxlength="200" /></td>
</tr>

<?php
}
echo "</table>";
echo "</div>";
}
?>
<div class="container-fluid">
<tr>
<td><button type="button" class="btn btn-success btn-bd btn-block"><input type="hidden" name="aggiorna_noterev" value="ok"><input type="hidden" value="<?php echo $scorri_oggetto['id_com']; ?>" name="id_com" >Memorizza nel database le note inserite</button></td>
</tr>
</div>

</form>

<?php
if (isset($_POST['aggiorna_noterev'])){
$query="UPDATE commesse
SET noterev='".$_POST['noterev']."'
WHERE id_com='".$_POST['id_com']."' ";
$oggetto =$mysqli->query($query);
}
?>


Questa invece la pagina con il relativo pulsante che non riesco a far funzionare


Naturalmente l'update dovrebbe avvenire sulla stessa pagina e magari dopo l'update un alert a video informa l'utente che le informazioni sono state memorizzate con successo.

Confido in un vostro supporto perchè non so davvero dove andare a parare.

Grazie anticipatamente a tutti
 
Cioè "dinamicamente con Jquery" intendi che vuoi che i dati vengano salvati senza che si ricarichi la pagina?
 
Prima di tutto, se non lo hai già fatto, devi includere jquery, inserendo questo nell'head:
HTML:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
Ora aggiungi un campo hidden con id="totale" e value="<?php echo $count;?>".
Subito prima dell'inizio del while aggiungi $i=0; mentre subito dopo l'inizio del while aggiungi $i++;
Poi nel campo input della nota aggiungi:
PHP:
id="nota<?php echo $i;?>"
Ora controlla nella sorgente del codice html che i vari campi nota della tabella si chiamino nota1, nota2, ecc.
Se è tutto ok, prosegui cosi:
Aggiungi uno script del genere, nell'head della pagina:
HTML:
<script>
$(document).ready(function(){
$("#id_bottone").click(function(){ //al posto di id_bottone metti l'id del tuo tasto di invio
var totale = $("#totale").val();
var note = [];
for(n=1;n<totale;n++){
note.push($("#nota"+n).val());
}
$.ajax({
        type:'POST',
        url:"modifica.php",
dataType: 'JSON',
        data:{'notes':note},
        success:function(msg){
            if(msg == "ok"){
//codice per mostrare che l'inserimento è riuscito
}else{
//codice per mostrare che l'inserimento non è riuscito
}
        },
        error:function(){
            alert('Errore.');
        }
    })
});
});
</script>
Infine devi creare un file modifica.php in cui ricevi i dati con POST e li inserisci nel database:
PHP:
$note = $_POST['notes'];
foreach($note as $nota){
$query = mysqli_query(...); //qui metti la query che modifica la riga del db
}
if($query){
echo "ok";
}else{
echo "errore";
}
 
Potresti farmi un esempio di come integrare il tuo codice nel mio ?

PHP:
<form method="POST" action="scadenza_revisione.php" id="inseriscinote" >
<?php
date_default_timezone_set('Europe/Rome');
require_once('function/date_day.php');

$veicolo='UNDEF';
$targa='UNDEF';
$cliente='UNDEF';
$tel='UNDEF';
$dtscrev='UNDEF';
$noterev='UNDEF';
$where='';

//var_dump($_GET['tipo']);
//exit;

if(isset($_GET['tipo']))$tipo = trim(strip_tags(stripslashes($_GET['tipo'])));
else echo 'Attenzione è stato riscontrato un errore contattare amministratore';

if ($tipo == "daeseguire"){
$where = "WHERE YEAR(dtscrev) = YEAR(NOW()) AND MONTH(dtscrev)= MONTH(NOW()) AND DAY(dtscrev) >= DAY(NOW())";

}elseif($tipo == "scadute"){
$where = "WHERE dtscrev < NOW()-1 AND dtscrev <> '0000-00-00'";
}

$query = "SELECT * FROM commesse $where GROUP BY targa ORDER BY dtscrev ASC LIMIT 20";
$oggetto =$mysqli->query($query);
$count = mysqli_num_rows($oggetto);

if ($count == 0) {

echo "<div class = 'container-ricerca-nulla'>";
echo "<div class ='row'><p class='btn btn-danger center-button'>Non sono presenti revisioni da eseguire</p></div>";
echo "</div>";

}else{

echo "<div class ='container-appuntamento'>";
echo "<table class='table table-striped table-responsive'>";
echo "<tr>";
echo "<th style='color:green'>Veicolo</th>";
echo "<th style='color:green'>Targa</th>";
echo "<th style='color:green'>Cliente</th>";
echo "<th style='color:green'>Telefono</th>";
echo "<th style='color:green'>Data Scadenza<br>Revisione</th>";
echo "<th style='color:red'>Note</th>";
echo "</tr>";


while($scorri_oggetto=$oggetto->fetch_assoc()){
$veicolo = mysqli_real_escape_string($mysqli, $scorri_oggetto['veicolo']);
$targa = mysqli_real_escape_string($mysqli, $scorri_oggetto['targa']);
$cliente = mysqli_real_escape_string($mysqli, $scorri_oggetto['cliente']);
$tel = mysqli_real_escape_string($mysqli, $scorri_oggetto['tel']);
$dtscrev = mysqli_real_escape_string($mysqli, $scorri_oggetto['dtscrev']);
$noterev = mysqli_real_escape_string($mysqli, $scorri_oggetto['noterev']);


/* FILTRI e CONVERSIONI SULLE VARIABILI POST */
$cliente = trim(strip_tags(stripslashes($cliente)));
$cliente = str_replace('\r\n' , '<br>', $cliente);
$cliente = stripslashes($cliente);
$cliente = wordwrap($cliente,18,"<br>"); //MANDO A CAPO OGNI 18 CARATTERI

$noterev = trim(strip_tags(stripslashes($noterev)));
$noterev = str_replace('\r\n' , '<br>', $noterev);
$noterev = stripslashes($noterev);


$veicolo = trim(strip_tags(stripslashes($veicolo)));
$veicolo = str_replace('\r\n' , '<br>', $veicolo);

$dtscrev = strtotime($dtscrev);
$dtscrev = date('d/m/Y', $dtscrev);
?>

<tr>
<td style="text-align:left;"><?php echo $veicolo; ?></td>
<td style="text-align:left;"><?php echo $targa; ?></td>
<td style="text-align:left;"><?php echo $cliente; ?></td>
<td style="text-align:left;"><?php echo $tel; ?></td>
<td><?php echo data_it($dtscrev);?></td>
<td><input name="noterev" type="text" value="<?php echo $scorri_oggetto['noterev']; ?>" size="63" maxlength="200" /></td>
</tr>

<?php
}
echo "</table>";
echo "</div>";
}
?>
<div class="container-fluid">
<tr>
<td><button type="button" class="btn btn-success btn-bd btn-block"><input type="hidden" name="aggiorna_noterev" value="ok"><input type="hidden" value="<?php echo $scorri_oggetto['id_com']; ?>" name="id_com" >Memorizza nel database le note inserite</button></td>
</tr>
</div>

</form>

<?php
if (isset($_POST['aggiorna_noterev'])){
$query="UPDATE commesse
SET noterev='".$_POST['noterev']."'
WHERE id_com='".$_POST['id_com']."' ";
$oggetto =$mysqli->query($query);
}
?>

Poi tu mi scrivi:
Ora controlla nella sorgente del codice html che i vari campi nota della tabella si chiamino nota1, nota2, ecc.

Nel database mysql ho un unico campo che è di tipo VARCHAR(200) e si chiama "noterev"
 
Nel database mysql ho un unico campo che è di tipo VARCHAR(200) e si chiama "noterev"
si intendevo i campi input della tabella html

PHP:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#id_bottone").click(function(){ //al posto di id_bottone metti l'id del tuo tasto di invio
var totale = $("#totale").val();
var array_note=new Array();
    for (var i=1; i<=totale; i++){
        array_note[i]=[];
        array_note[i]['targa']=$("#targa"+i).val();
        array_note[i]['nota']=$("#nota"+i).val();
    }

    var send_array = JSON.stringify(array_note);
$.ajax({
type:'POST',
url:"modifica.php",
dataType: 'JSON',
data:{'notes':send_array},
success:function(msg){
if(msg == "ok"){
//codice per mostrare che l'inserimento è riuscito
}else{
//codice per mostrare che l'inserimento non è riuscito
}
},
error:function(){
alert('Errore.');
}
})
});
});
</script>

</head>

<form method="POST" action="scadenza_revisione.php" id="inseriscinote" >
<?php
date_default_timezone_set('Europe/Rome');
require_once('function/date_day.php');

$veicolo='UNDEF';
$targa='UNDEF';
$cliente='UNDEF';
$tel='UNDEF';
$dtscrev='UNDEF';
$noterev='UNDEF';
$where='';

//var_dump($_GET['tipo']);
//exit;

if(isset($_GET['tipo']))$tipo = trim(strip_tags(stripslashes($_GET['tipo'])));
else echo 'Attenzione è stato riscontrato un errore contattare amministratore';

if ($tipo == "daeseguire"){
$where = "WHERE YEAR(dtscrev) = YEAR(NOW()) AND MONTH(dtscrev)= MONTH(NOW()) AND DAY(dtscrev) >= DAY(NOW())";

}elseif($tipo == "scadute"){
$where = "WHERE dtscrev < NOW()-1 AND dtscrev <> '0000-00-00'";
}

$query = "SELECT * FROM commesse $where GROUP BY targa ORDER BY dtscrev ASC LIMIT 20";
$oggetto =$mysqli->query($query);
$count = mysqli_num_rows($oggetto);

if ($count == 0) {

echo "<div class = 'container-ricerca-nulla'>";
echo "<div class ='row'><p class='btn btn-danger center-button'>Non sono presenti revisioni da eseguire</p></div>";
echo "</div>";

}else{

echo "<div class ='container-appuntamento'>";
echo "<table class='table table-striped table-responsive'>";
echo "<tr>";
echo "<th style='color:green'>Veicolo</th>";
echo "<th style='color:green'>Targa</th>";
echo "<th style='color:green'>Cliente</th>";
echo "<th style='color:green'>Telefono</th>";
echo "<th style='color:green'>Data Scadenza<br>Revisione</th>";
echo "<th style='color:red'>Note</th>";
echo "</tr>";

$i=0;
while($scorri_oggetto=$oggetto->fetch_assoc()){
$i++;
$veicolo = mysqli_real_escape_string($mysqli, $scorri_oggetto['veicolo']);
$targa = mysqli_real_escape_string($mysqli, $scorri_oggetto['targa']);
$cliente = mysqli_real_escape_string($mysqli, $scorri_oggetto['cliente']);
$tel = mysqli_real_escape_string($mysqli, $scorri_oggetto['tel']);
$dtscrev = mysqli_real_escape_string($mysqli, $scorri_oggetto['dtscrev']);
$noterev = mysqli_real_escape_string($mysqli, $scorri_oggetto['noterev']);


/* FILTRI e CONVERSIONI SULLE VARIABILI POST */
$cliente = trim(strip_tags(stripslashes($cliente)));
$cliente = str_replace('\r\n' , '<br>', $cliente);
$cliente = stripslashes($cliente);
$cliente = wordwrap($cliente,18,"<br>"); //MANDO A CAPO OGNI 18 CARATTERI

$noterev = trim(strip_tags(stripslashes($noterev)));
$noterev = str_replace('\r\n' , '<br>', $noterev);
$noterev = stripslashes($noterev);


$veicolo = trim(strip_tags(stripslashes($veicolo)));
$veicolo = str_replace('\r\n' , '<br>', $veicolo);

$dtscrev = strtotime($dtscrev);
$dtscrev = date('d/m/Y', $dtscrev);
?>

<tr>
<td style="text-align:left;"><?php echo $veicolo; ?></td>
<td style="text-align:left;"><?php echo $targa; ?></td>
<td style="text-align:left;"><?php echo $cliente; ?></td>
<td style="text-align:left;"><?php echo $tel; ?></td>
<td><?php echo data_it($dtscrev);?></td>
<td><input name="noterev" id="note<?php echo $i;?>" type="text" value="<?php echo $scorri_oggetto['noterev']; ?>" size="63" maxlength="200" /></td>
<input type="hidden" id="targa<?php echo $i;?>" value="<?php echo $targa;?>">
</tr>
<input type="hidden" id="totale" value="<?php echo $count;?>">
<?php
}
echo "</table>";
echo "</div>";
}
?>
<div class="container-fluid">
<tr>
<td><button type="button" id="bottone" class="btn btn-success btn-bd btn-block"><input type="hidden" name="aggiorna_noterev" value="ok"><input type="hidden" value="<?php echo $scorri_oggetto['id_com']; ?>" name="id_com" >Memorizza nel database le note inserite</button></td>
</tr>
</div>

</form>
modifica.php:
PHP:
$data = json_decode(stripslashes($_REQUEST['notes']));
foreach($data as $a){
$targa = $a->targa;
$nota = $a->nota;
    $query=mysqli_query($conn, "UPDATE commesse SET notarev='$nota' WHERE targa='$targa'");
}
if($query){
echo "ok";
}else{
echo "errore";
}
 
Buongiorno,
ho provato ad eseguire il codice da te proposto e anzitutto prima di proseguire ti ringrazio infinitamente.

Quando clicco sul button per memorizzare la nota, ottengo l'alert con la scritta

Ti posto il codice cosi come è adesso:

Pagina scadenza_revisione.php
PHP:
<script>
$(document).ready(function(){
$("#bottone").click(function(){ //al posto di id_bottone metti l'id del tuo tasto di invio
var totale = $("#totale").val();
var array_note=new Array();
    for (var i=1; i<=totale; i++){
        array_note[i]=[];
        array_note[i]['targa']=$("#targa"+i).val();
        array_note[i]['noterev']=$("#noterev"+i).val();
    }

    var send_array = JSON.stringify(array_note);
$.ajax({
type:'POST',
url:"modifica.php",
dataType: 'JSON',
data:{'notes':send_array},
success:function(msg){
if(msg == "ok"){
//codice per mostrare che l'inserimento è riuscito
}else{
//codice per mostrare che l'inserimento non è riuscito
}
},
error:function(){
alert('Errore.');
}
})
});
});
</script>

<form method="POST" action="scadenza_revisione.php" id="inseriscinote" >
<?php
date_default_timezone_set('Europe/Rome');
require_once('function/date_day.php');

$veicolo='UNDEF';
$targa='UNDEF';
$cliente='UNDEF';
$tel='UNDEF';
$dtscrev='UNDEF';
$noterev='UNDEF';
$where='';

//var_dump($_GET['tipo']);
//exit;

if(isset($_GET['tipo']))$tipo = trim(strip_tags(stripslashes($_GET['tipo'])));
else echo 'Attenzione è stato riscontrato un errore contattare amministratore';

if ($tipo == "daeseguire"){
$where = "WHERE YEAR(dtscrev) = YEAR(NOW()) AND MONTH(dtscrev)= MONTH(NOW()) AND DAY(dtscrev) >= DAY(NOW())";

}elseif($tipo == "scadute"){
$where = "WHERE dtscrev < NOW()-1 AND dtscrev <> '0000-00-00'";
}

$query = "SELECT * FROM commesse $where GROUP BY targa ORDER BY dtscrev ASC LIMIT 20";
$oggetto =$mysqli->query($query);
$count = mysqli_num_rows($oggetto);

if ($count == 0) {

echo "<div class = 'container-ricerca-nulla'>";
echo "<div class ='row'><p class='btn btn-danger center-button'>Non sono presenti revisioni da eseguire</p></div>";
echo "</div>";

}else{

echo "<div class ='container-appuntamento'>";
echo "<table class='table table-striped table-responsive'>";
echo "<tr>";
echo "<th style='color:green'>Veicolo</th>";
echo "<th style='color:green'>Targa</th>";
echo "<th style='color:green'>Cliente</th>";
echo "<th style='color:green'>Telefono</th>";
echo "<th style='color:green'>Data Scadenza<br>Revisione</th>";
echo "<th style='color:red'>Note</th>";
echo "</tr>";

$i=0;
while($scorri_oggetto=$oggetto->fetch_assoc()){
$i++;
$veicolo = mysqli_real_escape_string($mysqli, $scorri_oggetto['veicolo']);
$targa = mysqli_real_escape_string($mysqli, $scorri_oggetto['targa']);
$cliente = mysqli_real_escape_string($mysqli, $scorri_oggetto['cliente']);
$tel = mysqli_real_escape_string($mysqli, $scorri_oggetto['tel']);
$dtscrev = mysqli_real_escape_string($mysqli, $scorri_oggetto['dtscrev']);
$noterev = mysqli_real_escape_string($mysqli, $scorri_oggetto['noterev']);


/* FILTRI e CONVERSIONI SULLE VARIABILI POST */
$cliente = trim(strip_tags(stripslashes($cliente)));
$cliente = str_replace('\r\n' , '<br>', $cliente);
$cliente = stripslashes($cliente);
$cliente = wordwrap($cliente,18,"<br>"); //MANDO A CAPO OGNI 18 CARATTERI

$noterev = trim(strip_tags(stripslashes($noterev)));
$noterev = str_replace('\r\n' , '<br>', $noterev);
$noterev = stripslashes($noterev);


$veicolo = trim(strip_tags(stripslashes($veicolo)));
$veicolo = str_replace('\r\n' , '<br>', $veicolo);

$dtscrev = strtotime($dtscrev);
$dtscrev = date('d/m/Y', $dtscrev);
?>

<tr>
<td style="text-align:left;"><?php echo $veicolo; ?></td>
<td style="text-align:left;"><?php echo $targa; ?></td>
<td style="text-align:left;"><?php echo $cliente; ?></td>
<td style="text-align:left;"><?php echo $tel; ?></td>
<td><?php echo data_it($dtscrev);?></td>
<td><input name="noterev" id="noterev<?php echo $i;?>" type="text" value="<?php echo $scorri_oggetto['noterev']; ?>" size="63" maxlength="200" /></td>
<input type="hidden" id="targa<?php echo $i;?>" value="<?php echo $targa;?>">
</tr>
<input type="hidden" id="totale" value="<?php echo $count;?>">

<?php
}
echo "</table>";
echo "</div>";
}
?>
<div class="container-fluid">
<tr>
<td><button type="button" id="bottone" class="btn btn-success btn-bd btn-block"><input type="hidden" name="aggiorna_noterev" value="ok"><input type="hidden" value="<?php echo $scorri_oggetto['id_com']; ?>" name="id_com" >Memorizza nel database le note inserite</button></td>
</tr>
</div>
</form>

Pagina modifica.php

PHP:
<?php
$host = 'localhost';
$user = 'usermysql';
$pass = 'passwordmysql';
$db = 'databasemysql';
$mysqli = new mysqli("$host", "$user", "$pass", "$db");
$data = json_decode(stripslashes($_REQUEST['notes']));
foreach($data as $a){
$targa = $a->targa;
$noterev = $a->noterev;
$query=mysqli_query($mysqli, "UPDATE commesse SET noterev='$noterev' WHERE targa='$targa'");
}
if($query){
echo "ok";
}else{
echo "errore";
}
?>
 
Come prima cosa guarda la sorgente della pagina (Ctrl+U) e controlla che i vari input delle note abbiano id="nota1", nota2, nota3, ecc. Poi se è corretto, prova a fare qualche alert, tipo alert(send_array). La pagina è online? Se si, provo a vedere il codice e faccio qualche prova
 
Come prima cosa guarda la sorgente della pagina (Ctrl+U) e controlla che i vari input delle note abbiano id="nota1", nota2, nota3, ecc.

I campi a me non si chiamano il nome nota ma noterev

Poi se è corretto, prova a fare qualche alert, tipo alert(send_array). La pagina è online? Se si, provo a vedere il codice e faccio qualche prova
Non so dove andare ad inserire gli alert


Questo il codice sorgente della pagina CTRL + U, il campo viene correttamente incrementato (in totale l'esempio estrare 14 record ed il conto torna, ma nel codice sotto riportato ne ho inseriti solo 3 per comodità)

HTML:
<!doctype html>
<html lang="it">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="Gestionale Officina">

    <title>Gestione Officina</title>

    <!-- Bootstrap Core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link href="css/table.css" rel="stylesheet" media="screen" />
    <link href="css/home.css" rel="stylesheet" media="screen" />
    <link href="css/form.css" rel="stylesheet" media="screen" />
    <link href="css/font-awesome.min.css" rel="stylesheet" media="screen" />
    <link href='https://fonts.googleapis.com/css?family=Lato:300,400,700' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!--INIZIO SCRIPT-->
    <script type="text/javascript" src="http://ff.kis.v2.scr.kaspersky-labs.com/FD126C42-EBFA-4E12-B309-BB3FDD723AC1/main.js?attr=g8KFiG_7u1me8ZP9GjqnR6KbTLUsnQycdZmAHjS9qszqIciu7hswuAQvsr2FjfrONJBokNHZ0cgWj96qgjqb-Cmpfm8Vv88DW1cTMsOxadUoFc2aTbZnqhwuk_pRNXugm8DZcuAztRnqPW8GZNaLKg" charset="UTF-8"></script><script type="text/javascript" src="js/somma.js"></script>
    <script type="text/javascript" src="js/datapicker.js"></script>
    <script type="text/javascript" src="js/jquery-1.10.2.js"></script>
    <script type="text/javascript" src="js/jquery-ui-1.10.4.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui-i18n.js"></script>
    <!--FINE SCRIPT-->

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
    <script type="text/javascript" src="inc/jquery.ui.datepicker-it.js"></script>
    <script type="text/javascript" src="js/qcTimepicker.min.js"></script>
    <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
   

<script type="text/javascript">
   $(function() {
   var date = $('#datepicker').datepicker({ dateFormat: 'dd/mm/yy' }).val();
   var date = $('#datepicker1').datepicker({ dateFormat: 'dd/mm/yy' }).val();
   var date = $('#datepicker2').datepicker({ dateFormat: 'dd/mm/yy' }).val();
   //var minDate: new Date(, , );
   });
</script>


<script type="text/javascript">
   $(document).ready(function() {
   $('.timepicker').qcTimepicker();
   });
</script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>

<body>

    <!-- Page Content -->
   
    <div class="container white">
        <div class="header">
            <div class="logo"><a href="index.php?page=home">Gestione Officina</a></div>
        </div>

        <div class="row">

        <script>
$(document).ready(function(){
$("#bottone").click(function(){ //al posto di id_bottone metti l'id del tuo tasto di invio
var totale = $("#totale").val();
var array_note=new Array();
    for (var i=1; i<=totale; i++){
        array_note[i]=[];
        array_note[i]['targa']=$("#targa"+i).val();
        array_note[i]['noterev']=$("#noterev"+i).val();
    }

    var send_array = JSON.stringify(array_note);
$.ajax({
type:'POST',
url:"modifica.php",
dataType: 'JSON',
data:{'notes':send_array},
success:function(msg){
if(msg == "ok"){
//codice per mostrare che l'inserimento è riuscito
}else{
//codice per mostrare che l'inserimento non è riuscito
}
},
error:function(){
alert('Errore.');
}
})
});
});
</script>

<form method="POST" action="scadenza_revisione.php" id="inseriscinote" >
<div class ='container-appuntamento'><table class='table table-striped table-responsive'><tr><th style='color:green'>Veicolo</th><th style='color:green'>Targa</th><th style='color:green'>Cliente</th><th style='color:green'>Telefono</th><th style='color:green'>Data Scadenza<br>Revisione</th><th style='color:red'>Note</th></tr>
<tr>
<td style="text-align:left;">FIAT PANDA</td>
<td style="text-align:left;">AA123AA</td>
<td style="text-align:left;">ROSSI MARIO</td>
<td style="text-align:left;">33312312320</td>
<td>Giovedi<br>13/08/2020</td>
<td><input name="noterev" id="noterev1" type="text" value="" size="63" maxlength="200" /></td>
<input type="hidden" id="targa1" value=AA123AA">
</tr>
<input type="hidden" id="totale" value="14">


<tr>
<td style="text-align:left;">OPEL CORSA</td>
<td style="text-align:left;">BB123AA</td>
<td style="text-align:left;">BIANCHI MARIO</td>
<td style="text-align:left;">33312412321</td>
<td>Lunedi<br>17/08/2020</td>
<td><input name="noterev" id="noterev2" type="text" value="" size="63" maxlength="200" /></td>
<input type="hidden" id="targa2" value="BB123AA">
</tr>
<input type="hidden" id="totale" value="14">


<tr>
<td style="text-align:left;">LANCIA VOYAGER</td>
<td style="text-align:left;">CC123AA</td>
<td style="text-align:left;">VERDE GIULIO</td>
<td style="text-align:left;">00000</td>
<td>Martedi<br>18/08/2020</td>
<td><input name="noterev" id="noterev3" type="text" value="" size="63" maxlength="200" /></td>
<input type="hidden" id="targa3" value="CC123AA">
</tr>
<input type="hidden" id="totale" value="14">

ecc. ecc.

</table></div><div class="container-fluid">
<tr>
<td><button type="button" id="bottone" class="btn btn-success btn-bd btn-block"><input type="hidden" name="aggiorna_noterev" value="ok"><input type="hidden" value="" name="id_com" >Memorizza nel database le note inserite</button></td>
</tr>
</div>

</form>

<!--?php
if (isset($_POST['aggiorna_noterev'])){
$query="UPDATE commesse
SET noterev='".$_POST['noterev']."'
WHERE id_com='".$_POST['id_com']."' ";
$oggetto =$mysqli->query($query);
}
?-->
        </div>

        <hr>

           
                <div class="footer">
                    <div class="footer-text"> Testo del footer <br />
                    Gestione Officina - Copyright &copy; 2020 - All rights reserved.
                    </div>
                </div>
           
    </div>
   

   
    <!-- /.container -->

    <!-- Bootstrap Core JavaScript -->
    <script src="js/bootstrap.min.js"></script>

</body>
</html>
 
Questo l'ho testato e mi funziona:
HTML:
<script>
$(document).ready(function(){
$("#bottone").click(function(){ //al posto di id_bottone metti l'id del tuo tasto di invio
var totale = $("#totale").val();
var array_note=[];
    for (var i=1; i<=totale; i++){
    var targ = $("#targa"+i).val();
    var not = $("#noterev"+i).val();
        array_note.push({
        targa: targ,
        nota: not
        });
    }

    var send_array = JSON.stringify(array_note);
    alert(send_array);
$.ajax({
type:'POST',
url:"modifica.php",
dataType: 'JSON',
data:{'notes':send_array},
success:function(msg){
if(msg == "ok"){
//codice per mostrare che l'inserimento è riuscito
}else{
//codice per mostrare che l'inserimento non è riuscito
}
},
error:function(){
alert('Errore.');
}
})
});
});
</script>
 
Questo l'ho testato e mi funziona:
HTML:
<script>
$(document).ready(function(){
$("#bottone").click(function(){ //al posto di id_bottone metti l'id del tuo tasto di invio
var totale = $("#totale").val();
var array_note=[];
    for (var i=1; i<=totale; i++){
    var targ = $("#targa"+i).val();
    var not = $("#noterev"+i).val();
        array_note.push({
        targa: targ,
        nota: not
        });
    }

    var send_array = JSON.stringify(array_note);
    alert(send_array);
$.ajax({
type:'POST',
url:"modifica.php",
dataType: 'JSON',
data:{'notes':send_array},
success:function(msg){
if(msg == "ok"){
//codice per mostrare che l'inserimento è riuscito
}else{
//codice per mostrare che l'inserimento non è riuscito
}
},
error:function(){
alert('Errore.');
}
})
});
});
</script>

Al click sul pulsante a me compare, prima una box con tutte queste info e fin qui tutto ok, perchè prende correttamente in input sia la targa che la relativa nota

Codice:
[{"targa":"AA123AA","nota":"Questa è una nota di test"},{"targa":"AA124AA","nota":""},{"targa":"AA125AA","nota":""},{"targa":"AA126AA","nota":""},{"targa":"AA127AA","nota":""},{"targa":"AA128AA","nota":""},{"targa":"AA129AA","nota":""},{"targa":"AA130AA","nota":""},{"targa":"AA131AA","nota":""},{"targa":"AA132AA","nota":""},{"targa":"AA133AA","nota":""},{"targa":"AA134AA","nota":""},{"targa":"AA135AA","nota":""},{"targa":"AA136AA","nota":""}]

Per cuorisità, considerato che a me non funziona, la query della pagina modifica.php scritta in questo modo secondo te è corretta ?

PHP:
<?php
$host = 'localhost';
$user = 'usermysql';
$pass = 'passwordmysql';
$db = 'databasemysql';
$mysqli = new mysqli("$host", "$user", "$pass", "$db");
$data = json_decode(stripslashes($_REQUEST['notes']));
foreach($data as $a){
$targa = $a->targa;
$noterev = $a->noterev;
$query=mysqli_query($mysqli, "UPDATE commesse SET noterev='$noterev' WHERE targa='$targa'");
}
if($query){
echo "ok";
}else{
echo "errore";
}
?>
 
Prova cosi:
PHP:
<?php
$host = 'localhost';
$user = 'usermysql';
$pass = 'passwordmysql';
$db = 'databasemysql';
$mysqli = new mysqli("$host", "$user", "$pass", "$db");
$data = $_REQUEST['notes'];
$array = json_decode($data, true);
for($i=0;$i<count($array);$i++){
$targa = $array[$i]['targa'];
$nota = $array[$i]['nota'];
$query=mysqli_query($mysqli, "UPDATE commesse SET noterev='$nota' WHERE targa='$targa'");
}
echo "ok";
?>
 

Discussioni simili