版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Thekingyu/article/details/79865761
<body> <form autocomplete="off" id="myform"> <input type="text" name="name" value=""/> <input type="text" name="phone" /> <div onclick="ajaxPost()" >11</div> </form> </body> <script> function ajaxPost(){ var formData = $("#myform").serialize(); //serialize() 方法通过序列化表单值,创建 URL 编码文本字符串,这个是jquery提供的方法 console.log(formData) $.ajax({ method:"post", dataType:"json", url:"{:url('index')}", data:formData,//这里data传递过去的是序列化以后的字符串 success:function(data){ console.log(data) } }); } </script>
<?php namespace app\index\controller; use think\Controller; use think\Request; class Index extends Controller { public function index() { if (request()->isPost()) { var_dump(input(''));die; $data['name'] = input('name'); $data['phone'] = input('phone'); if(empty($data['name'])){return array('info' => '不能为空!', 'status' => -1);} if(empty($data['phone'])){return array('info' => '不能为空!', 'status' => -1);} if(is_string($data['phone'])){return array('info' => '不能为空!', 'status' => -1);} $res = db('index')->insert($data); if ($res) { return array('info' => '新增成功!', 'status' => 1, 'target' => 'back'); } else { return array('info' => '新增失败!', 'status' => -1); } }else{ return $this->fetch(); } // return '28822'; } public function test() { return '333'; } }第一次就捐出去了