0

I am retrieving a html code from my website and I need to store it into a database. Code to retrieve looks like this:

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

And output is:

    <title>Template 1</title>
        <link href="http://localhost/fyproject/public/templates/css.css" rel="stylesheet" type="text/css">


    <div class="logo">
        <img class="images" id="image" src="#" alt="Your Logo">
    </div>
<div contenteditable="true" id="content" class="draggable ui-widget-content refresh ui-draggable ui-draggable-handle" style="position: relative;"><p>hlo</p></div>
<div id="comments">
<form name="forma">
<textarea name="commentUser" id="commentUser" class="refresh" cols="40" rows="5">Comments here...
</textarea><br>
<input type="submit" value="Ready!" id="send-data"><!--onClick="writeComment(e);"--> 
<div id="editTxt" class="refresh" contenteditable="true">
<p>This text can be by the user.</p>
</div>
</form></div>

I have started to do AJAX code which is saved in js file and it looks like:

 function updateDatabase(newCode)
            {

            // 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/fyproject/public/template2', {
                 _token: $('meta[name=csrf-token]').attr('content'),
                 newCode: code2,
             }
            )
            .done(function(data) {
                alert(data);
            })
            .fail(function() {
                alert( "error" );
            });
        }

Route:

Route::post('template2', 'BuilderController@postDB');

Controller:

     public function postDB(Request $request) {
        $newLat = $request->input('newCode');

        return "Is This Really Working? Oh by the way: newLat: $newLat";
    }

However I get this error in a console:

POST http://localhost/fyproject/public/template2 500 (Internal Server Error)

Am I getting at the right track? What am I doing wrong here? If this is not good could I get a suggestion how to do this in another way?

Thank You

Przemek
  • 834
  • 6
  • 21
  • 49
  • 500 error can mean anything. Try to see where your code fails by putting echo/print/dump statements in your `postDB` function. Don't have a lot of experience with AJAX, but your code looks pretty fine to me. – Loek Jan 19 '17 at 15:44
  • What happens if you attempt to visit that URL directly? – random_user_name Jan 19 '17 at 15:44
  • 2
    Check the server's error logs, turn on PHP error reporting, check any error trapping mechanism that Laravel uses, etc. "500 Internal Server Error" means that there was an error server-side and it's not telling you what it is. You need to find out what that error is. – David Jan 19 '17 at 15:45
  • 1
    Possible duplicate of [How can I make PHP display the error instead of giving me 500 Internal Server Error](http://stackoverflow.com/questions/2687730/how-can-i-make-php-display-the-error-instead-of-giving-me-500-internal-server-er) – random_user_name Jan 19 '17 at 15:45
  • I change code to redirect me to a same page so instead of "template2" I redirect to "template" and it gives me this error: POST http://localhost/fyproject/public/template 405 (Method Not Allowed) – Przemek Jan 19 '17 at 15:57

0 Answers0