Ajax请求案例

1 表单内容

<form method="post" class="J_ajaxForms" name="form1">
<div class="info">
<ul class="mycol mycol-3 clearfix">
<li class="col"><input name="name" id="name" type="text" placeholder="您的姓名:"></li>
<li class="col"><input name="phone" id="phone" type="text" placeholder="联系电话:"></li>
<li class="col"><input name="email" id="email" type="text" placeholder="邮箱地址:"></li>
</ul>
</div>
<textarea name="content" placeholder="详细......" id="content"></textarea>
<div class="more clearfix">
<a href="javascript:;" class="hover-circle-big fl" id="submitbtn">立即留言</a>
</div>
</form>


2 JS内容

<!-- 提交表单数据 -->
<script>
layui.use(['layer', 'form'], function () {
var layer = layui.layer;
var form = layui.form;

$('#submitbtn').click(() => {

// 获取数据
var data = {
name: $('#name').val(),
phone: $('#phone').val(),
email: $('#email').val(),
content: $('#content').val()
};

// 检测数据
if (typeof (data.name) == "undefined" || data.name == '' || data.name == null || data.name.trim() == '') {
layer.msg('姓名不能为空', { icon: 2, time: 1500 }, () => {
$('#name').val('');
$('#name').focus();
});
return false;
}
if (typeof (data.phone) == "undefined" || data.phone == '' || data.phone == null || data.phone.trim() == '') {
layer.msg('电话不能为空', { icon: 2, time: 1500 }, () => {
$('#phone').val('');
$('#phone').focus();
});
return false;
}
if (typeof (data.content) == "undefined" || data.content == '' || data.content == null || data.content.trim() == '') {
layer.msg('详情不能为空', { icon: 2, time: 1500 }, () => {
$('#content').val('');
$('#content').focus();
});
return false;
}

// 发起请求
$.ajax({
url: "{:url('home/Contact/addLiuyan')}",
data,
method: 'POST'
}).then(({ code, msg, data }) => {
if (code === 1) {
layer.msg(msg, { icon: 1, time: 1500 }, () => {
location.reload();
});
return false;
} else {
layer.msg(msg, { icon: 2, time: 1500 });
return false;
}
})
return false;
});
});
</script>



3 后台处理

// 留言信息
public function addLiuyan()
{
if(request()->isPost()){
// 接收数据
$data = [
'name' => input('name',''),
'phone' => input('phone',''),
'email' => input('email',''),
'content' => input('content','')
];
// 验证数据
$rule = [
'name' => 'require|max:25',
'phone' => ['require','regex:/^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/'],
'email' => 'email',
'content' => 'require'
];
$msg = [
'name.require' => '姓名不能为空',
'name.max' => '姓名最多不能超过25个字符',
'phone.require' => '电话不能为空',
'phone.regex' => '电话格式错误',
'email.email' => '邮箱格式错误',
'content.require' => '详情不能为空'
];
$validate = new Validate($rule, $msg);
$result = $validate->check($data);
if(!$result){
return json(['code' => 0, 'msg' => $validate->getError(), 'data' => []]);
}
// 添加数据
$data['create_time'] = time();
$data['update_time'] = time();
$res = Db::name('liuyan')->insert($data);
// 返回数据
if (!empty($res)) {
return json(['code' => 1, 'msg' => '留言成功', 'data' => []]);
}
return json(['code' => 0, 'msg' => '留言失败', 'data' => []]);
}
return json(['code' => 0, 'msg' => '请求类型错误', 'data' => []]);
}


相关推荐

  • 获取指定目录下的所有图片信息

    1 获取指定目录下的所有图片信息// 获取指定目录下的所有图片信息 public function getImagesInfo($directory) { $images = []; // 创建递归目录迭代器 $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY ); // 遍历目录中的每个文件 foreach (

  • Thinkphp各版本的PHP要求

    ThinkPHP 8.0:运行环境要求PHP8.0+,兼容PHP8.3ThinkPHP 6.1:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 6.0:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 5.1:运行环境要求PHP5.6+,兼容PHP8.0ThinkPHP 5.0:运行环境要求PHP5.4+,兼容PHP7.3

  • Thinkphp5.1路径常量

    1 配置文件位置根目录/config/template.php2 配置文件内容&lt;?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reser

  • Thinkphp5.0路径常量

    1 配置文件位置根目录/application/模块名/config.php2 配置文件内容&lt;?php//配置文件return [ // 后台视图输出字符串内容替换 'view_replace_str' =&gt; [ '__PUBLIC__' =&gt; '/', '__STATIC__' =&gt; '/static', '__CONSOLE__' =&gt; '/static/console', '__CONSOLE_CSS__' =&gt; '/static/console/css', '__CONSOLE_IMAGES__' =&gt; '/static/console/ima

  • wp站点防止别人进行DDOS攻击

    1 简介wp站点防止别人进行DDOS攻击。2 配置位置位置:根目录/wp-config.php3 配置内容在【根目录/wp-config.php】文件的开头添加如下代码:if(strpos($_SERVER['REQUEST_URI'], 'xmlrpc.php') !== false){ $protocol = $_SERVER['SERVER_PROTOCOL'] ?? ''; if(!in_array($protocol, ['HTTP/1.1', 'HTTP/2', 'HTTP/2.0', 'HTTP/3'], true)){ $protocol = 'HTTP/1.0'; } hea

  • 只读属性

    1 只读属性简介只读属性的声明方式类似于普通属性,但需要使用 readonly 关键字。2 只读属性例子class Point { public readonly float $x; public readonly float $y; public function __construct(float $x, float $y) { $this-&gt;x = $x; $this-&gt;y = $y; }}$point = new Point(3.5, 2.8);echo $point-&gt;x; // 输出: 3.5echo $point-&gt;y; // 输出: 2.8// 下面的尝