如果没有数据,如何隐藏表格并使用jQuery或JavaScript显示表单
how to hide the table if there is no data and show the form using jquery or javascript
我正在获取表格格式的记录并在前端显示数据,但是如果没有数据,我需要隐藏包括表标题在内的总表。
<table style="width:87%;">
<tr class="spaces">
<th>S.No.</th>
<th style="width:20%;">Rent Earned</th>
<th>House</th>
<th>Address of Property</th>
</tr>
<?php include "rentaldetails.php";
while($row = mysql_fetch_array($result))
{?>
<tr>
<td>
<?php echo $row['house_details_id'];?>
</td>
<td>
<?php echo $row['rental_annual_rent'];?>
</td>
<td>
<?php echo $row['rental_tax_paid'];?>
</td>
<td>
<?php echo $row['rental_town'];?>
</td>
<td style="width:21%;"><a class="button add" onClick="document.location.href='income_tax.php'">Edit Details</a></td>
<td style="width:21%;"><a class="buttons delete" href="deleterental.php?id=<?php echo $row['house_details_id'];?>" onclick="return confirm('Are you sure to delete');" class="table-icon delete">Delete Property</a></td>
<td></td>
</tr>
<?php
}
?>
</table>
<form method="POST" action="details.php">
<th>Address Line</th>
<td><input type="text" name="address" value="" /></td>
<th>Town/City</th>
<td><input type="text" name="city" value="" /></td>
<button type="submit" class = "medium" style="background-color: #a9014b;float:left;">Save</button>
如果数据库中没有数据,我需要隐藏这些总表,并且应该显示表单。
<?php include "rentaldetails.php";
if(mysql_num_rows($result)== 0){
$dispNone = "display:none";
}else{
$dispNone = "display:block";
}
?>
<table style="width:87%;" style="<?=$dispNone?>">
<tr class="spaces">
<th>S.No.</th>
<th style="width:20%;">Rent Earned</th>
<th>House</th>
<th>Address of Property</th>
</tr>
<?php include "rentaldetails.php";
while($row = mysql_fetch_array($result))
{?>
<tr>
<td>
<?php echo $row['house_details_id'];?>
</td>
<td>
<?php echo $row['rental_annual_rent'];?>
</td>
<td>
<?php echo $row['rental_tax_paid'];?>
</td>
<td>
<?php echo $row['rental_town'];?>
</td>
<td style="width:21%;"><a class="button add" onClick="document.location.href='income_tax.php'">Edit Details</a></td>
<td style="width:21%;"><a class="buttons delete" href="deleterental.php?id=<?php echo $row['house_details_id'];?>" onclick="return confirm('Are you sure to delete');" class="table-icon delete">Delete Property</a></td>
<td></td>
</tr>
<?php
}
?>
</table>
编辑:
.PHP:
if(mysql_num_rows($result)== 0){
$dispNone = "display:none";
$dispForm = "display:block";
}else{
$dispNone = "display:block";
$dispForm = "display:none";
}
?>
.HTML:
<form style="width:87%;" style="<?=$dispForm?>">
</form>
编辑2:
<table style="width:87%;<?=$dispNone?>">
<form style="width:87%;<?=$dispForm?>">
您可以使用
if else 条件来显示和隐藏总计表如下所示:
<?php
if(mysqli_num_rows($result) > 0){ ?>
<table style="width:87%;">
<tr class="spaces">
<th>S.No.</th>
<th style="width:20%;">Rent Earned</th>
<th>House</th>
<th>Address of Property</th>
</tr>
<?php include "rentaldetails.php";
while($row = mysql_fetch_array($result))
{?>
<tr>
<td>
<?php echo $row['house_details_id'];?>
</td>
<td>
<?php echo $row['rental_annual_rent'];?>
</td>
<td>
<?php echo $row['rental_tax_paid'];?>
</td>
<td>
<?php echo $row['rental_town'];?>
</td>
<td style="width:21%;"><a class="button add" onClick="document.location.href='income_tax.php'">Edit Details</a></td>
<td style="width:21%;"><a class="buttons delete" href="deleterental.php?id=<?php echo $row['house_details_id'];?>" onclick="return confirm('Are you sure to delete');" class="table-icon delete">Delete Property</a></td>
<td></td>
</tr>
<?php } ?>
</table>
<?php } ?>
您可以使用
if else
语句来执行此操作。
if(!empty(mysql_fetch_array($result))){
//Here your datatable
}else{
//echo 'No Data';
}
Try this:
<?php
include "rentaldetails.php";
if(!empty(mysql_fetch_array($result))){?>
<table style="width:87%;">
<tr class="spaces" >
<th>S.No.</th>
<th style="width:20%;">Rent Earned</th>
<th>House</th>
<th>Address of Property</th>
</tr>
<?php
while($row = mysql_fetch_array($result))
{?>
<tr>
<td><?php echo $row['house_details_id'];?></td>
<td><?php echo $row['rental_annual_rent'];?></td>
<td><?php echo $row['rental_tax_paid'];?></td>
<td><?php echo $row['rental_town'];?></td>
<td style="width:21%;"><a class="button add" onClick="document.location.href='income_tax.php'">Edit Details</a></td>
<td style="width:21%;"><a class="buttons delete" href="deleterental.php?id=<?php echo $row['house_details_id'];?>" onclick="return confirm('Are you sure to delete');" class="table-icon delete" >Delete Property</a></td>
<td></td>
</tr>
<?php
}
?>
</table>
<?php } ?>
您可以使用
mysql_num_rows()
来隐藏<table>
它将在获取记录之前检查no的行:
<?php
if(mysql_num_rows($result) > 0){
?>
<table style="width:87%;">
<tr class="spaces">
<th>S.No.</th>
<th style="width:20%;">Rent Earned</th>
<th>House</th>
<th>Address of Property</th>
</tr>
<?php include "rentaldetails.php";
while($row = mysql_fetch_array($result))
{?>
<tr>
<td>
<?php echo $row['house_details_id'];?>
</td>
<td>
<?php echo $row['rental_annual_rent'];?>
</td>
<td>
<?php echo $row['rental_tax_paid'];?>
</td>
<td>
<?php echo $row['rental_town'];?>
</td>
<td style="width:21%;"><a class="button add" onClick="document.location.href='income_tax.php'">Edit Details</a></td>
<td style="width:21%;"><a class="buttons delete" href="deleterental.php?id=<?php echo $row['house_details_id'];?>" onclick="return confirm('Are you sure to delete');" class="table-icon delete">Delete Property</a></td>
<td></td>
</tr>
<?php
}
?>
</table>
<?
}
?>
旁注:
请使用mysqli_*
或PDO
扩展,因为mysql_*
在 PHP 7 中已弃用并关闭。
Jquery 解决方案:
$(document).ready(function(){
if ($("#mytable td").length == 0){
$("#mytable").hide();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="mytable" style="width:87%;">
<tr class="spaces">
<th>S.No.</th>
<th style="width:20%;">Rent Earned</th>
<th>House</th>
<th>Address of Property</th>
</tr>
</table>
如果您需要隐藏整个表格,您可以使用它HTML 表标记中的 PHP 代码,如果您的查询不返回任何结果,它应该隐藏整个表。
<?php echo (mysql_num_rows($result)>0)?'visible':'hidden'; ?>
所以基本上你隐藏表格的整个代码如下所示:
<table <?php echo (mysql_num_rows($result)>0)?'visible':'hidden'; ?> style="width:87%;">
<tr class="spaces">
<th>S.No.</th>
<th style="width:20%;">Rent Earned</th>
<th>House</th>
<th>Address of Property</th>
</tr>
<?php include "rentaldetails.php";
while($row = mysql_fetch_array($result))
{?>
<tr>
<td>
<?php echo $row['house_details_id'];?>
</td>
<td>
<?php echo $row['rental_annual_rent'];?>
</td>
<td>
<?php echo $row['rental_tax_paid'];?>
</td>
<td>
<?php echo $row['rental_town'];?>
</td>
<td style="width:21%;"><a class="button add" onClick="document.location.href='income_tax.php'">Edit Details</a></td>
<td style="width:21%;"><a class="buttons delete" href="deleterental.php?id=<?php echo $row['house_details_id'];?>" onclick="return confirm('Are you sure to delete');" class="table-icon delete">Delete Property</a></td>
<td></td>
</tr>
<?php
}
?>
</table>
<form method="POST" action="details.php">
<th>Address Line</th>
<td><input type="text" name="address" value="" /></td>
<th>Town/City</th>
<td><input type="text" name="city" value="" /></td>
<button type="submit" class = "medium" style="background-color: #a9014b;float:left;">Save</button>
相关文章:
- jQuery表单添加不适用于下拉列表
- Jquery表单验证插件-如果选中复选框,如何在提交时执行某些操作
- Ajax在JQuery表单插件中不起作用
- jQuery表单验证适用于Mozilla和Internet Explorer,但不适用于Chrome或Safari
- jQuery表单,存在检查
- setTimeOut AFTER jQuery表单提交
- jquery表单验证复选框错误显示
- jQuery表单验证(如果==“执行此操作”)仅适用于第一次输入
- jquery表单提交无法使用多个输入(IE和Firefox)
- Jquery表单提交仍在刷新
- 如何触发我的 jquery 表单提交
- jQuery 表单插件:XMLHttpRequest 无法加载 http://www...没有“访问控制允许来源”
- jQuery表单验证和提交问题
- jquery表单和图像提交
- jQuery表单和Json问题
- Jquery表单插件未显示成功功能
- JavaScript/JQuery表单验证运行两次
- jQuery表单在就绪/加载时不起作用
- 停止jQuery表单验证重置由其他JavaScript填充的typeahead.js输入字段
- jQuery表单验证器:需要提交两次