0

I´m generating some divs in an external .js file that are essential for my website. With those divs i create a button on a blade. This button shall call the Controller Method appendright() on click. I know that normally a form ist created to send some data like shown below.

<form method="POST" class="input-group" action="" enctype="multipart/form-data">
    <input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}"/>
    <input type="text" class="form-control col-lg-3" id="inputValue" name="inputValue" required>
    <div class="input-group-append">
      <button class="btn btn-success" type="submit" onclick="theneededMethod()">+</button>
    </div>
</form>

The Problem is that i append everything. All divs and the button are inside of ' ' marks. So trying to call the functon with {{ route('appendright()')}} or url does not work because the method is surrounded with ' ' marks as well.

$('#anchor').append('<div id="'+id+'" class="fontBoxHeading box index start">
   <div class="border defaultBorder">
      <div class="innerBox notSelected">
         <div class="text">
            <div class="textPadding">'+sitemapHome+'</div>
         </div>
      </div>
   </div>
   <form method="POST" class="input-group" action="" enctype="multipart/form-data">
      <input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}"/>
      <div class="input-group-append">
         <button class="btn btn-success" type="submit" onclick="{{ route('appendright()')}}">+</button>
      </div>
   </form>
</div>');

Maybe I just need to exclude those ' ' marks somehow. But i don´t know how. I just wannt to call a Controller function appendright().

  • 1
    Where and how are you using `{{ route('appendright()')}}`? The second block of code that you have provided does not show this. If you are indeed using this, you can get around it with [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) - however, this is unlikely to help you . See Jerodev's answer below. – Lewis Jun 12 '19 at 08:43
  • I edited the question. I just seem to not get it right when i call the function. – Misuke Ichiru Jun 12 '19 at 09:11
  • replace the '' with backticks. $('#anchor').append(`
    `);
    – Wim Pruiksma Jun 14 '19 at 14:14
  • SHi* can't use backticks in here. Well take a look here https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Template_literals – Wim Pruiksma Jun 14 '19 at 14:16

1 Answers1

3

You can't just call a server side PHP function from a client side JavaScript. If you want to do this you will need to use ajax calls from your JavaScript to reach the server side code.

Make sure you read about the differences between server side and client side code. For example, on this question: https://stackoverflow.com/a/13840431/743016

Jerodev
  • 32,252
  • 11
  • 87
  • 108
  • That was not the problem here. laravel can do that just fine. it was just an issue with a Variable name. i changed it an it works perfektly fine. – Misuke Ichiru Jun 13 '19 at 06:15