thinkphp+Ajax 实现页面刷新不跳转提交

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>wangEditor demo</title>
</head>

<body>
<p>标题:</p><input type="text" name="title" id="titles" style="width: 100%; height: 20px;">

<div id="editor">
    <p>欢迎使用 <b>wangEditor</b> 富文本编辑器</p>
</div>

<input type="radio" class="recom" name="identity" value="1" checked="checked">推荐
<input type="radio" class="recom" name="identity" value="0">不推荐

<button class="one" type="button">提交</button>
<br/>
<textarea id="contents" name="content" style="display: none;"></textarea>

<script src="/static/js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="/static/js/wangEditor.min.js"></script>

<script type="text/javascript">
    var E = window.wangEditor;
    var editor = new E('#editor');

    var $text1 = $('#contents');
    editor.customConfig.onchange = function (html) {
        // 监控变化,同步更新到 textarea
        $text1.val(html)
    };

    editor.customConfig.debug = true;
    editor.customConfig.uploadFileName = 'image';                       //上传的文件名
    // 或者 var editor = new E( document.getElementById('editor') )
    editor.customConfig.uploadImgServer = "/hfback/after/uploads";  // 上传图片到服务器
    editor.customConfig.uploadImgMaxSize = 3 * 1024 * 1024;         //图片的尺寸
    editor.customConfig.uploadImgMaxLength = 5;                     //最多上传多少
    editor.customConfig.uploadImgParams = {
        type: 'editor'  // 属性值会自动进行 encode ,此处无需 encode
    };
    editor.create();
    $text1.val(editor.txt.html())

    //点击提交按钮
    $(".one").click(addUser)

    function addUser() {
        var addhelp = {
            //要先获取标题 内容
            titles: $("#titles").val(),
            contents: $("#contents").val(),
            recommend: $("input[name='identity']:checked").val()
        };
        //提交内容到服务器
        $.ajax({
            url: "/hfback/After/present",
            data: addhelp,
            type: "post",
            dataType: "json",
                            //mgs 是返回的数据存储在mgs
            success: function (mgs) {
                //弹窗提示
                console.dir(mgs)
                
                // 提交是否成功 给个判断
                if (mgs["one"] == '1') {
                    alert('添加成功');
                } else {
                    alert('添加失败')
                }
            }
        })
    }
</script>
</body>
</html>

后台控制器方法,获取过来的值,插入数据库当中

public function present()
{
    $data =Request::only(["titles","contents","recommend"]);

    $values = [
        'title' => $data['titles'],
        'content' => $data['contents'],
        'recommend' => $data['recommend'],
        'time' => time()
    ];

    $validate = new addition;
    if (!$validate->check($values)) {
        $this->success(($validate->getError()), '/hfback/after/addition');
    }

    $aaa = Db::table('hf_help')
        ->insert($values);

    if($aaa == 1){
        return json(["one" => 1]);
    }else{
        return json(["one" => 2]);
    }
}

转载于:https://my.oschina.net/u/4003338/blog/2251961

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值