2017-01-27 11 views
0

js変数(code2)の値をデータベースに保存していると、すべてがうまくいくように見えます。エラーが発生せず、アクションが実行されると警告が表示されますfrom($ newLat)はhtmlコードを含む変数(code2)の実際の値ですが、データベースには何も挿入されません。同じコードをデータベースに手動で挿入しようとすると、すべて正常に動作します。laravel ajaxを使用してmysqlに挿入しない

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', '[email protected]'); 
    Route::get('template', '[email protected]'); 
    Route::post('template', '[email protected]'); 
    Route::get('logout', '[email protected]'); 
}); 

コントローラー:

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

ブレード:かっこが欠落ため

@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 
+2

$用テンプレートをしてみてください>(保存)。 –

答えて

5

ルック:

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

作品<3ありがとう – Przemek

関連する問題