[英]How to pass a mysql database data to a javascript function using php?
I'm creating a web where people can add fanbase on it and delete it by clicking an X button.我正在创建一个 web,人们可以在其中添加粉丝群并通过单击 X 按钮将其删除。 Here's what I want: when a user click on the X button, a modal will pop-up saying "are you sure", and when he presses delete button on the modal, the data will be deleted from database (I'm using mysql database).这就是我想要的:当用户点击 X 按钮时,会弹出一个模态框说“你确定”,当他按下模态框上的删除按钮时,数据将从数据库中删除(我使用的是 mysql数据库)。
When the user clicks the X button, I hope the modal showed up with the specific name of the fanbase that the user want to delete.当用户点击 X 按钮时,我希望模态框显示用户想要删除的粉丝群的具体名称。 But I don't know how to pass the fanbase's id (from my database) to a function i made and use it.但我不知道如何将粉丝群的 id(来自我的数据库)传递给我制作和使用的 function。 Here what it looks like:这是它的样子:
the modal from "welcome.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?
<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>
the code for iteratively add divs of fanbases from "showfanbase.php"从“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;
}
}?>
and onclick of that button, a function (deletefunction()) on the "Welcome.php" will be executed.和该按钮的 onclick 将执行“Welcome.php”上的 function (deletefunction())。 inside it i pass the fanbase's id that is about to be deleted.在里面我传递了即将被删除的粉丝群的 ID。 This function suppose to be only showing the modal, but i want to make this function can hold the fanbase's id that is about to be deleted.这个 function 假设只显示模态,但我想让这个 function 可以保存即将被删除的粉丝群 id。
<script>
function deletefunction(del_id){
$('#delmdl').modal();}
</script>
what do I had to add to my code so I can show the fanbase's name on the modal and so I can pass that fanbase's id to deletefanbase.php(this is for deleting the data at mysql database)?我必须在我的代码中添加什么,以便我可以在模式上显示粉丝群的名称,然后我可以将该粉丝群的 id 传递给 deletefanbase.php(这是用于删除 mysql 数据库中的数据)?
you should take a hidden field to the form the post that value to the deletefanbase.php
您应该采取一个隐藏字段的形式将该值发布到deletefanbase.php
take a hidden field in welcome.php
in eg在welcome.php
中取一个隐藏字段,例如
<input type='hidden' name='delbyid' id='delbyid value="">
when function goes execute set delete id to the hidden field befor you submit the form当 function 在您提交表单之前执行 set delete id 到隐藏字段时
$('#delbyid').val(del_id);
it will post the value of that field with $_POST['delbyid'] on deletefanbase.php here you can execute the query to delete particular record with that 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";
}
THANKS FOR THE REPLY, IT WORKS.感谢您的回复,它的工作原理。 and I added an ajax on welcome.php to get the fanbase name on the modal.我在 Welcome.php 上添加了 ajax 以获取模态上的粉丝群名称。 I figured out you can't pass javascript variable directly to php variable.我发现您不能将 javascript 变量直接传递给 php 变量。
so, it's like this:所以,它是这样的:
the modal from "welcome.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>
the function on the welcome.php欢迎使用 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);
}
});
}
the getfbname.php, this is the php to get the fanbase name to show up on the modal 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'];
}?>
thankyou again for the answer, helped me alot!再次感谢您的回答,对我帮助很大!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.