HPCMS V9如何使用javascript ajax方式提交表单

一、前台模板(注:需要引入jquery文件)

一、前台模板(注:需要引入jquery文件)

<form id="myform" class="subscribe-form subscription" >
      <input type="hidden" name="fid" value="15">
      <input type="hidden" name="sid" value="1">
      <input type="hidden" name="dosubmit" value="1">
      <input type="email" id="yemail" name="info[yemail]" placeholder="请输入邮箱">
      <input type="button" value="提交" onclick="formsubmit()">
      <div class="clearfix"></div>
      <p class="newsletter-success"></p>
      <p class="newsletter-error"></p>
</form>
 
<script language="javascript">
function formsubmit() {
    var yemail = $('#yemail').val();
    if (yemail == '') {
      $(".newsletter-error").html("邮箱不能为空!").fadeIn().delay(3000).fadeOut();
      return false;
    }
    if(!yemail.match(/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/))
    {
      $(".newsletter-error").html("邮箱格式不正确!").fadeIn().delay(3000).fadeOut();
      return false;
    }
    var params = $('#myform').serialize(); //序列化表单的值
    $.ajax({
        url:'index.php?m=formguide&c=index&a=diyform', //后台处理程序
        type:'post',       //数据传送方式
        dataType:'json',   //接受数据格式
        data:params,       //要传送的数据
        success:Response//回传函数(这里是函数名字)
    });
}
function Response(resp) {
    if (resp.result === 'success') {
      $('.newsletter-success').html("提交成功!").fadeIn().delay(3000).fadeOut();
    } else if (resp.result === 'error') {
      $('.newsletter-error').html("提交失败!").fadeIn().delay(3000).fadeOut();
    }
}
</script>

 

二、表单控制器 phpcms/modules/formguide/index.php 中增加diyform()函数.

public function diyform(){
        if (!isset($_POST['fid']) || empty($_POST['fid'])) {
            $res['result'] = "error";
            exit;
        }
        $siteid = $_POST['sid'] ? intval($_POST['sid']) : 1;
        $formid = intval($_POST['fid']);
        $r = $this->db->get_one(array('modelid'=>$formid, 'siteid'=>$siteid, 'disabled'=>0), 'tablename, setting');
        if (!$r) {
            $res['result'] = "error";
            exit;
        }
        $setting = string2array($r['setting']);
        if ($setting['enabletime']) {
            if ($setting['starttime']>SYS_TIME || ($setting['endtime']+3600*24)<SYS_TIME) {
                $res['result'] = "error";
                exit;
            }
        }
        if (isset($_POST['dosubmit'])) {
            $tablename = 'form_'.$r['tablename'];
            $this->m_db->change_table($tablename);
 
            $data = array();
            require CACHE_MODEL_PATH.'formguide_input.class.php';
            $formguide_input = new formguide_input($formid);
            $data = new_addslashes($_POST['info']);
            $data = new_html_special_chars($data);
            $data = $formguide_input->get($data);
            $data['datetime'] = SYS_TIME;
            $data['ip'] = ip();
            $dataid = $this->m_db->insert($data, true);
            if ($dataid) {
                $this->db->update(array('items'=>'+=1'), array('modelid'=>$formid, 'siteid'=>$this->siteid));
            }
            $res['result'] = "success";
            echo json_encode($res);
        }
    }

 

标签: 表单