简体   繁体   English

与其将表中的MySQL数据显示为HTML表,不如将其显示在可编辑的文本框中

[英]Instead of displaying MySQL Data from tables into HTML tables, display it on editable text boxes

I've managed to import data from my database table into an HTML table, however, I'd like to make them in locked text boxes and when an "edit" button is pressed, the corresponding text box will be unlocked, data can be changed and updated in the MySQL tables? 我已经设法将数据库表中的数据导入到HTML表中,但是,我想将它们放置在锁定的文本框中,并且当按下“编辑”按钮时,相应的文本框将被解锁,数据可以在MySQL表中更改和更新了? How can I do this? 我怎样才能做到这一点?

I've researched things for similar ways of putting them into text boxes however I did not find any clear answer. 我已经研究了将它们放入文本框的类似方法,但是没有找到明确的答案。

$result = mysqli_query($con,"SELECT * FROM dados");

echo "<table border='2'>
<tr>
<th>nome</th>
<th>Idade</th>
<th>Email</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['nome'] . "</td>";
echo "<td>" . $row['idade'] . "</td>";
echo "<td>" . $row['email'] . "</td>";

echo "</tr>";
}
echo "</table>";

This is how the table is showing up on my PHP file, and everything shows fine, however, I'd like for each field to be a locked text box that on press of a button "edit" can be edited. 这就是表格在我的PHP文件上显示的方式,并且一切正常,但是,我希望每个字段都是一个锁定的文本框,单击“编辑”按钮即可对其进行编辑。

you can create new column with tow icon one for delete and other for edit when clicked on that button a javascript function will start and give it the id of that row it will use ajax to query and get the data of that row and open a modal to edit and you can use ajax to save data. 您可以创建带有拖曳图标的新列,当单击该按钮时,一个用于删除,另一个用于编辑,一个javascript函数将启动并为其提供该行的ID,它将使用ajax查询并获取该行的数据并打开模式编辑,您可以使用ajax保存数据。

let's go step by step : 让我们一步一步走:

1- add new column 1-添加新列

<?php
$result = mysqli_query($con, "SELECT * FROM dados");
echo "<table border='2'>
<tr>
<th>nome</th>
<th>Idade</th>
<th>Email</th>
<th>Edit</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['nome'] . "</td>";
echo "<td>" . $row['idade'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td><i class=\"fas fa-clock\" data-toggle="modal" data- 
target="#exampleModal" onclick=\"getDataForEdit(" . $row['id'] . 
")\"></i></td>";
echo "</tr>";
}
?>

2- create a modal to insert the data we will get from getDataForEdit function 2-创建一个模式以插入我们将从getDataForEdit函数获取的数据

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria- 
labelledby="exampleModalLabel"
 aria-hidden="true">
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria- 
label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <div class="modal-body">
            <div class="form-group">
                <form action="" method="post">
                    @csrf
                    <div class="md-form mb-4">
                        <label data-error="wrong" data-success="right" 
for="defaultForm-pass">title
                        </label>
                        <div class="row">
                            <div class="col-8">
                                <input type="text" name="name" id="defaultForm- 
pass"
                                       class="form-control validate">
                            </div>
                            <div class="col-2">
                                <input type="text" name="tag" id="defaultForm- 
pass-email"
                                       class="form-control validate">
                            </div>
                            <div class="col-2">
                                <button type="submit" class="btn btn- 
primary">submit</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data- 
dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save 
changes</button>
        </div>
    </div>
</div>
</div>

3- so now we should code javascript first we ajax and get the data of the row we want to edit then we insert that in the modal 3-所以现在我们应该先编写JavaScript代码,然后使用ajax并获取要编辑的行的数据,然后将其插入到模态中

<script>
function getDataForEdit(id) {
    var response = null;
    return $.ajax({
        type: 'GET',
        url: 'myanswerpage.php?id=' + id,
        success: function (data) {
            insertData(data.name,data.email);
        },
        error: function () {
            alert('getting data was not successful');

        }
    });
}
</script>

<script>
function insertData(name,email) {
    document.getElementById('defaultForm-pass').value = name;
    document.getElementById('defaultForm-pass-email').value = email;
}
</script>

so when ever you click the icon for edit a modal will open with the information of the row you want to edit. 因此,无论何时单击编辑图标,都会打开一个模态,其中包含要编辑的行的信息。

You can use forms instead of tables and fetch all data and keep all the data in input tag as disabled. 您可以使用表格而不是表格来获取所有数据,并将所有数据保留在输入标记中为禁用状态。

By using jquery click event, you can make it editable and then write whatever you want and save it. 通过使用jquery click事件,可以使其可编辑,然后编写所需的任何内容并保存。 This data on saving should be edited in database by including php and firing mysql update query. 有关保存的数据,应在数据库中通过添加php和触发mysql更新查询来进行编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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