Come passare una variabile al modal di bootstrap

francesco_mendola

Nuovo Utente
2 Mar 2020
1
0
1
Salve, ho un problema con il codice che c'è sotto, non riesco a passare la variabile id dal form del td al modal con i dettagli del td della tabella.
L'errore che ottengo è: Notice: Undefined index: id in E:\xampp\htdocs\dcg\pages\tickets-assistenza.php on line 38
Come risolvo? Grazie in anticipo.

Il codice è in un'unico file php:
PHP:
<?php
    session_start();
    
    if (!(isset($_SESSION['autorizzato']) && $_SESSION['autorizzato'] == true)) {
        header("Location: login.php");
    }

    $nomepagina = 'sezioni';

    $conn = mysqli_connect('localhost', 'root', '', 'gestione');
    if (!$conn) {
        die("Connessione non riuscita: ".mysqli_connect_error());
    }
    
    function prendiTickets($conn){
        $sql = "SELECT * FROM tickets";
        $result = $conn->query($sql);
        while($row = $result->fetch_assoc()){
            echo "<form method='POST' action='".strumentiRisposta($conn)."'>";
                echo "<tr class='odd gradeX'>";
                    echo "<td>".$row['id']."</td>";
                    echo "<td>".$row['cognomenome']."</td>";
                    echo "<td>".$row['oggetto']."</td>";
                    echo "<input type='hidden' name='id' value='".$row['id']."'>";
                    echo "
                        <td class='text-right'>
                            <button type='submit' name='dettagli_strumenti' class='btn btn-primary btn-xs' data-toggle='modal' data-target='#modaldettagli'><i class='fa fa-cogs'></i>
                                &nbsp;&nbsp;Dettagli e strumenti
                            </button>
                        </td>
                    ";
                echo "</tr>";
            echo "</form>";
           }
    }
    
    function strumentiRisposta($conn){
            $id = $_POST['id'];
            echo "
                <div class='modal fade' id='modaldettagli' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>
                    <div class='modal-dialog' role='document'>
                        <div class='modal-content'>
                            <div class='modal-header'>
                                <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
                                <h4 class='modal-title' id='myModalLabel'>Dettagli e strumenti per ticket n. ".$id."</h4>
                            </div>
                            <form id='risposta'>
                                <div class='modal-body'>
                                    <p>
                                        <b>Mittente: </b>Cognome Nome ([email protected])<br>
                                        <b>Invio: </b>00/00/0000 00:00<br>
                                        <b>Oggetto: </b>Oggetto<br>
                                    </p>
                                    <div class='form-group'>
                                        <label>Messaggio</label>
                                        <textarea class='form-control' rows='3' style='resize: none;' disabled>Messaggio completo</textarea>
                                    </div>
                                    <br>
                                    <div class='form-group'>
                                        <label>Risposta</label>
                                        <textarea class='form-control' rows='3' required style='resize: none;' placeholder='Inserisci il messaggio di risposta'></textarea>
                                    </div>
                                </div>
                            </form>
                            <div class='modal-footer'>
                                <button type='button' class='btn btn-default btn-sm' data-dismiss='modal'>Chiudi</button>
                                <button type='submit' name='submit' form='risposta' class='btn btn-success btn-sm'>Invia risposta</button>
                                <button type='submit' name='' class='btn btn-danger btn-sm'>Elimina</button>
                            </div>
                        </div>
                    </div>
                </div>
            ";
    }

    /*function eliminaProfilo($conn){
        if(isset($_POST['eliminaProfiloBTN'])){
            $_POST['id'] = $id;
            $sql = "DELETE FROM profili WHERE id='$id'";
            $result = $conn->query($sql);
            //echo "<meta http-equiv='refresh' content='0'>";
        }
    }*/
?>

<!DOCTYPE html>
<html lang="en">
    <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="">
        <meta name="author" content="">
        <title>Tickets assistenza - CGAD</title>
        <link href="../css/bootstrap.min.css" rel="stylesheet">
        <link href="../css/metisMenu.min.css" rel="stylesheet">
        <link href="../css/startmin.css" rel="stylesheet">
        <link href="../css/font-awesome.min.css" rel="stylesheet" type="text/css">
        <link href="../css/dataTables/dataTables.bootstrap.css" rel="stylesheet">
        <link href="../css/dataTables/dataTables.responsive.css" rel="stylesheet">
        <link rel="shortcut icon" href="../img/favicon.png">
    </head>
    <body>
        <?php include('../php/navbar.php'); ?>
        <div id="wrapper">
            <!--div class="modal fade" id="modaldettagli" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            <h4 class="modal-title" id="myModalLabel">Dettagli e strumenti per ticket n. 0</h4>
                        </div>
                        <form id="risposta">
                            <div class="modal-body">
                                <div class="form-group">
                                    <label>Text area</label>
                                    <textarea class="form-control" rows="3" required></textarea>
                                </div>
                            </div>
                        </form>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default btn-sm" data-dismiss="modal">Chiudi</button>
                            <button type="submit" name="submit" form="risposta" class="btn btn-success btn-sm">Rispondi</button>
                            <button type="submit" name="" class="btn btn-danger btn-sm">Elimina</button>
                        </div>
                    </div>
                </div>
            </div-->
            <div id="page-wrapper">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-lg-12">
                            <h1 class="page-header">Tickets assistenza</h1>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-12">
                            <div class="panel panel-default">
                                <div class="panel-heading">Tickets aperti</div>
                                <div class="panel-body">
                                    <div class="table-responsive">
                                        <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                            <thead>
                                                <tr>
                                                    <th>#</th>
                                                    <th>Cognome e nome</th>
                                                    <th>Oggetto</th>
                                                    <th>Azioni</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php prendiTickets($conn); ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <script src="../js/jquery.min.js"></script>
        <script src="../js/bootstrap.min.js"></script>
        <script src="../js/metisMenu.min.js"></script>
        <script src="../js/startmin.js"></script>
        <script src="../js/dataTables/jquery.dataTables.min.js"></script>
        <script src="../js/dataTables/dataTables.bootstrap.min.js"></script>
        <script>
            $(document).ready(function() {
                $('#dataTables-example').DataTable({
                        responsive: true
                });
            });
        </script>
    </body>
</html>
 

max1974

Utente Attivo
7 Mar 2013
107
0
16
Salve, ho un problema con il codice che c'è sotto, non riesco a passare la variabile id dal form del td al modal con i dettagli del td della tabella.
L'errore che ottengo è: Notice: Undefined index: id in E:\xampp\htdocs\dcg\pages\tickets-assistenza.php on line 38
Come risolvo? Grazie in anticipo.

Il codice è in un'unico file php:
PHP:
<?php
    session_start();
   
    if (!(isset($_SESSION['autorizzato']) && $_SESSION['autorizzato'] == true)) {
        header("Location: login.php");
    }

    $nomepagina = 'sezioni';

    $conn = mysqli_connect('localhost', 'root', '', 'gestione');
    if (!$conn) {
        die("Connessione non riuscita: ".mysqli_connect_error());
    }
   
    function prendiTickets($conn){
        $sql = "SELECT * FROM tickets";
        $result = $conn->query($sql);
        while($row = $result->fetch_assoc()){
            echo "<form method='POST' action='".strumentiRisposta($conn)."'>";
                echo "<tr class='odd gradeX'>";
                    echo "<td>".$row['id']."</td>";
                    echo "<td>".$row['cognomenome']."</td>";
                    echo "<td>".$row['oggetto']."</td>";
                    echo "<input type='hidden' name='id' value='".$row['id']."'>";
                    echo "
                        <td class='text-right'>
                            <button type='submit' name='dettagli_strumenti' class='btn btn-primary btn-xs' data-toggle='modal' data-target='#modaldettagli'><i class='fa fa-cogs'></i>
                                &nbsp;&nbsp;Dettagli e strumenti
                            </button>
                        </td>
                    ";
                echo "</tr>";
            echo "</form>";
           }
    }
   
    function strumentiRisposta($conn){
            $id = $_POST['id'];
            echo "
                <div class='modal fade' id='modaldettagli' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>
                    <div class='modal-dialog' role='document'>
                        <div class='modal-content'>
                            <div class='modal-header'>
                                <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
                                <h4 class='modal-title' id='myModalLabel'>Dettagli e strumenti per ticket n. ".$id."</h4>
                            </div>
                            <form id='risposta'>
                                <div class='modal-body'>
                                    <p>
                                        <b>Mittente: </b>Cognome Nome ([email protected])<br>
                                        <b>Invio: </b>00/00/0000 00:00<br>
                                        <b>Oggetto: </b>Oggetto<br>
                                    </p>
                                    <div class='form-group'>
                                        <label>Messaggio</label>
                                        <textarea class='form-control' rows='3' style='resize: none;' disabled>Messaggio completo</textarea>
                                    </div>
                                    <br>
                                    <div class='form-group'>
                                        <label>Risposta</label>
                                        <textarea class='form-control' rows='3' required style='resize: none;' placeholder='Inserisci il messaggio di risposta'></textarea>
                                    </div>
                                </div>
                            </form>
                            <div class='modal-footer'>
                                <button type='button' class='btn btn-default btn-sm' data-dismiss='modal'>Chiudi</button>
                                <button type='submit' name='submit' form='risposta' class='btn btn-success btn-sm'>Invia risposta</button>
                                <button type='submit' name='' class='btn btn-danger btn-sm'>Elimina</button>
                            </div>
                        </div>
                    </div>
                </div>
            ";
    }

    /*function eliminaProfilo($conn){
        if(isset($_POST['eliminaProfiloBTN'])){
            $_POST['id'] = $id;
            $sql = "DELETE FROM profili WHERE id='$id'";
            $result = $conn->query($sql);
            //echo "<meta http-equiv='refresh' content='0'>";
        }
    }*/
?>

<!DOCTYPE html>
<html lang="en">
    <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="">
        <meta name="author" content="">
        <title>Tickets assistenza - CGAD</title>
        <link href="../css/bootstrap.min.css" rel="stylesheet">
        <link href="../css/metisMenu.min.css" rel="stylesheet">
        <link href="../css/startmin.css" rel="stylesheet">
        <link href="../css/font-awesome.min.css" rel="stylesheet" type="text/css">
        <link href="../css/dataTables/dataTables.bootstrap.css" rel="stylesheet">
        <link href="../css/dataTables/dataTables.responsive.css" rel="stylesheet">
        <link rel="shortcut icon" href="../img/favicon.png">
    </head>
    <body>
        <?php include('../php/navbar.php'); ?>
        <div id="wrapper">
            <!--div class="modal fade" id="modaldettagli" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            <h4 class="modal-title" id="myModalLabel">Dettagli e strumenti per ticket n. 0</h4>
                        </div>
                        <form id="risposta">
                            <div class="modal-body">
                                <div class="form-group">
                                    <label>Text area</label>
                                    <textarea class="form-control" rows="3" required></textarea>
                                </div>
                            </div>
                        </form>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default btn-sm" data-dismiss="modal">Chiudi</button>
                            <button type="submit" name="submit" form="risposta" class="btn btn-success btn-sm">Rispondi</button>
                            <button type="submit" name="" class="btn btn-danger btn-sm">Elimina</button>
                        </div>
                    </div>
                </div>
            </div-->
            <div id="page-wrapper">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-lg-12">
                            <h1 class="page-header">Tickets assistenza</h1>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-12">
                            <div class="panel panel-default">
                                <div class="panel-heading">Tickets aperti</div>
                                <div class="panel-body">
                                    <div class="table-responsive">
                                        <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                            <thead>
                                                <tr>
                                                    <th>#</th>
                                                    <th>Cognome e nome</th>
                                                    <th>Oggetto</th>
                                                    <th>Azioni</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php prendiTickets($conn); ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <script src="../js/jquery.min.js"></script>
        <script src="../js/bootstrap.min.js"></script>
        <script src="../js/metisMenu.min.js"></script>
        <script src="../js/startmin.js"></script>
        <script src="../js/dataTables/jquery.dataTables.min.js"></script>
        <script src="../js/dataTables/dataTables.bootstrap.min.js"></script>
        <script>
            $(document).ready(function() {
                $('#dataTables-example').DataTable({
                        responsive: true
                });
            });
        </script>
    </body>
</html>
Non so se ho capito bene cosa stai cercando.....
dalla tabella passare il valore id alla modal form ???
PHP:
$('#dataTables-example tbody').on('click', 'tr', function () {
                var table = $('#dataTables-example').DataTable();
                var data = table.row(this).data();
                alert('Mio id = '+data.id);
              });