PHP 即点即改

lavarel5.4

这里写图片描述

路由:/routes/web.php

Route::get('/', function () {
    return view('welcome');
});
Route::any('show/index', 'ShowController@index');
Route::any('show/upd', 'ShowController@upd');

创建控制器ShowController.php

<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use App\Http\Models\Show;
class ShowController extends Controller
{
    public function index(){
        $show=new Show();
        $info=$show->getData();
        return view('show.index',['info'=>$info]);
    }
    public function upd(){
        $data=Input::all();
        $show=new Show();
        $res=$show->updRow($data);
        echo json_encode($res);
    }
}

创建模型Show.php

<?php
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;
use DB;
class Show extends Model
{
    public function getData(){
        return DB::table('news')->get();
    }
    public function updRow($data){
        $id=$data['id'];
        $res=DB::table('news')->where(array('id'=>$id))->update($data);
        $info=DB::table('news')->where(array('id'=>$id))->first();
        return $info;
    }
}

创建视图/resources/views/show/index.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
    <style>
    /*input{display: none;}*/
    </style>
</head>
<body>
<center>
    <table border="1">
        <tr>
            <td>id</td>
            <td>title</td>
            <td>content</td>
        </tr>
        <?php foreach($info as $k => $v){ ?>
        <tr>
            <td><?= $v->id ?></td>
            <td><span><?= $v->title ?></span><input type="hidden" name="hehe" data-id="<?= $v->id ?>" value="<?= $v->title ?>"></td>
            <td><?= $v->content ?></td>
        </tr>
        <?php } ?>
    </table>
</center>
</body>
</html>
<script src="{{URL::asset('/js/jquery.1.12.min.js')}}"></script>
<script>
    $(function(){
        $(document).on('click','span',function(){
        // $('span').click(function(){
            var obj=$(this);
            obj.hide();
            // obj.next('input').show().focus();
            obj.next('input').prop('type','text').focus();
        });
        $(document).on('blur','input',function(){
        // $('input').blur(function(){
            var obj=$(this);
            var title=obj.val();
            var id=obj.attr('data-id');
            $.ajax({
                type:'post',
                url:'upd',
                data:{title:title,id:id},
                dataType:'json',
                success:function(res){
                    // alert('修改成功');
                    obj.prev().html(res.title).show();
                    obj.prop('type','hidden');
                }
            })
        });
    })
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值