说明(不会phpcms的别看):
(1)表单建立完成后,可以使用ajax,jsonp,form方式来提交信息(三种方式任选其一)
(2)c=forms(这里控制器修改为forms,不是index)
(1)使用form方式
demo:
<form action="/index.php?m=formguide&c=forms&a=show&formid=13&action=js&siteid=1" method="post">
您的姓名:<input type="text" name="info[you_name]" value="" />
您的手机号:<input type="text" name="info[you_tel]" value="" />
<select name="info[you_province]" >
<option value="您所在的省份">您所在的省份:</option>
<option value="北京">北京</option>
<option value="上海">上海</option>
</select>
<input class="zg_sub" type="submit" id="dosubmit" value="提交信息" />
</form>
(2)使用jsonp方式
demo(html): 修改提交按钮修改为button
您的姓名:<input type="text" id="you_name" value="" />
您的手机号:<input type="text" id="you_tel" value="" />
<select id="you_province" >
<option value="您所在的省份">您所在的省份:</option>
<option value="北京">北京</option>
<option value="上海">上海</option>
</select>
<input class="zg_sub" type="button" id="dosubmit" value="提交信息" />
demo(js): 修改提交按钮修改为button
$(function (){
$("#dosubmit").click(function(event) {
//表单地址(注意将action值修改为jsonp)!!!!!
var url = '/index.php?m=formguide&c=forms&a=show&formid=25&action=jsonp&siteid=1';
//定义info对象并赋值
var info = {};
info.you_name = $("#you_name").val();
info.you_tel = $("#you_tel").val();
info.you_province = $("#you_province option:selected").val();
//发送ajax(注意使用get请求,dateType为jsonp)
$.ajax({
url: url,
type: 'GET',
dataType: 'jsonp',
data: {info: info},
success:function (json)
{
if (json.status==1) {
//json.status为1代表正确(以下的无用,不用看)
$("#myform").hide();
$(".zg_form img").show();
setTimeout(function() {
$(".zg_bdbox").hide();
$(".zg_btnbox").show();
return false;
sec++;
},3000)
} else {
alert('失败');
}
}
})
});
})
(3)使用普通ajax方式提交
demo(html): 修改提交按钮修改为button
您的姓名:<input type="text" id="you_name" value="" />
您的手机号:<input type="text" id="you_tel" value="" />
<select id="you_province" >
<option value="您所在的省份">您所在的省份:</option>
<option value="北京">北京</option>
<option value="上海">上海</option>
</select>
<input class="zg_sub" type="button" id="dosubmit" value="提交信息" />
demo(js): 修改提交按钮修改为button
$(function (){
$("#dosubmit").click(function(event) {
//表单地址(注意将action值修改为json)!!!!!
var url = '/index.php?m=formguide&c=forms&a=show&formid=25&action=json&siteid=1';
//定义info对象并赋值
var info = {};
info.you_name = $("#you_name").val();
info.you_tel = $("#you_tel").val();
info.you_province = $("#you_province option:selected").val();
//发送ajax(注意使用get请求,dateType为jsonp)
$.ajax({
url: url,
type: 'GET',
dataType: 'jsonp',
data: {info: info},
success:function (json)
{
if (json.status==1) {
//json.status为1代表正确(以下的无用,不用看)
$("#myform").hide();
$(".zg_form img").show();
setTimeout(function() {
$(".zg_bdbox").hide();
$(".zg_btnbox").show();
return false;
sec++;
},3000)
} else {
alert('失败');
}
}
})
});
})
后端PHP代码如下
<?php
defined('IN_PHPCMS') or exit('No permission resources.');
define('CACHE_MODEL_PATH',PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_model'.DIRECTORY_SEPARATOR.'caches_data'.DIRECTORY_SEPARATOR);
class forms {
private $db, $m_db, $M, $fields;
function __construct() {
$this->db = pc_base::load_model('sitemodel_model');
$this->m_db = pc_base::load_model('sitemodel_field_model');
$this->M = new_html_special_chars(getcache('formguide', 'commons'));
$this->siteid = intval($_GET[siteid]) ? intval($_GET[siteid]) : get_siteid();
$this->M = $this->M[$this->siteid];
}
/**
* 前端发送过来的信息
*/
public function show()
{
$this->siteid = isset($_GET['siteid']) ? intval($_GET['siteid']) : 1;
$this->formid = isset($_GET['formid']) ? (int)$_GET['formid'] : '';
$this->action = isset($_GET['action']) ? $_GET['action'] : 'js';
//检查表单id
$r = $this->db->get_one(array('modelid'=>$this->formid, 'siteid'=>$this->siteid, 'disabled'=>0), 'tablename, setting');
if (!$r || !$this->formid) {
return $this->return_data(array('status'=>0,'msg'=>L('form_no_exist')));
}
$this->tablename = $r['tablename'];
$setting = string2array($r['setting']);
if ($setting['enabletime']) {
if ($setting['starttime']>SYS_TIME || ($setting['endtime']+3600*24)<SYS_TIME) {
return $this->return_data(array('status'=>0,'msg'=>L('form_expired')));
}
}
//以下注释设置为无效
// if ($setting['allowunreg']==0 && !$userid && $_GET['action']!='js') showmessage(L('please_login_in'), APP_PATH.'index.php?m=member&c=index&a=login&forward='.urlencode(HTTP_REFERER));
$data = array();
$data = new_addslashes($_REQUEST['info']);
//如果是gbk的请加下面这行
//if ((string)$this->action != 'js') foreach($data as $key =>$val) $data[$key] = iconv('utf-8','gbk',$val);
//上面这行是比较重要的
$dataid = $this->check_data($data);//检查数据如果正确,插入数据
if ($dataid) {
if ($setting['sendmail']) {
pc_base::load_sys_func('mail');
$mails = explode(',', $setting['mails']);
if (is_array($mails)) {
foreach ($mails as $m) {
sendmail($m, L('tips'), $this->M['mailmessage']);
}
}
}
$this->db->update(array('items'=>'+=1'), array('modelid'=>$this->formid, 'siteid'=>$this->siteid));
}
return $this->return_data(array('status'=>1,'msg'=>L('thanks')));
}
//返回数据给用户(成功或失败)
public function return_data($data)
{
if ($this->action=='jsonp') {
$callback = $_GET['callback'];
exit($callback."(".json_encode($data).")");
} elseif ($this->action=='json') {
exit(json_encode($data));
} else {
showmessage($data['msg']);
}
}
//处理用户传入的数据
public function check_data($data)
{
$userid = param::get_cookie('_userid');
$data = new_html_special_chars($data);
$data = $this->get($data);
if (isset($data['status']) && $data['status']==0) return $this->return_data($data);
$data = $data['data'];
$data['userid'] = $userid;
$data['username'] = param::get_cookie('_username');
$data['datetime'] = SYS_TIME;
$data['ip'] = ip();
$tablename = 'form_'.$this->tablename;
$this->m_db->change_table($tablename);
$dataid = $this->m_db->insert($data, true);
return $dataid;
}
public function get($data)
{
$data = trim_script($data);
$info = array();
$this->fields = getcache('formguide_field_'.$this->formid, 'model');
foreach($this->fields as $field) {
$name = $field['name'];
$minlength = $field['minlength'];
$maxlength = $field['maxlength'];
$pattern = $field['pattern'];
$errortips = $field['errortips'];
$value = $data[$field['field']];
if(empty($errortips)) $errortips = $name.' '.L('not_meet_the_conditions');
$length = is_array($value) ? (empty($value) ? 0 : 1) : strlen($value);
if($minlength && $length < $minlength) {
return array('status'=>0,'msg'=>$name.' '.L('not_less_than').' '.$minlength.L('characters'));
}
if($maxlength && $length > $maxlength) {
return array('status'=>0,'msg'=>$name.' '.L('not_more_than').' '.$maxlength.L('characters'));
} elseif($maxlength) {
$value = str_cut($value,$maxlength,'');
}
if($pattern && $length && !preg_match($pattern, $value)) {
return array('status'=>0,'msg'=>$errortips);
}
$func = $field['formtype'];
if(method_exists($this, $func)) $value = $this->$func($field['field'], $value);
$info[$field['field']] = $value;
//颜色选择为隐藏域 在这里进行取值
if ($_POST['style_color']) $info['style'] = $_POST['style_color'];
if($_POST['style_font_weight']) $info['style'] = $info['style'].';'.strip_tags($_POST['style_font_weight']);
}
return array('status'=>1,'msg'=>'ok','data'=>$info);
}
public function send_sms()
{
//发送短信的接口(手机号验证,表单id验证,来源域名验证)待完成
}
//以下不用管
function textarea($field, $value) {
if(!$this->fields[$field]['enablehtml']) $value = strip_tags($value);
return $value;
}
function editor($field, $value) {
$setting = string2array($this->fields[$field]['setting']);
$enablesaveimage = $setting['enablesaveimage'];
$site_setting = string2array($this->site_config['setting']);
$watermark_enable = intval($site_setting['watermark_enable']);
$value = $this->attachment->download('content', $value,$watermark_enable);
return $value;
}
function box($field, $value) {
if($this->fields[$field]['boxtype'] == 'checkbox') {
if(!is_array($value) || empty($value)) return false;
array_shift($value);
$value = implode(',', $value);
return $value;
} elseif($this->fields[$field]['boxtype'] == 'multiple') {
if(is_array($value) && count($value)>1) {
$value = implode(',', $value);
return $value;
}
} else {
return $value;
}
}
function images($field, $value) {
//取得图片列表
$pictures = $_POST[$field.'_url'];
//取得图片说明
$pictures_alt = isset($_POST[$field.'_alt']) ? $_POST[$field.'_alt'] : array();
$array = $temp = array();
if(!empty($pictures)) {
foreach($pictures as $key=>$pic) {
$temp['url'] = $pic;
$temp['alt'] = str_replace(array('"',"'"),'`',$pictures_alt[$key]);
$array[$key] = $temp;
}
}
$array = array2string($array);
return $array;
}
function datetime($field, $value) {
$setting = string2array($this->fields[$field]['setting']);
if($setting['fieldtype']=='int') {
$value = strtotime($value);
}
return $value;
}
}
?>