找回密码
 立即注册
查看: 3085|回复: 1

pbootcms使用Ajax无刷新提交留言及表单

[复制链接]

650

主题

2041

回帖

3346

积分

管理员

积分
3346

社区居民社区明星最爱沙发灌水天才终身成就奖诚信商户

QQ
发表于 2022-5-17 13:18:17 | 显示全部楼层 |阅读模式
1,表单验证
  1. <form onsubmit="return submsg(this);">
  2.     联系人<input type="text" name="contacts" required id="contacts">
  3.     手 机<input type="text" name="mobile" required id="mobile">
  4.     内 容<textarea name="content" id="content"></textarea>
  5.     验证码<input type="text" name="checkcode" required id="checkcode">
  6.     <img title="点击刷新" src="{pboot:checkcode}" onclick="this.src='{pboot:checkcode}?'+Math.round(Math.random()*10);" />
  7.     <button type="submit">提交留言</button>
  8. </form>
复制代码
2,Ajax提交
  1. <script>
  2. //ajax提交留言,由于涉及到提交地址标签的解析,JS需要放在html文件中
  3. function submsg(obj){
  4.   var url='{pboot:msgaction}'; //如果是自定义表单则使用地址{pboot:form fcode=*}
  5.   var contacts=$(obj).find("#contacts").val();
  6.   var mobile=$(obj).find("#mobile").val();
  7.   var content=$(obj).find("#content").val();
  8.   var checkcode=$(obj).find("#checkcode").val();
  9. //此处加个判断,避免碰到刷留言
  10.   if (!$('[name="contacts"]').val()) {alert('姓名不能为空');returnfalse;   }
  11. // 判断在要写入数组前,这里我就举例一个其余的可以发挥自己的想法写
  12.   $.ajax({
  13.     type: 'POST',
  14.     url: url,
  15.     dataType: 'json',
  16.     data: {
  17.         contacts: contacts,
  18.         mobile: mobile,
  19.         content: content,
  20.         checkcode: checkcode
  21.     },
  22.     success: function (response, status) {
  23.       if(response.code){
  24.          alert("谢谢您的反馈,我们会尽快联系您!");
  25.          $(obj)[0].reset();
  26.       }else{
  27.          alert(response.data);
  28.       }
  29.     },
  30.     error:function(xhr,status,error){
  31.       alert('返回数据异常!');
  32.     }
  33.   });
  34.   return false;
  35. }
  36. </script>
复制代码


安徽大学七彩梦论坛欢迎您~! www.ahubbs.com

650

主题

2041

回帖

3346

积分

管理员

积分
3346

社区居民社区明星最爱沙发灌水天才终身成就奖诚信商户

QQ
 楼主| 发表于 2022-5-17 13:19:05 | 显示全部楼层
      <div class="contact-r">
        <form onsubmit="return submsg(this);" class="contact-form">
          <div class="">
            <input type="text" class="inp l" name="contacts" required id="contacts" placeholder="您的姓名">
            <input type="text" class="inp r" name="mobile" required id="mobile" placeholder="您的联系电话">
          </div>
          <textarea cols="30" rows="10" name="content" required id="content"></textarea>
          <div class="">{pboot:if({pboot:msgcodestatus})}
            <input type="text" class="inp l" name="checkcode" required id="checkcode" placeholder="验证码">
            <img src="{pboot:checkcode}" title="点击刷新" width="120" height="30" border="0" align="top" id="verifyImg" style="margin:0 0 15px 15px;" onClick="this.src='{pboot:checkcode}?'+Math.round(Math.random()*10);"/>{/pboot:if} </div>
           <input type="submit" value="提交您的需求" class="sub" id="sub">
        </form>
      </div>
安徽大学七彩梦论坛欢迎您~! www.ahubbs.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|安徽大学论坛 ( 皖ICP备11003929号-3 )

GMT+8, 2024-4-25 07:15 , Processed in 0.046358 second(s), 3 queries , Gzip On, Redis On.

Powered by Discuz! X3.5

Copyright © 2006-2024 安徽大学论坛 All Rights Reserved

快速回复 返回顶部 返回列表