1

i'm trying to submit a modal to the controller, and doing a @foreach loop but when i try to send the id of the corresponding item's id, it sends the last value of the id, always = 13 :-

<table class="table table-bordered">
               <thead>
                  <tr>
                     <th>ID</th>
                     <th>Name</th>
                     <th>Action</th>
                  </tr>
               </thead>
               <tbody>
                  @foreach($categories as $category)
                  <tr>
                     <td>{{ $category->id }}</td>
                     <td>{{ $category->name }}</td>
                     <td>  

                        <button class="myBtn btn btn-primary">Edit</button>

                        <button class="btn btn-danger">Delete</button>
                     </td>
                  </tr>
                  @endforeach
               </tbody>
            </table>
         </div>

so when i press on the edit button it always sends the last value of the id not the corresponding one

   <form action="{{ route('category.update', $category->id) }}" method="post">
              <div>
                  <label for="name">Name:</label>
                  <input type="text" id="Category Name" name="name">
              </div>
              <button type="submit" class="bn btn-success">Save</button>
              {{ csrf_field() }}
            </form>
Mohamed Gabr
  • 633
  • 6
  • 18

1 Answers1

2

Write onlick function which ll set route to your form:

<button data-url="{{ route('category.update', $category->id) }}" class="myBtn btn btn-primary" onclick="changeRoute({{ route('category.update', $category->id) }})">Edit</button>

Write this in your script, but you need to give id to your form as myForm

  <script>
    function changeRoute(url) {
     alert(url);
     $("#myForm").attr("action",url);
    }

     $(".myBtn").click(function() {
        var url = $(this).attr("data-url");
        console.log(url);
        $("#myForm").attr("action",url);

     });
    </script>
Omi
  • 3,544
  • 5
  • 18
  • 38
  • i did it but still shows the last $category->id , it doesn't send the id of the item which i clicked its button – Mohamed Gabr May 19 '17 at 11:51
  • it doesn't seem to execute the function down there, and i have set the id for the form – Mohamed Gabr May 19 '17 at 12:53
  • You don't need action for your form just remove that and check your form selector is right – Omi May 19 '17 at 13:59
  • does this seem to be right? – Mohamed Gabr May 19 '17 at 14:17
  • i get this error when i remove the Action attribute from the form " MethodNotAllowedHttpException in RouteCollection.php line 233:" – Mohamed Gabr May 19 '17 at 14:28
  • your changeRoute() function not getting called – Omi May 19 '17 at 15:53
  • yeah i guessed that, i also did the form ID the way u said and included it in the button the way u mentioned , and the script is the same, one thing is , u sure the ID attribute works with forms? check this out http://stackoverflow.com/questions/7470268/html-input-name-vs-id – Mohamed Gabr May 19 '17 at 16:05
  • Can you check the alter you getting right value, you can inspect to verify form has attach action after this function call – Omi May 19 '17 at 16:09
  • i don't quiet get what u mean, but i changed the value by adding 1 more category and its stuck with last value still which became 14 and then tried 2 console.log the url when the function is called, but it doesn't log anything? which means it doesnt get called, u sure ID works fine with Forms? – Mohamed Gabr May 19 '17 at 16:30
  • Check the updated answer i have added click event for button click now you dont need onclick attribute – Omi May 19 '17 at 16:35
  • now it works, thank you for your time :D im so grateful <3 – Mohamed Gabr May 19 '17 at 17:08