博主信息
博文 22
粉丝 1
评论 1
访问量 26546
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
表格自动生成器
forever浅笑
原创
1377人浏览过

1.png

2.png

3.png

p1.png

前台代码如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>表格生成器</title>
    <style>
       *{margin:0;padding:0;} body{padding:300px;} .tbl{width:380px;margin:0 auto;} .tbl input{outline: none;border:1px solid #333;width:170px;} .tbl h3{color:green;text-align:center;} .tbl h3, .tbl div{padding:10px;} button{background: green;color:#fff;border:0;padding:4px;} .tbl div:nth-child(5){text-indent:80px;} table {border-collapse:collapse; width:80%;} td,th{border:1px solid #000;text-align: center;} label {width:68px;display: inline-block;}
    </style>
</head>
<body>
    <div>
        <h3>表格生成器</h3>
        <div><label for="row">输入行</label> <input type="text" name="row" id="row"></div>
        <div><label for="col">输入列</label> <input type="text" name="col" id="col"></div>
        <div><label for="col">标 题</label> <input type="text" name="title" id="title"></div>
        <div><button type="submit">生成表格</button> <button type="reset">重置表格</button></div>
    </div> 
    <script src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script>
    <script>
        
     
            var flag = true;
            $('button:first').click(function() {
               $(':input').not('button').each(function(index,obj) {

                    if ($(obj).val().length == 0) {
                        $(obj).after('<span style="color:red;">不能为空</span>');
                        setTimeout(function() {
                            $(obj).next().remove();
                        },500);
                        flag = false;
                        return false;
                    } else if (isNaN($(obj).val()) && index != 2) {
                        $(obj).after('<span style="color:red;">必须为数字</span>');
                        setTimeout(function() {
                            $(obj).next().remove();
                        },500);
                        flag = false;
                        return false;
                    } else if ($(obj).val() <= 0 && index != 2) {
                        $(obj).after('<span style="color:red;">必须为正数</span>');
                        setTimeout(function() {
                            $(obj).next().remove();
                        },500);
                        flag = false;
                        return false;
                    } else {
                        flag = true;
                    }
               })
                //console.log(flag);
               if (flag == true) {
                    $.get('action.php',{
                        rows:$('#row').val(),
                        cols:$('#col').val(),
                        title:$('#title').val()
                    },function(data) {
                        $(".tbl").next().remove();
                        $(".tbl").after(data);
                        flag = false;
                    })
               }
            });
            $('button:last').click(function() {
                $(':input').not('button').val('');
                $(':input').first().focus();
                $('.tbl').next().remove();
                flag = true;
            })
    </script>
   
</body>
</html>

后台代码如下:

<?php 
    // 判断ajax请求类型
    if ($_SERVER['REQUEST_METHOD'] == 'GET') {
        //  判断
        if (!empty($_GET['rows']) && !empty($_GET['cols']) && !empty($_GET['title'])) {
            $rows = $_GET['rows'];
            $cols = $_GET['cols'];
            $title = $_GET['title'];
            $tbl = "<h4 style='padding:2% 0;'>{$title}</h4>";
            $tbl .= '<table>';
            $tbl .= '<tr>';
            for ($i=0; $i < $cols; $i++) { 
                $tbl .= '<th style="background:green;padding:10px;">x</th>';
            }
            $tbl .= '</tr>';
            for ($i=0; $i < $rows; $i++) { 
                $tbl .= '<tr>';
                    for ($j=0; $j < $cols; $j++) { 
                        $data = $i * $cols + $j + 1;
                        $tbl .= "<td>{$data}</td>";
                    }
                $tbl .= '</tr>';
            }
            $tbl .= '</table>';
            exit($tbl);
        }
    } else {
        exit('非法请求');
    }


批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学