php - Laravel: Change the div with ajax and multiple [email protected]

434

I want to change thediv's content withAJAX (I don't want to reload all of the page when I click on the links), i found some documentations, but I only saw "static" solutions (they are "hard coded" that "if you click on this bring this", but I don't want to use a 3000 row switch-case on the bottom of my project).

Someone can show me a "dynamic" solution where I only have to give the controller, action and the parameters to the on-click and thejquery router makes the routing without tinkering?

My example code:

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
  <head>
    @include('includes.head')
  </head>
  <body>
    <div id="header">
     <nav id="navbar" >
      <ul >
        <li>
          <a action="[email protected]" params="[a => 24, b => 52]">
                        <button type="button" class="btn btn-link">First Example</button>
                    </a>
                </li>
                <li>
                    <a action="[email protected]" params="[id => 1, newValue => 42]">
            <button type="button" >Second Example</button>
          </a>
        </li>
     </nav>
    </div
    <div id="app">
      <!-- This will be changed by the router -->
    </div>
    <footer >
      @include('includes.footer')
    </footer>

    <!-- Scripts -->
    <script src="{{ asset('js/app.js') }}"></script>
  </body>
</html>

Controller Actions

class FirstExampleController extends Controller{

    public function firstExample(Request $request){
        $a = $request -> a;
        $b = $request -> b;

        $c = $a + $b;

        return $c;
    }
}

class SecondExampleController extends Controller{

    public function secondExample(Request $request){
        $id = $request -> id;
        $newValue = $request -> newValue;

        //database operation where the id's object's new value will be $newValue

        return $this->showItems;
    }
}

People are also looking for solutions to the problem: php - Multiple If-Else statement skipping the other statements

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.