vendredi 27 janvier 2017

laravel not inserting into mysql by using ajax

I am stuck with saving a value from js variable (code2) into a database, everything seems to work I am not getting any errors and once actions are executed I get alert which it an output from ($newLat) which is an actual value of variable (code2) that contains html code however nothing is inserted into a database. If I try to insert same code into a database manually everything works fine.

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:

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="">
        <img src=""/>
        </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="" rel="stylesheet" type="text/css">
<link href="" rel="stylesheet" type="text/css">
<link  href="" 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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire