织梦园模板网提供云优cms模板,pbootcms模板,Wordpress主题模板等各类企业新闻资讯网站模板下载服务。织梦园网站模板交流群
用户名:
密码:
注册
注册后享受折扣价

织梦自定义表单通过ajax提交的实现方法

织梦园模板 / 2019-01-11 / 收藏

自定义表单通过ajax判断,提交不用跳转页面,提高用户体验。具体方法如下:

html表单代码部分,就提交按钮改成botton,,添加onclick事件

表单代码:

<form action="javascript:;" enctype="multipart/form-data" method="post">

<input type="hidden" name="action" value="post" />

<input type="hidden" name="diyid" value="1" />

<input type="hidden" name="do" value="2" />

<label>姓名:<input type="text" id="name" name="name" value="" /></label>

<label>电话:<input type="text" id="tel" name="tel" value="" /></label>

<label>手机:<input type="text" id="iphone" name="iphone" value="" /></label>

<label>邮箱:<input type="text" id="email" name="email" value="" /></label>

<table>

<tr>

<td valign="top">留言:</td>

<td><textarea id="liuyan" name="liuyan"></textarea></td>

</tr>

</table>

<input type="hidden" name="dede_fields" value="name,textchar;tel,textchar;iphone,textchar;email,textchar;liuyan,multitext" />

<input type="button" class="submit fr" value="提 交" onclick="add_ajaxmessage()"/>

</form>

js代码(这个代码之前***要引入jQuery库):

function add_ajaxmessage(){

var name=document.getElementById("name");

var tel =document.getElementById("tel");

var iphone=document.getElementById("iphone");

var email=document.getElementById("email");

var liuyan=document.getElementById("liuyan");

//定义变量接收上面表单每项的值和几个dede隐藏的input的值

var dataString = 'name='+ name.value + '&tel=' + tel.value + '&iphone=' + iphone.value + '&email=' + email.value + '&liuyan='+ liuyan.value +'&action=post'+ '&diyid=1&do=2&dede_fields=name,textchar;tel,textchar;iphone,textchar;email,textchar;liuyan,multitext';

$.ajax({

type: "POST",

url: "/plus/diy.php", //提交到后台文件

data: dataString, //传值

success: function(data) {

alert(data);//成功打印PHP返回的值

}

});

return false;

}

php代码:

找到:if(!empty($dede_fields))

{

//在里面加入判断语句,不判断也可以

if($name==""){

echo "请填写您的姓名!";//注意:输出用echo输出,不要用showMsg();

exit();

}

}

找到:$goto = !empty($cfg_cmspath) ? $cfg_cmspath : '/';

$bkmsg = '发布成功,请等待管理员处理...';

这两句,改成:echo "提交成功!";

删掉下面这一句:showmsg($bkmsg, $goto);

相关安装使用

收缩