jquery - laravel 不使用 ajax 插入到 mysql

标签 jquery mysql ajax laravel laravel-5

我坚持将 js 变量 (code2) 的值保存到数据库中,一切似乎都正常,我没有收到任何错误,一旦执行了操作,我就会收到警报,它是 ($newLat) 的输出,这是一个包含 html 代码的变量 (code2) 的实际值,但是没有任何内容被插入到数据库中。如果我尝试手动将相同的代码插入数据库,一切正常。

JS:

$(function() {
    $('.content-link').click(function(e) {
        e.preventDefault();
        $('#content-link2').load($(this).attr("href"), function() {
            $('#content').draggable({
                containment: "#content-link2",
                scroll: false
            });
        });
    });
    return false;
});
var code2 = "";
document.getElementById("content-link2").onmousedown = function() {
    mousedown();
};
document.getElementById("content-link2").onmouseup = function() {
    mouseup();
};

function mousedown() {
    code2 = document.getElementById("content-link2").innerHTML;
    console.log(code2);
}

function mouseup() {
    code2 = document.getElementById("content-link2").innerHTML;
    console.log(code2);
}

Ajax :

function updateDatabase(newCode)
{
    code2 = document.getElementById("content-link2").innerHTML;
    console.log(code2);
    // make an ajax request to a PHP file
    // on our site that will update the database
    // pass in our lat/lng as parameters
    $.post('http://localhost/template', {
            _token: $('meta[name=csrf-token]').attr('content'),
            newCode: ("code2"),
        })
        .done(function(code2) {
            alert(code2);
        })
        .fail(function() {
            alert("error");
        });
}

路线:

Route::group(['middleware' => ['web']], function () {

    Route::get('home', 'BuilderController@homepage');
    Route::get('template', 'BuilderController@templates');
    Route::post('template', 'BuilderController@postDB');
    Route::get('logout', 'BuilderController@getLogout');
}); 

Controller :

class BuilderController extends Controller
{
    function templates()
    {
        $templates = Template::all();
        return view('layouts/template', ['templates' => $templates]);
        $id = $templates->id;
    }
    function homepage()
    {
        return view('layouts/home');
    }
    public function getlogout()
    {
    \Auth::logout();
    return redirect('/home');
    }
     public function postDB(Request $request) {
        $newLat = $request->input('newCode');
        $templates = new Template();
        $templates->file = $newLat;
        $templates->save;
        return $newLat;
    }
}

Blade :

@extends('layouts.master') @section('title', 'Website Builder') @section('content')
<div class="container template_class ">
    @foreach ($templates as $template)
    <a class="content-link" href="{{ asset($template->file )}}">
        <img src="{{ asset($template->image )}}"/>
        </a> @endforeach

</div>
<div class="features form-group">
    <input class="filestyle form-control margin images" data-input="false" type="file" data-buttonText="Upload Logo" data-size="sm" data-badge="false" onchange="readURL(this);" />

    <button onClick=" updateDatabase(this);"</button>
  <script>
  $( function() {
    $( document ).tooltip();
  } );
  </script>
    <button style="display: none" class="form-control margin btn btn-primary" id="showColor">Show Colors</button>
    <button style="display: none" class="form-control margin btn btn-primary" id="hideColor">Hide Colors</button>
    <input title="Choose a color and then click on any box" style="display: none" class="btn btn-default form-control margin" type="color" id="colorChoice">
    <a style="display: none" href="#" class="btn btn-default form-control margin" id="cp4">Background</a>

    <button style="display: none" onclick="$('#fonts1').bfhfonts({font: 'Arial'})" id="fontsShow" class="btn btn-primary form-control margin">Load Fonts</button>
    <button style="display: none" class="btn btn-primary form-control margin" id="fontsHide">Hide Fonts</button>
    <select title="Choose a font and then click on any box" style="display: none" id="fonts1" class="form-control margin"></select>

    <button style="display: none" onclick="$('#googlefonts1').bfhgooglefonts({font: 'Lato'})" id="googleShow" class="btn btn-primary form-control margin">Google fonts</button>
    <button style="display: none" class="btn btn-primary form-control margin" id="googleHide">Hide Google</button>
    <select title="Choose a font and then click on any box" style="display: none" id="googlefonts1" class="form-control margin"></select>

    <button style="display: none" onclick="$('#fontsizes1').bfhfontsizes({fontsize: '12'})" id="sizeShow" class="btn btn-primary form-control margin">Load font size</button>
    <button style="display: none" class="btn btn-primary form-control margin" id="sizeHide">Hide font size</button>
    <select title="Choose a font size and then click on any box" style="display: none" id="fontsizes1" class="form-control margin"></select>

    <button style="display: none" class="form-control margin btn btn-default" id="finishEdit">Done</button>
    <button class="form-control margin btn btn-default" id="startEdit">Edit</button>

    <button type="button" class="form-control margin btn btn-warning" id="getRequest">Save</button>
</div>
<div id="content-link2"></div>

</body>
<link href="{{asset('css/bootstrap-colorpicker.min.css')}}" rel="stylesheet" type="text/css">
<link href="{{asset('css/bootstrap-formhelpers.min.css')}}" rel="stylesheet" type="text/css">
<link  href="{{asset ('//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css')}}" rel="stylesheet" type="text/css">
<script type="text/javascript" src="{!! asset('js/bootstrap-colorpicker.min.js') !!}">
</script>
<script type="text/javascript" src="{!! asset('js/bootstrap-formhelpers.js') !!}">
</script>
<script type="text/javascript" src="{!! asset('js/template.js') !!}"></script>
<script type="text/javascript" src="{!! asset('js/bootstrap-filestyle.min.js') !!}">
</script>

</html>
@endsection

最佳答案

寻找缺少的括号:

public function postDB(Request $request) {
    $newLat = $request->input('newCode');
    $templates = new Template();
    $templates->file = $newLat;
    $templates->save(); // <--- HERE
    return $newLat;
}

关于jquery - laravel 不使用 ajax 插入到 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41899537/

相关文章:

php - 当前时间秒

MySQL 全文搜索返回 0 个结果

jquery - 添加到动态生成的有序列表行的 CSS 样式

javascript - 跨域请求不适用于 Safari 中的 SoundCloud

php - 卡在 Laravel 查询中

php - ZF2 中的 Zend\Db 如何控制事务?

javascript - Rails - 无法在失败的 ajax 表单提交上显示错误

java - 关于weblogic portlets功效的一个问题

javascript - 将 ajax 调用中的 JSON 数据显示为 HTML

jquery - 我的链接变成了特殊字符