thinkPHP form表单提交

<body>
<form autocomplete="off" id="myform">
    <input type="text" name="name" value=""/>
    <input type="text" name="phone" />
    <div οnclick="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';
    }
}
第一次就捐出去了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值