为了账号安全,请及时绑定邮箱和手机立即绑定

ajax无刷新表单提交

标签:
Html/CSS

ajax无刷新提交笔记

数据库自己模拟建一个 执行一下添加功能

php部分采用pdo方法:

<?php$db ='mysql';$host ='localhost';$port ='3306';$dbname = 'ajax';$user = 'root';$pwd  = '';$dsn = "$db:host=$host;port=$port;dbname=$dbname";   //设定字符集 $options = array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES \'UTF8\''); $pdo = new PDO($dsn,$user,$pwd,$options);$name = $_POST['name'];//print_r ($name);exit;$pwd  = $_POST['pwd'];$code  = $_POST['code'];$sql = "insert into user (name,pwd,code) values ('".$name."','".$pwd."','".$code."')";$res=$pdo->exec($sql);  if ($res){   echo 1;   exit;}else{   echo 2;   exit;}

ajax前端部分

<script type="text/javascript" class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="jquery-1.8.3.min.js"></script>        <script type="text/javascript">        $(function(){            $(".user_main_r button").click(function(){                                   $.post('./login.php', $('form').serialize(), function(data) {                                if(data == 1){                   alert ('添加成功');                     $("#admin_user").val('');                     $("#admin_psd").val('');                     $("#captcha").val('');                }else{                   alert ('添加失败');                }                              });                        });                });                                         </script>        <title>无刷新用户登录</title>    </head>    <body id="userlogin_body">                         <form id="form" method="post" >                        <ul>                                                     <div class="user_main_box">                                    <ul>                                        <li class="user_main_text">用户名: </li>                                        <li class="user_main_input">                                            <input id="admin_user" name="name" maxlength="20" />                                                                                </li>                                   </ul>                                    <ul>                                        <li class="user_main_text">密&nbsp;&nbsp;&nbsp;&nbsp;码: </li>                                        <li class="user_main_input">                                            <input class="TxtPasswordCssClass" id="admin_psd" name="pwd" type="password">                                                                                    </li>                                    </ul>                                    <ul>                                        <li class="user_main_text">身份证: </li>                                        <li class="user_main_input">                                            <input class="TxtValidateCodeCssClass" id="captcha" name="code" type="text">                                                                                                               </li>                                    </ul>                                                                    </div>                            </li>                            <li class="user_main_r">                                <button  type="button" >提交</button>                            </li>                        </ul>                    </form>               </body>


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消