欢迎您光临自学哈网,只为分享网络知识教程,供大家学习参考!

「自学哈网」PbootCMS使用Ajax无刷新提交留言及表单

作者 : 自学哈 本文共1326个字,预计阅读时间需要4分钟 2022-10-4 共204人阅读
也想出现在这里? 联系我们

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

1、html留言表单代码:

<form onsubmit="return submsg(this);">
    联系人<input type="text" name="contacts" required id="contacts">
    手 机<input type="text" name="mobile" required id="mobile">
    内 容<textarea name="content" id="content"></textarea>
    验证码<input type="text" name="checkcode" required id="checkcode">
    <img title="点击刷新" src="{pboot:checkcode}" onclick="this.src='{pboot:checkcode}?'+Math.round(Math.random()*10);" />
    <button type="submit">提交留言</button>
</form>

2、Ajax验证代码:

<script>
//ajax提交留言,由于涉及到提交地址标签的解析,JS需要放在html文件中
function submsg(obj){
  var url='{pboot:msgaction}'; //如果是自定义表单则使用地址{pboot:form fcode=*}
  var contacts=$(obj).find("#contacts").val();
  var mobile=$(obj).find("#mobile").val();
  var content=$(obj).find("#content").val();
  var checkcode=$(obj).find("#checkcode").val();
  
  $.ajax({
    type: 'POST',
    url: url,
    dataType: 'json',
    data: {
        contacts: contacts,
        mobile: mobile,
        content: content,
        checkcode: checkcode
    },
    success: function (response, status) {
      if(response.code){
         alert("谢谢您的反馈,我们会尽快联系您!");
         $(obj)[0].reset(); 
      }else{
         alert(response.data);
      }
    },
    error:function(xhr,status,error){
      alert('返回数据异常!');
    }
  });
  return false;
}
</script>

本站声明:
本站所有资源来源于网络,分享目的仅供大家学习和交流!如若本站内容侵犯了原著者的合法权益,可联系邮箱976157886@qq.com进行删除。
自学哈专注于免费提供最新的分享知识、网络教程、网络技术的资源分享平台,好资源不私藏,大家一起分享!

自学哈网 » 「自学哈网」PbootCMS使用Ajax无刷新提交留言及表单
也想出现在这里? 联系我们
© 2022 Theme by - 自学哈网 & WordPress Theme. All rights reserved 浙ICP备2022016594号