编写一个PHP程序:该程序通过在页面表单设置表格的行数、列数、长度、宽度,并根据此参数在页面上绘制出表格。
<html>
<head>
<title>表格添加器</title>
</head>
<body>
<?php
// 定义变量并初始化为空字符串
$width = "";
$height = "";
$rows = "";
$cols = "";
// 检查表单是否被提交
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// 从表单中获取值
$width = test_input($_POST["width"]);
$height = test_input($_POST["height"]);
$rows = test_input($_POST["rows"]);
$cols = test_input($_POST["cols"]);
}
// 处理表单数据并添加表格
function add_table() {
// 从全局变量中获取值
global $width, $height, $rows, $cols;
// 创建表格
echo "<table border='1' cellpadding='5'>";
// 循环添加行和列
for ($i = 1; $i <= $rows; $i++) {
echo "<tr>";
for ($j = 1; $j <= $cols; $j++) {
echo "<td width='$width' height='$height'></td>";
}
echo "</tr>";
}
// 关闭表格
echo "</table>";
}
// 验证用户输入
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>表格添加器</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table border="0" cellpadding="0">
<?php
// 添加5行文本框
for ($i = 1; $i <= 5; $i++) {
echo "<tr><td><input type='text' name='width' value='$width' size='3'></td>";
echo "<td>x</td>";
echo "<td><input type='text' name='height' value='$height' size='3'></td>";
echo "<td><input type='text' name='rows' value='$rows' size='3'></td>";
echo "<td>x</td>";
echo "<td><input type='text' name='cols' value='$cols' size='3'></td></tr>";
}
?>
</table>
<br>
<input type="submit" name="submit" value="确定">
<input type="button" value="重置" onclick="location.href='<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>'">
</form>
<?php
// 检查是否提交了表单
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// 添加表格
add_table();
}
?>
</body>
</html>
根据需求你自己调整
<!DOCTYPE html>
<html>
<head>
<title>绘制表格</title>
</head>
<body>
<form method="post">
<label>表格行数:</label>
<input type="number" name="rows" min="1"><br><br>
<label>表格列数:</label>
<input type="number" name="cols" min="1"><br><br>
<label>表格长度:</label>
<input type="number" name="width" min="1"><br><br>
<label>表格宽度:</label>
<input type="number" name="height" min="1"><br><br>
<input type="submit" name="submit" value="绘制表格">
</form>
<?php
if (isset($_POST['submit'])) {
// 获取表单参数
$rows = $_POST['rows'];
$cols = $_POST['cols'];
$width = $_POST['width'];
$height = $_POST['height'];
// 绘制表格
echo '<table border="1">';
for ($i = 1; $i <= $rows; $i++) {
echo '<tr>';
for ($j = 1; $j <= $cols; $j++) {
echo '<td width="' . $width . 'px" height="' . $height . 'px"></td>';
}
echo '</tr>';
}
echo '</table>';
}
?>
</body>
</html>