2017-01-27 5 views
0

Ich bin stecken mit dem Erstellen einer Funktion in AJAX, die einen Wert von Variable "Code2" nehmen würde und senden Sie es an den Controller, die es an die Datenbank senden würde. Problem Ich bekomme die ganze Zeit ist:Laravel senden Daten an mysql mit AJAX

Ist das wirklich funktioniert? Ach übrigens: newLat: code2

So ist es Art aber statt newLat funktioniert: code2 sollte es newLat sein: (HTML-Code in Variable code2 definiert) Wie kann ich dieses Problem beheben?

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

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

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

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 
+0

scheint wie etwas, mit dem Token falsch gelaufen ist, versuchen Sie die Token mit Ajax-Header einzurichten: '$ .ajaxSetup ({ Header: { 'X-CSRF-token': $ (" Meta [name = "csrf -token "] '). attr (' Inhalt ') } });' – Troyer

+0

Immer noch der gleiche Fehler – Przemek

+0

Überprüfen Sie Ihre '/ storage/logs/laravel.log' Datei :) – Troyer

Antwort

0

prüfen /path/to/project/root/storage/logs/laravel.log für das, was Interner Serverfehler auftritt. Wenn keine Protokolldatei vorhanden ist, stellen Sie sicher, dass APP_DEBUG in /path/to/project/root/.env auf true festgelegt ist.

+0

Dies sollte eher ein Kommentar als eine Antwort sein. Sobald Sie ausreichend Repräsenz haben, können Sie jeden Beitrag kommentieren. Bis dahin konzentrieren Sie sich auf Fragen, die klar sind und direkt beantwortet werden können. – empiric

Verwandte Themen