1. 程式人生 > >HPCMS V9使用ajax方式提交表單

HPCMS V9使用ajax方式提交表單

lena for response fad serial ack color end 成功

一、前臺模板(註:需要引入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_-])[email protected]([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);
        }
    }

HPCMS V9使用ajax方式提交表單