4-1前端表单的制作-PHP与Ajax实现表单验证

4-1前端表单的制作-PHP与Ajax实现表单验证

伪蟹怪粗泄绩穿钒画兴逞填抵


表单验证极其重要    PHP最初的目标就是处理表单

前后端都必须进行验证   否则会留下严重的安全隐患!

新建目录4  在该目录下新建demo.php

4-1前端表单的制作-PHP与Ajax实现表单验证第1张

代码:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>实例:用PHP来处理表单</title>
</head>
<body>
    <table>
        <caption>用户注册</caption>
        <tr>
<!--            label标签与id一一对应 点击邮箱时光标会自动跳到输入框  用户友好-->
            <td><label for="email">邮箱</label></td>
            <td><input type="text" name="eamil" id="email"></td>
        </tr>
        <tr>
            <td><label for="password1">密码</label></td>
            <td><input type="password" name="password1" id="password1"></td>
        </tr>
        <tr>
            <td><label for="password2">确认</label></td>
            <td><input type="password" name="password2" id="password2"></td>
        </tr>
        <tr>
            <td><label for="secret">性别</label></td>
<!--            注意:单选框必须加value属性  且所以name值必须一致-->
            <td><input type="radio" name="gender" id="male" value="male"><label for="male">男</label>
                <input type="radio" name="gender" id="female" value="female"><label for="female">女</label>
<!--                默认性别即为保密:checked=""-->
                <input type="radio" name="gender" id="secret" value="secret" checked=""><label for="secret">保密</label>

            </td>
        </tr>
        <tr>
            <td><label for="level">级别</label></td>
            <td>
<!--                下拉列表框必须指定value值-->
                <select name="level" id="level">
                    <option value="0">小白</option>
<!--                    默认选中中级-->
                    <option value="1" selected="">中级</option>
                    <option value="2">大神</option>

                </select>
            </td>
        </tr>
        <tr>
            <td><label for="php">语言</label></td>
            <td>
<!--                多选框name值必须是数组  默认选中PHP-->
                <input type="checkbox" name="lang[]"id="php"value="php" checked=""><label for="php">php</label>
                <input type="checkbox" name="lang[]"id="java"value="java"><label for="java">java</label>
                <input type="checkbox" name="lang[]"id="python"value="python"><label for="python">python</label>
                <input type="checkbox" name="lang[]"id="c"value="c"><label for="c">c</label>

            </td>
        </tr>

<!--        文本域-->
        <tr>
            <td><label for="comment">简介:</label></td>
            <td>
<!--                3行 30列-->
                <textarea name="comment" id="comment" rows="3" cols="30"></textarea>
            </td>
        </tr>
        <tr>
<!--            两列合并-->
            <td colspan="2" align="center">
                <button type="submit" name="submit" id="submit" value="submit">提交</button>
            </td>
        </tr>



    </table>
</body>
</html>

执行:

4-1前端表单的制作-PHP与Ajax实现表单验证第2张

美化:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>实例:用PHP来处理表单</title>
    <style type="text/css">
        table
        {
            /*背景色:小麦色*/
            background-color:wheat;
            /*添加阴影*/
            box-shadow: 3px 3px 3px #888;
            /*添加百分之3的圆角*/
            border-radius:3%;
            /*内间距15px*/
            padding;15px;
            /*外边距:上下30px  左右居中*/
            margin:30px auto;
        }
        table td
        {
            /*内间距8px*/
            padding:8px;
        }
        table caption
        {
            /*标题字体大小1.5em*/
            font-size: 1.5em;
            /*外边距距离底部10px   将标题与表格分开一点*/
            margin-bottom:10px;
        }
        textarea
        {
            /*不允许文本域拉伸*/
            resize: none;
        }
        table button
        {
            width:100px;
            height:30px;
            border:none;
            background-color:skyblue;
            color:white;

        }
        table button:hover
        {
            /*访问按钮时  字体变为1.1em 背景色变为橘红 鼠标变为手型*/
            font-size:1.1em;
            background-color:coral;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <table>
        <form>
        <caption>用户注册</caption>
        <tr>
<!--            label标签与id一一对应 点击邮箱时光标会自动跳到输入框  用户友好-->
            <td><label for="email">邮箱</label></td>
            <td><input type="text" name="eamil" id="email"></td>
        </tr>
        <tr>
            <td><label for="password1">密码</label></td>
            <td><input type="password" name="password1" id="password1"></td>
        </tr>
        <tr>
            <td><label for="password2">确认</label></td>
            <td><input type="password" name="password2" id="password2"></td>
        </tr>
        <tr>
            <td><label for="secret">性别</label></td>
<!--            注意:单选框必须加value属性  且所以name值必须一致-->
            <td><input type="radio" name="gender" id="male" value="male"><label for="male">男</label>
                <input type="radio" name="gender" id="female" value="female"><label for="female">女</label>
<!--                默认性别即为保密:checked=""-->
                <input type="radio" name="gender" id="secret" value="secret" checked=""><label for="secret">保密</label>

            </td>
        </tr>
        <tr>
            <td><label for="level">级别</label></td>
            <td>
<!--                下拉列表框必须指定value值-->
                <select name="level" id="level">
                    <option value="0">小白</option>
<!--                    默认选中中级-->
                    <option value="1" selected="">中级</option>
                    <option value="2">大神</option>

                </select>
            </td>
        </tr>
        <tr>
            <td><label for="php">语言</label></td>
            <td>
<!--                多选框name值必须是数组  默认选中PHP-->
                <input type="checkbox" name="lang[]"id="php"value="php" checked=""><label for="php">php</label>
                <input type="checkbox" name="lang[]"id="java"value="java"><label for="java">java</label>
                <input type="checkbox" name="lang[]"id="python"value="python"><label for="python">python</label>
                <input type="checkbox" name="lang[]"id="c"value="c"><label for="c">c</label>

            </td>
        </tr>

<!--        文本域-->
        <tr>
            <td><label for="comment">简介:</label></td>
            <td>
<!--                3行 30列-->
                <textarea name="comment" id="comment" rows="3" cols="30"></textarea>
            </td>
        </tr>
        <tr>
<!--            两列合并-->
            <td colspan="2" align="center">
                <button type="submit" name="submit" id="submit" value="submit">提交</button>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

执行:

4-1前端表单的制作-PHP与Ajax实现表单验证第3张


琶缺俏岗桶挤摆挂慰悍圣霸蹿