Laravel使用ajax将数据发送到mysql

时间:2017-01-27 14:52:45

标签: javascript jquery ajax laravel-5

我很难在ajax中创建一个函数,该函数将从变量“code2”获取一个值并将其发送到控制器,然后将控制器发送到数据库。我一直在努力的问题是:

  

这真的有效吗?哦顺便说一句:newLat:code2

所以它有点工作但不是newLat:code2它应该是newLat :(在变量code2中定义的html代码)那么我该如何解决这个问题?

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');
}); 

控制器:

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');

        return "Is This Really Working? Oh by the way: newLat: $newLat";
    return redirect('layouts/template', ['templates' => $templates]);
    }
}

刀片:

@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

1 个答案:

答案 0 :(得分:0)

检查 /path/to/project/root/storage/logs/laravel.log ,了解正在发生的内部服务器错误。如果没有日志文件,请确保 /path/to/project/root/.env 中的APP_DEBUG设置为true