批改状态:合格
老师批语:
1/表格的输出需要循环判断,if()else(),for()等语句等功能相当强大;
2/php语法中 ; 一定不要遗漏;
3/基本思路:
判断输入的内容,有无内容,是否符合预定要求;用get方法将后台数据获得;后台判断行列数量,生成表格的拼接字符串,并返回给前端;清空之前生成的数据,避免显示的堆积。
做一份功能之前,要先分析好设计的思路才能事半功倍。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>表格自动生成器</title>
</head>
<body>
<p><lable>请输入标题:<input type="text" name="tName"></lable></p>
<p><lable>请输入行:<input type="text" name="rows" class="nub"></lable></p>
<p><lable>请输入列:<input type="text" name="cols" class="nub"></lable></p>
<p><button>生成表格</button><button>重置表格</button></p>
</body>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
/**首先对输入内容进行验证:
1/非空验证;
2/验证输入的是否字母;
3/验证输入的是否0;
**/
var flag = true
$('button:first').click(function(){
$('.nub').each(function(index,obj){
if ($(obj).val().length == 0){
$(obj).after('<span>不能为空</span>')
setTimeout(function(){
$(obj).next().remove()
},1000)
return false
} else if (isNaN($(obj).val())) {
$(obj).after('<span>必须为数字</span>')
setTimeout(function(){
$(obj).next().remove()
},1000)
return false
} else if ($(obj).val() <= 0) {
$(obj).after('<span>必须大于0</span>')
setTimeout(function(){
$(obj).next().remove()
},1000)
return false
}
})
if (flag == true){
$.get(
'demo.php',
{
tName:$('input[name="tName"]').val(),
rows:$('input[name="rows"]').val(),
cols:$('input[name="cols"]').val(),
},
function(data){
$('p:last').next().remove()
$('p:last').after(data)
}
// flag = false
)
}
$('button:last').click(function(){
$(':input').not('button').val('')
$('input:first').focus()
$('p:last').next().remove()
})
flag = false
})
</script>
</html>点击 "运行实例" 按钮查看在线实例
<?php
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
if (!empty($_GET['rows']) && !empty($_GET['cols'])) {
$tName =$_GET['tName'];
$rows = $_GET['rows'];
$cols = $_GET['cols'];
$table = '<h3 align="center">'.$tName.'</h3>'.'<table border="1" cellspacing="0" cellpadding="3" align="center" width="80%">';
$table .= '<tr align="center" bgcolor="green">';
for ($i=0; $i<$cols; $i++) {
$thead_Name = ['id','姓名','性别','年龄','学科','成绩','级别'];
$table .='<th>'.$thead_Name[$i].'</th>';
}
for ($r=0; $r<$rows; $r++) {
$table .='<tr>';
for ($c=0; $c<$cols; $c++) {
$table .='<td align="center">'.'text'.'</td>';
}
$table .='</tr>';
}
$table .='</table>';
echo $table;
}
} else {
exit('<span>非法请求</span>');
}点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号