【发布时间】:2017-05-07 20:07:20
【问题描述】:
我创建了从DB 中删除php 中的一些项目。我也使用了搜索功能。搜索功能机制是当我搜索某些内容时,现有的表会隐藏并显示搜索详细信息。在搜索和原始表中有一个名为删除的按钮。原始表格按钮在 ajax 中正常工作。
但搜索结果删除按钮无法正常工作。没有错误。当我单击搜索时,结果删除按钮没有发生。帮我解决这个问题。我的代码如下。原始表删除按钮类是“mybutton”。搜索到的按钮类是“mybutton21”。谢谢。
<form action="../PHP/searchrmvcom.php" method="post">
<div class="search hidden-xs hidden-sm">
<input type="text" placeholder="Search" id="search" name="search">
</div>
<div>
<input type="button" value="Search" id="searchrmvcom" name="searchrmvcom">
<script>
$("#searchrmvcom").click(function () {
var comname=$('#search').val();
$.ajax({
type:"post",
url:"../PHP/searchrmvcom.php",
data:{comname:comname},
success:function (data3) {
$('#rmvcomdiv').hide();
$('#ela').html(data3)
}
});
});
</script>
</div>
</form>
</div>
<div class="col-md-5">
<div class="header-rightside">
<ul class="list-inline header-top pull-right">
<li class="hidden-xs"><a href="#" class="add-project" data-toggle="modal" data-target="#add_project">Change Password</a></li>
</ul>
</div>
</div>
</header>
</div>
<div class="user-dashboard">
<h1>Comapny Remove</h1>
<div class="row">
<!-- code here -->
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default panel-table">
<div class="panel-heading">
<div class="row">
<div class="col col-xs-6">
<h3 class="panel-title">Company Removal</h3>
</div>
</div>
</div>
<div id="ela"></div>
<div class="panel-body" id="rmvcomdiv" name="rmvcomdiv">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th>Action</th>
<th>ID</th>
<th>Registration number</th>
<th>Company Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php
include('../PHP/dbconnection.php');
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "";
$sql = "select * from company where activation_code=1";
$res = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($res)):
?>
<tr>
<td align="center"><button type="submit" class="btn btn-default myButton" value="<?php echo $row['companyid']; ?>" id="accept" name="accept">Remove</button></td>
<td><?php echo $row['companyid']; ?></td>
<td><?php echo $row['government_reg_no']; ?></td>
<td><?php echo $row['company_name']; ?></td>
<td><?php echo $row['email']; ?></td>
</tr>
<?php
endwhile
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<script>
$(".myButton").click(function () {
var company_id = $(this).val();
$.ajax({
type:"POST",
url:"../PHP/deletecompanycode.php",
data:{comid: company_id},
success:function (data) {
alert(data);
location.reload(true);
}
});
});
</script>
<script>
$(".myButton21").click(function () {
var company_id2 = $(this).val();
$.ajax({
type:"POST",
url:"../PHP/deletecompanycode.php",
data:{comid: company_id2},
success:function (data2) {
alert(data2);
location.reload(true);
}
});
});
</script>
删除公司代码.php
<?php
include('dbconnection.php');
$comid=$_POST['comid'];
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql2="select * from company where companyid='$comid'";
$res2 = mysqli_query($conn, $sql2);
$row2 = mysqli_fetch_assoc($res2);
$sql3="delete from login where
username='".$row2['government_reg_no']."'";
$sql="delete from company where companyid='$comid'";
$sql4="delete from vacancy where companyid='$comid'";
$conn->query($sql3);
if ($conn->query($sql3) === TRUE && $conn->query($sql) === TRUE && $conn->query($sql4)===TRUE ) {
echo 'Successfully Removed';
}
else{
echo 'Occured Error..Try Again';
}
?>
searchrmvcom.php
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
require('../PHP/dbconnection.php');
$sql="select * from company where company_name like '%".$_POST["comname"]."%' and activation_code=1";
$res=mysqli_query($conn,$sql);
if(mysqli_num_rows($res)>0) {
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th>Action</th>
<th>ID</th>
<th>Registration number</th>
<th>Company Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_assoc($res)) {
?>
<tr>
<td align="center"><button type="submit" class="btn btn-default myButton21" value="<?php echo $row['companyid']; ?>" id="accept" name="accept">Remove</button></td>
<td><?php echo $row['companyid']; ?></td>
<td><?php echo $row['government_reg_no']; ?></td>
<td><?php echo $row['company_name']; ?></td>
<td><?php echo $row['email']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
?>
【问题讨论】:
-
@JeremyE。我也通过编辑添加该页面。
-
问题似乎是你在按钮存在之前为 mybutton21 调用了你的函数(在点击时绑定它),所以这个函数永远不会附加到按钮上。
-
@JeremyE。那么我应该怎么做才能解决这个问题。我是 php 新手。
-
这是一个 JS 问题,而不是 PHP 问题。您需要将 mybutton21 的函数调用移动到 searchrmvcom 的成功函数中。我会写一个答案
-
对不起,我的回答有误,不要加
$(".myButton21").click(myFunction()),加$(".myButton21").click(myFunction)。第一个调用该函数,第二个只是引用它,因此单击时将调用它。我会更新我的答案。让我知道这是否有效