PbootCMS
后台配置参数栏目开启API,不开启API强制认证
1、表单提交判断是否有存在的手机号码
/**
*@url:/api.php/cms/checkForm
* @type post
* @param int mobile 手机号码
* @return success true
*/
<!-- 留言表单 -->
<form id="myform" method="post" >
联系人:<input type="text" name="contacts" id="contacts">
手机: <input type="text" name="mobile" id="mobile">
内容: <input type="text" name="content" id="content">
<button type="botton" onclick="checkForm();">提交</button>
</form>
<script>
function checkForm() {
var url = '/api.php/cms/checkForm';
var mobile = $("#mobile").val();
var contacts = $("#contacts").val();
var content = $("#content").val();
$.ajax({
type: 'POST',
url: url,
dataType: 'json',
data: {
mobile: mobile
},
success: function(response, status) {
if (response.code) {
alert(response.data);
} else {
$.ajax({
type: 'POST',
url: '{pboot:msgaction}',
dataType: 'json',
data: {
contacts: contacts,
mobile: mobile,
content: content,
},
success: function(response, status) {
if (response.code) {
alert("谢谢您的反馈,我们会尽快联系您!");
$('#myform')[0].reset();
}
}
});
}
},
});
}
</script>
- 控制器代码:apps/api/controller/CmsController.php,添加代码
// 表单是否存在数据
public function checkForm()
{
// 获取参数
$acode = request('acode', 'var') ?: $this->lg;
$mobile = request('mobile', 'int');
// 读取数据
if ( !empty($mobile)) {
$resule = $this->model->checkForm($acode,$mobile);
if ($resule) {
json(1, '有数据!');
}else{
json(0, '没有数据!');
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/3a2a52bef5e048b5b00afd66968d4297.jpg?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBASVTlsYpCb3k=,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
- 模型代码:apps/api/model/CmsModel.php,添加代码
public function checkForm($acode,$mobile)
{
$where = array(
"acode='" . $acode . "'",
"mobile='" . $mobile . "'"
);
return parent::table('ay_message')->where($where)->select();
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/ece7e1dbf63e49c7bb0aa2d9dcfc9f1b.jpg?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBASVTlsYpCb3k=,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)