![](/img/trans.png)
[英]How to pass a MySQL data from PHP to Javascript through a function
[英]How to pass a mysql database data to a javascript function using php?
我正在创建一个 web,人们可以在其中添加粉丝群并通过单击 X 按钮将其删除。 这就是我想要的:当用户点击 X 按钮时,会弹出一个模态框说“你确定”,当他按下模态框上的删除按钮时,数据将从数据库中删除(我使用的是 mysql数据库)。
当用户点击 X 按钮时,我希望模态框显示用户想要删除的粉丝群的具体名称。 但我不知道如何将粉丝群的 id(来自我的数据库)传递给我制作和使用的 function。 这是它的样子:
来自“welcome.php”的模态
<div class="modal fade" id="delmdl">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Delete Fanbase</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<form id="" method="post" action="deletefanbase.php">
<div class="modal-body">
<h5>Are you sure you want to delete this fanbase?
<span><!-- this is where the fanbase's name should be --></span>
</h5>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button name="addfbmdl" type="submit" class="btn btn-danger" >Delete</button>
</div>
</form>
</div>
</div>
</div>
从“showfanbase.php”迭代添加粉丝群 div 的代码
<?php
require "session.php";
require "connect.php";
if(!isset($_SESSION["loggedin"])||$_SESSION["loggedin"]!==true){
header("location: login.php");
exit;
}
$sql="SELECT fanroom.id AS fr_id, fanroom.uid AS uid, fanroom.aid AS aid, fanroom.name AS fr_name,
fanroom.description AS fr_desc,
artists.id, artists.name AS a_name, users.username,users.id AS user_id
FROM fanroom INNER JOIN artists ON fanroom.aid=artists.id INNER JOIN users ON fanroom.uid=users.id
ORDER BY fanroom.created_at DESC";
$result=$link->query($sql);
if($result){
$output="<div class='container' style='margin:15px;'>";
if($result->num_rows>0){
while($row = $result->fetch_assoc()){
if($row['user_id']==$_SESSION['id']){
$username="You";
$output.="<div class='row'>
<div class='col border' style='margin:10px;border-radius:10px;padding:15px;'>
<h3>".$row['fr_name']."</h3>
<p style='color:grey;'>Made by <b>".$username."</b></p>
<h5 style='color:grey;'>A <b>".$row['a_name']."'s</b> Fanbase</h5>
<p>".$row['fr_desc']."</p>
<button id='delete' type='button' class='btn btn-danger'
onclick='deletefunction(".$row['fr_id'].")' >x</button> #this is the X button
</div>
</div>";
}
else{
$username=$row['username'];
$output.="<div class='row'>
<div class='col border' style='margin:10px;border-radius:10px;padding:15px;'>
<h3>".$row['fr_name']."</h3>
<p style='color:grey;'>Made by <b>".$username."</b></p>
<h5 style='color:grey;'>A <b>".$row['a_name']."'s</b> Fanbase</h5>
<p>".$row['fr_desc']."</p>
</div>
</div>";
}
}
$output.="</div>";
echo $output;
}
}?>
和该按钮的 onclick 将执行“Welcome.php”上的 function (deletefunction())。 在里面我传递了即将被删除的粉丝群的 ID。 这个 function 假设只显示模态,但我想让这个 function 可以保存即将被删除的粉丝群 id。
<script>
function deletefunction(del_id){
$('#delmdl').modal();}
</script>
我必须在我的代码中添加什么,以便我可以在模式上显示粉丝群的名称,然后我可以将该粉丝群的 id 传递给 deletefanbase.php(这是用于删除 mysql 数据库中的数据)?
您应该采取一个隐藏字段的形式将该值发布到deletefanbase.php
在welcome.php
中取一个隐藏字段,例如
<input type='hidden' name='delbyid' id='delbyid value="">
当 function 在您提交表单之前执行 set delete id 到隐藏字段时
$('#delbyid').val(del_id);
它将在 deletefanbase.php 上使用 $_POST['delbyid'] 发布该字段的值,您可以执行查询以删除具有该 ID 的特定记录
//you need to make connection for database or include database connection file before run query
$delid=$_POST['delbyid'];
$sql="DELETE FROM table_name WHERE id = $delid ";
$result=$link->query($sql);
if($result){
echo "delete successfully";
}
感谢您的回复,它的工作原理。 我在 Welcome.php 上添加了 ajax 以获取模态上的粉丝群名称。 我发现您不能将 javascript 变量直接传递给 php 变量。
所以,它是这样的:
来自“welcome.php”的模态
<div class="modal fade" id="delmdl">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Delete Fanbase</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<form id="" method="post" action="deletefanbase.php">
<div class="modal-body">
<h5>Are you sure you want to delete this fanbase?</h5>
<p>Fanbase name: <span id="fbname"></span></p>
<input type='hidden' name='delbyid' id='delbyid' value="">
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button name="addfbmdl" type="submit" class="btn btn-danger" >Delete</button>
</div>
</form>
</div>
</div>
</div>
欢迎使用 function。php
function deletefunction(del_id){
$('#delmdl').modal();
$('#delbyid').val(del_id);
$.ajax({
url:"getfbname.php",
type:"POST",
data:({fb_id:del_id}),
success:function(response){
$('#fbname').html(response);
}
});
}
getfbname.php,这是 php 让粉丝群名称显示在模态上
<?php
require "session.php";
require "connect.php";
if(!isset($_SESSION["loggedin"])||$_SESSION["loggedin"]!==true){
header("location: login.php");
exit;
}
$id=$_POST['fb_id'];
$sql="SELECT name FROM fanroom WHERE id=$id";
$result=$link->query($sql);
if($row=$result->fetch_assoc()){
echo $row['name'];
}?>
再次感谢您的回答,对我帮助很大!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.