Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it L'avatar di osgb
    Registrato dal
    May 2004
    residenza
    Monza
    Messaggi
    180

    Aggiornare record senza refresh

    Buonasera. Ho un codice che in teoria dovrebbe aggiornare un record dal db senza refresh della pagina.
    Non mi da nessun errore, ma non aggiorna neanche. Se qualcuno riesce a capire dove sta l'errore mi fa un piacere, grazie in anticipo.

    codice:
    ...       
    <td><input type="text" value="<?php echo $Cart03; ?>" id="<?php echo $Cart06; ?>"></td>
    <td><input type="text" value="<?php echo $Cart04; ?>" id="<?php echo $Cart06; ?>"></td>
    <td><button onclick="aggiornaDato(<?php echo $Cart06; ?>, <?php echo $Cart03; ?>, <?php echo $Cart04; ?>)">Aggiorna!</button></td>
    ...
    <script>
    function aggiornaDato(id, tess, cart) {
        fetch('update_Cartellino.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            body: `id=${id}&tess=${nTess}&cart=${nCart}`
        })
        .then(response => response.text())
        .then(data => {
            if(data === "success") {
                document.getElementById(id).innerText = nTess;
                alert("Dato aggiornato con successo!");
            }
        });
    }
    </script>
    
    ...

    "update_Cartellino.php"

    Codice PHP:
    <?php
    $id 
    $_POST['id'];
    $tess $_POST['tess'];
    $cart $_POST['cart'];
    $stmt $cndb->prepare("UPDATE `tabella` SET `Ntessera` = ?, `Cartellino` = ? WHERE `ID` = '" $id "'");
    $stmt->bind_param('i'$tess$cart);
    if (
    $stmt->execute()) {        echo "success";
    } else {        echo 
    "error";
    }
    $stmt->close();
    ?>

    Ultima modifica di osgb; 03-01-2026 a 19:05

  2. #2
    Utente di HTML.it
    Registrato dal
    Oct 2011
    Messaggi
    193
    codice HTML:
    ...
    <td><input type="text" value="<?php echo $Cart03; ?>" id="<?php echo $Cart06; ?>"></td>
    <td><input type="text" value="<?php echo $Cart04; ?>" id="<?php echo $Cart06; ?>"></td>
    <td><button onclick="aggiornaDato(<?php echo $Cart06; ?>, <?php echo $Cart03; ?>, <?php echo $Cart04; ?>)">Aggiorna!</button></td>
    ...
    <script>
    function aggiornaDato(id, tess, cart) {
        fetch('update_Cartellino.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            body: `id=${id}&tess=${tess}&cart=${cart}`
        })
        .then(response => response.text())
        .then(data => {
            if(data === "success") {
                document.getElementById(id).innerText = nTess;
                alert("Dato aggiornato con successo!");
            }
        });
    }
    </script>
    ...
    e
    Codice PHP:
    <?php
    if(isset($_POST['id'], $_POST['tess'], $_POST['cart'])) {
    $id $_POST['id'];
    $tess $_POST['tess'];
    $cart $_POST['cart'];
    $stmt $cndb->prepare("UPDATE `tabella` SET `Ntessera` = ?, `Cartellino` = ? WHERE `ID` = '" $id "'");
    $stmt->bind_param('i'$tess$cart);
    if (
    $stmt->execute()) {        echo "success";
    } else {        echo 
    "error";
    }
    $stmt->close();
    }
    } else {
    echo 
    'non atteso';
    }
    ?>
    Ultima modifica di darbula; 04-01-2026 a 14:31

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.