简体   繁体   English

我想用我从数据库中使用Ajax从另一个数据库文件中获取的数据填充表单元素

[英]I want to populate my form elements with data i fetch from my database in another php file using ajax

I am trying to populate my form elements with a row fetched from a database. 我试图用从数据库中获取的行填充表单元素。 I am trying to use ajax to send the id of the row i want to fetch when a button is clicked. 我试图使用ajax发送我想要在单击按钮时获取的行的ID。

I am trying to use ajax. 我正在尝试使用Ajax。 I have successfully retrieved the row i want in the php file but cant send the data back to the requesting page. 我已经成功检索了我想要的php文件中的行,但无法将数据发送回请求页面。

My table has edit and delete buttons for each row. 我的表格的每一行都有编辑和删除按钮。

while ($row = mysqli_fetch_array($result)) {
    echo '<tr style="margin:10px;">';
    echo "<td>".$row['title']."</td>";
    echo "<td>".$row['uname']."<td>";
    echo '<td><button class="btn btn-warning" onClick="editBtnClicked('.$row['id'].')">Edit</button><td>';
    echo '<td><button class="btn btn-danger" onClick="deleteBtnClicked('.$row['id'].')">Delete</button><td>';
    echo "</tr>";
}

And my script looks like this: 我的脚本如下所示:

function editBtnClicked(id) {
    $.ajax({
        type: 'post',
        url: 'edit.php',
        data: { edit_id: id },
        success: function (data) {
            // ...
        }
    })
}

And my edit.php file: 和我的edit.php文件:

$id= $_POST['edit_id'];

$stmt= "SELECT * FROM blog WHERE id=".$id;

$result= $conn->query($stmt);

$rows = $result->fetch_assoc();

I want to send $rows['name'] and $rows['body'] to the page that contains the table and populate a form that has two text areas with it. 我想将$rows['name']$rows['body']发送到包含表的页面,并填充具有两个文本区域的表单。

At the end of your edit.php file, add this line edit.php文件的末尾,添加以下行

echo json_encode($rows);

Modify your function like this: (I am assuming that your name and body textareas have ids nameTextArea and bodyTextArea respectively.) 像这样修改您的函数:(我假设您的姓名和正文textareas分别具有ids nameTextAreabodyTextArea 。)

function editBtnClicked(id) {
    $.ajax({
        type: 'post',
        url: 'edit.php',
        data: { edit_id: id },
        success: function (data) {
            console.log(data);
            $('#nameTextArea').val(data.name);
            $('#bodyTextArea').val(data.body);
        }
    })
}

暂无
暂无

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

相关问题 如何从存储在我 PC 上的文件中获取 XML 数据并使用 javascript 以 HTML 格式填充表格? - How can I fetch XML Data from a file stored on my PC and populate a table in HTML using javascript? 我想用所选项目的数据填充我的编辑表单 - i want to populate my edit form with the data of the selected item 如何从MySQL数据库中提取数据以在页面加载时预先填充表单? - How do I pull data from my MySQL database to pre populate a form on page load? 我想使用我的私人 API 来获取我的数据,但我希望它们是复选框 - I want, using my private API, to fetch my data but I want them to be checkboxes 我想使用 ajax 和 php 以表格形式检索数据 - I want to retrive data in tabular form using ajax and php 如何使用变量中的数据填充sql数据库? - How can I populate my sql database with data from a variable? 使用Ajax,如何将表单中的下拉菜单选择值与数据库中的记录进行匹配,并获取关联的记录/行以预填表单? - Using Ajax how can I match a drop down menu selection value from a form to records in my database and fetch associated record/row to prefill a form? 我正在尝试使用PHP中的Ajax从数据库中获取图像 - I am trying to fetch image from database using ajax in php 我想在页面加载时自动从我的代码文件夹中获取一个文件到我的 js 文件中 - I want to fetch a file from my code folder to my js file automatically when the page loads 我想将数据库中的数据与googlelocation下拉菜单进行匹配 - I want to match data from my database to googlelocation dropdown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM