博主信息
博文 37
粉丝 0
评论 1
访问量 37192
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
ajax表单处理和js Jscript总结-2019-10-30
H先生
原创
903人浏览过

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>form提交</title>
    <style type="text/css">
        div{margin: 10px;text-align: center;}
    </style>
</head>
<body>
    <form  method="post" action="/1030/ajax.php">
        <div style="text-align: center;">
            <label for="">用户名</label>
            <input type="text" name="username">
        </div>
        <div style="text-align: center;">
            <label for="">密码</label>
            <input type="password" name="pwd">
        </div>
        <div>
            <label for="">验证码</label>
            <input type="text" name="vericode">
        </div>
        <div>
            <button onclick="save()">提交</button>
        </div>
    </form>

    <script type="text/javascript">
        function save() {

        }

    </script>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例



实例

<?php

$username = $_POST['username'];

$pwd = $_POST['pwd'];

$vericode = $_POST['vericode'];

if ($vericode != '123'){
    $html = '<div style="color: red;">验证码错误</div>
            <script>setTimeout(function() {
                  window.location.href="/1030/form.html"
                },1000);
             </script>';
    exit($html);
}

if ($username != 'admin'){
    $html = '<div style="color: red;">用户名错误</div>
            <script>setTimeout(function() {
                  window.location.href="/1030/form.html"
                },1000);
             </script>';
    exit($html);
}

if ($pwd != '123456'){
    $html = '<div style="color: red;">密码错误</div>
            <script>setTimeout(function() {
                  window.location.href="/1030/form.html"
                },1000);
             </script>';
    exit($html);
}

exit('登录成功');

运行实例 »

点击 "运行实例" 按钮查看在线实例



1.png











实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>form提交</title>
    <script src="jquery-3.4.1.min.js" type="text/javascript"></script>
    <style type="text/css">
        div{margin: 10px;text-align: center;}
    </style>
</head>
<body>
    <form  method="post" action="/1030/ajax.php">
        <div style="text-align: center;">
            <label for="">用户名</label>
            <input type="text" name="username">
        </div>
        <div style="text-align: center;">
            <label for="">密码</label>
            <input type="password" name="pwd">
        </div>
        <div>
            <label for="">验证码</label>
            <input type="text" name="vericode">
        </div>
        <div>
            <button type="button" onclick="save()">提交</button>
        </div>
    </form>

    <script type="text/javascript">
        function save() {
            var obj = new Object();
            //var username = $.trim($('input[name="username"]').val());
            obj.username = $.trim($('input[name="username"]').val());
            //var pwd = $.trim($('input[name="pwd"]').val());
            obj.pwd = $.trim($('input[name="pwd"]').val());
            //var vericode = $.trim($('input[name="vericode"]').val());
            obj.vericode = $.trim($('input[name="vericode"]').val());

            if (obj.username==''){
                alert('请输入用户名');
                return;
            }
            if (obj.pwd==''){
                alert('请输密码');
                return;
            }
            if (obj.vericode==''){
                alert('请输入验证码');
                return;
            }
            //$.post('/1030/ajax.php',{username:username,pwd:pwd,vericode:vericode},function (data) {
            //$.post('/1030/ajax.php',obj,function (data) {
            $.post('/1030/ajax.php',$('form').serialize(),function (data) {
                if (data.code>0){
                    alert(data.msg);
                    return;
                }
                alert(data.msg);
                window.location.href="http://www.baidu.com";
            },'json');
        }

    </script>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php

$username = $_POST['username'];

$pwd = $_POST['pwd'];

$vericode = $_POST['vericode'];

if ($vericode != '123'){
    exit(json_encode(array('code'=>1,'msg'=>'验证码错误')));
}

if ($username != 'admin'){
    exit(json_encode(array('code'=>1,'msg'=>'用户名错误')));
}

if ($pwd != '123456'){
    exit(json_encode(array('code'=>1,'msg'=>'密码错误')));
}

exit(json_encode(array('code'=>0,'msg'=>'登录成功')));

运行实例 »

点击 "运行实例" 按钮查看在线实例


1.png




批改状态:合格

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

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

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