You don't need a separate form for each article, you don't need a hidden input, and you don't need JavaScript. Just use a button instead of an input like the other answer suggested. Any of those buttons will submit the form if they're clicked, and $_POST['id']
will have the value of the button that was clicked.
<button>
is different than <input>
because it's not a self-closing tag. With an <input>
, the value is the button text. But with a <button>
, you can give it a value, and then put the text you want it to have between the tags.
Here's an example based on your code.
<form method="POST" action="{{url('/deleteArticle')}}">
{{ csrf_field() }}
@foreach($articles as $a)
<div class="test">
<div class="name"><?= $a['name_a'] ?></div>
<button type="submit" class="del cross" name="id" value='<?= $a['id_a'] ?>' >X</button>
</div>
@endforeach
</form>
Unrelated to the question, I also fixed the repeated csrf_field and merged the two classes on the button.