简体   繁体   中英

Loading data from a SQL database in a modal

I am currently displaying data in a table via a foreach loop and I want to be able to click a edit button in a specific row and it open a modal dialog which fetches additional data from a table based on the ID of that row?

<?php
foreach ($db->query('SELECT * FROM websitehosting ORDER BY CUSTOMERNAME ASC') as $row)
{
    $date1 = $row['SERVICERENEWALDATE'];
    $myDateTime = DateTime::createFromFormat('Y-m-d H:i:s', $date1);
    $formattedrenewaldate = $myDateTime->format('d-M-Y');
    $hello = $row['ID'];

    // echo out the contents of each row into a table
    echo "<tr>";
    echo '<td>' . $hello . '</td>';
    echo '<td>' . $row['CUSTOMERNAME'] . '</td>';
    echo '<td>' . $row['DOMAINNAME'] . '</td>';
    echo '<td>' . $formattedrenewaldate . '</td>';
    echo '<td>' . $row['STATUS'] . '</td>';
    echo '<td><button name="submitid" class="btn btn-info footable-edit edit" id=' . $row['ID'] . ' data-toggle="modal" data-target="#myModal" >Info</button></td>';
    echo '<td>' . $row['UPDATEDBY'] . '</td>';
    echo '<td><a class="btn btn-success" onclick="my1Function()">Edit</a></td>';
    echo "</tr>";
}

// close table>
echo "</table>";
?>

Currently the ID of each row is set to the ID of the edit button but I cant get the data below to adjust to the id so it displays the correct data. Obviously at the moment it is set to 26 but I want it to be set to the ID of the button or just the ID of that row. Eventually I want to be able to edit the data but for now I am just displaying it

<div id="myModal" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Service Information</h4>
            </div>
            <div class="modal-body">
                <?php
                $dummy = '26';
                foreach ($db->query("SELECT * FROM websitehosting WHERE ID = '$dummy'") as $row)
                {
                    $date = $row['SIGNUPDATE'];
                    $test = $row['ID'];
                    $myDateTime = DateTime::createFromFormat('Y-m-d H:i:s', $date);
                    $formattedsignupdate = $myDateTime->format('d-M-Y');

                    $date1 = $row['SERVICERENEWALDATE'];
                    $myDateTime1 = DateTime::createFromFormat('Y-m-d H:i:s', $date1);
                    $formattedrenewaldate = $myDateTime1->format('d-M-Y');
                    ?>
                    <table><?php echo $hello; ?>
                        <tr>
                            <td><b>ID:</b></td>
                            <td><label id="hello"></label></td>
                        </tr>
                        <tr>
                            <td style="width:150px;"><b>Customer:</b></td>
                            <td><?php echo $row['CUSTOMERNAME']; ?></td>
                        </tr>
                        <tr>
                            <td style="width:150px;"><b>Domain Name:</b></td>
                            <td><?php echo $row['DOMAINNAME']; ?></td>
                        </tr>
                        <tr>
                            <td style="width:150px;"><b>Signup Date:</b></td>
                            <td><?php echo $formattedsignupdate; ?></td>
                        </tr>
                        <tr>
                            <td style="width:150px;"><b>Renewal Date:</b></td>
                            <td><?php echo $formattedrenewaldate; ?></td>
                        </tr>
                        <tr>
                            <td style="width:150px;"><b>Hosting Type:</b></td>
                            <td><?php echo $row['HOSTINGTYPE']; ?></td>
                        </tr>
                        <tr>
                            <td style="width:150px;"><b>Current Server:</b></td>
                            <td><?php echo $row['ACTIVESERVER']; ?></td>
                        </tr>
                        <tr>
                            <td style="width:150px;"><b>Website Type:</b></td>
                            <td><?php echo $row['WEBSITETYPE']; ?></td>
                        </tr>
                        <tr>
                            <td style="width:150px;"><b>Diskspace:</b></td>
                            <td><?php echo $row['DISKSPACE']; ?>GB</td>
                        </tr>
                        <tr>
                            <td style="width:150px;"><b>Bandwidth:</b></td>
                            <td><?php echo $row['BANDWIDTH']; ?>GB</td>
                        </tr>
                        <tr>
                            <td style="width:150px;"><b>Annual Cost:</b></td>
                            <td>£<?php echo $row['ANNUALCOST']; ?></td>
                        </tr>
                        <tr>
                            <td style="width:150px;"><b>Comments:</b></td>
                            <td><?php echo $row['COMMENTS']; ?></td>
                        </tr>
                    </table>
                <?php } ?>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

you can create js function like edit_row(var row_id) and in html table you can add

<td>
<button onclick="edit_row(<?=$row['id']?>)">edit</button>
</td>

and the end of page you can add html modal like this http://jsfiddle.net/h3WDq/ and the body of js function it should be like this

function like edit_row(var row_id)
{
// get data with ajax 
$.ajax({
            url: "get_data.php",
             type:"POST",
            data : {id:row_id},
            dataType: "JSON",
            success: function(result){
// here you can set modal inputs value from result 
//finally you can run modal

    }});
}

after this you need to post modal form values you can do it with ajax request or normal form post .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM