我正在尝试使用 ajax 从数据库中检索数据并以引导模式中的形式填充
Posted
技术标签:
【中文标题】我正在尝试使用 ajax 从数据库中检索数据并以引导模式中的形式填充【英文标题】:I am trying to retrieve data from a database using ajax and populate in form that is in a bootstrap modal 【发布时间】:2019-09-13 06:32:58 【问题描述】:当我单击下图中的编辑按钮时,我需要帮助
和编辑模态图像
这是主页的代码
<?php require_once("connect.php"); ?>
<?php require_once("fetch.php"); ?>
<?php require_once("select.php"); ?>
<?php require_once("process.php"); ?>
<!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">
<title>Home</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="css/style.css">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-8 col-sm-offset-2">
<ul class="nav nav-pills">
<li><a href="#">Home</a></li>
</ul>
<button type="button" class="btn btn-primary pull-right" data-toggle="modal" data-target="#myModal">Add</button>
<table class="table" id="table">
<tr>
<th>S/N</th>
<th>Name</th>
<th>Email</th>
<th>Description</th>
<th>Edit</th>
<th>View</th>
<th>Delete</th>
</tr>
<?php
$query = "SELECT * FROM contact";
$runQuery = mysqli_query($connect, $query);
if(!$runQuery)
die("Could not run select query ".mysqli_error($connect));
$sn = 1;
while($row = mysqli_fetch_assoc($runQuery)) ?>
<tr>
<td><?php echo $sn++ ?></td>
<td><?php echo $row["name"] ?></td>
<td><?php echo $row["email"] ?></td>
<td><?php echo $row["description"] ?></td>
<td>
<button type="button" data-toggle="modal" data-target="#myModal" class="btn btn-warning btn-sm edit-data" id="<?php echo $row['userid'] ?>">Edit</button>
</td>
<td>
<button type="button" data-target="#viewModal" data-toggle="modal" class="btn btn-primary btn-sm view-data" id="<?php echo $row['userid'] ?>">View</button>
</td>
<td>
<button type="button" class="btn btn-danger btn-sm del-data" id="<?php echo $row['userid'] ?>">Danger</button>
</td>
</tr>
<?php ?>
</table>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add Data</h4>
</div>
<div class="modal-body">
<div id="success" class="alert alert-success"></div>
<form class="form" method="POST" id="insertData">
<div id="nameError" class="alert alert-danger"></div>
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name" id="name">
</div>
<div id="emailError" class="alert alert-danger"></div>
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" name="email" id="email">
</div>
<input type="hidden" name="userid" id="contactUserId">
<div id="descriptionError" class="alert alert-danger"></div>
<div class="form-group">
<label>Description</label>
<textarea name="description" id="description" class="form-control"></textarea>
</div>
<input type="submit" class="btn btn-primary" value="Add" name="add" id="add">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<div id="viewModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">View Data</h4>
</div>
<div class="modal-body" id="contactDetail">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/myscript.js"></script>
</body>
</html>
此代码用于在数据库process.php中输入和更新数据
<?php
if(!empty($_POST))
$name = $_POST["name"];
$email = $_POST["email"];
$description = $_POST["description"];
$userid = $_POST["userid"];
if($userid != '')
$query = "UPDATE contact SET name = '$name', email='$email', description='$description' WHERE userid = $userid ";
$runQuery = mysqli_query($connect, $query);
if(!$runQuery)
die("Could not run query ".mysqli_error($connect));
else
$query = "INSERT INTO contact(name, email, description) VALUES ";
$query .=" ('$name', '$email', '$description')";
$runQuery = mysqli_query($connect, $query);
if(!$runQuery)
die("Could not run insert query ".mysqli_error($connect));
此代码从数据库中检索数据
<?php
if(isset($_POST['userid']))
$editId = (int)$_POST['userid'];
$query = "SELECT * FROM contact WHERE userid = $editId";
$runQuery = mysqli_query($connect, $query);
if(!$runQuery)
die("Could not run query ".mysqli_error($connect));
$getData = mysqli_fetch_assoc($runQuery);
echo json_encode($getData);
这是我的 js 文件
$(document).ready(function()
$("#insertData").submit(function(event)
event.preventDefault();
if($("#name").val() == "")
$("#nameError").css("display", "block").html("Name is required");
else if ($("#email").val() == "")
$("#emailError").css("display", "block").html("Email is required");
else if ($("#description").val() == "")
$("#descriptionError").css("display", "block").html("Description is required");
else
$.ajax(
url: "index.php",
method: "POST",
data: $("#insertData").serialize(),
success: function(data)
$("#success").css("display", "block").html("Data added");
$("#insertData")[0].reset();
);
);
$(document).on("click", ".edit-data", function()
var contactId = $(this).attr("id");
$.ajax(
url:"index.php",
method: "POST",
data: contactId:contactId,
dataType: "json",
success: function(data)
$("#name").val(data.name);
$("#email").val(data.email);
$("#description").val(data.description);
$("#contactUserId").val(data.userid);
$("#add").val("Update");
$("#myModal").modal("show");
);
);
$(document).on('click', '.view-data', function()
var contactId = $(this).attr("id");
if(contactId != '')
$.ajax(
url:"index.php",
method:"POST",
data:contactId:contactId,
success:function(data)
$('#contactDetail').html(data);
$('#viewModal').modal('show');
);
);
);
【问题讨论】:
你尝试的方式非常混乱,IMO。你只需要这样做。 HTML/CSS 没问题。单击编辑按钮,您可以显示模态窗口并进行ajax调用,它将读取数据库,然后返回数据。然后您可以使用 jquery 将每个值设置为其各自的字段 您正在对数据进行编码,但我找不到任何解码它的地方。在ajax成功时,“data = JSON.parse(data);”将此作为您的第一行,然后尝试。它可能会解决您的问题。 @Vishwa 感谢您的努力,如果您给我看一个代码 sn-p,我将不胜感激 @Rishi 非常感谢您的回复我试过了,但还是不行,我正在尝试复制我从 youtube 视频youtube.com/watch?v=SpyVEbFQ6Bc&feature=youtu.be 中学到的东西@ 你能不能也缩短你的代码,只粘贴不起作用的部分。 【参考方案1】:您正在发送contactId
,并且在您的PHP 中您正在寻找userid
。将您的数据更改为data: userid: userid
。
$(document).on("click", ".edit-data", function()
var userid = $(this).attr("id");
$.ajax(
url: "index.php",
method: "POST",
data: userid: userid ,
dataType: "json",
success: function(data)
$("#name").val(data.name);
$("#email").val(data.email);
$("#description").val(data.description);
$("#contactUserId").val(data.userid);
$("#add").val("Update");
$("#myModal").modal("show");
);
);
【讨论】:
感谢您的回复它仍然没有工作我认为问题来自数据:contactId : contactId 我还没有理解这个概念。 是的,将data: contactId : contactId
更改为data: userid: userid
,就像我说的那样,因为您的PHP 正在寻找$_POST['userid'])
而您正在发送contactId
并确保index.php
不包含html代码。以上是关于我正在尝试使用 ajax 从数据库中检索数据并以引导模式中的形式填充的主要内容,如果未能解决你的问题,请参考以下文章
jquery-无法从我通过ajax调用获得的对象类型响应中检索数据