2

I have a list of articles and I put a submit button to all of them.

The fact is I can't get the good input hidden when I'm in my controller because it will take the last input.

<form method="POST" action="{{url('/deleteArticle')}}">
@foreach($articles as $a)
        {{ csrf_field() }}

    <div class="test">
                <div class="name"><?= $a['name_a'] ?></div>
        <input type="hidden" name="id" id="ida" value='<?= $a['id_a'] ?>'/>
        <input type="submit" class="del" id="delA" class="cross" name="id">X</input>
    </div>
@endforeach
</form>

Instead of doing the trick with javascript, I thought about looping my articles and foreach of them having a form.

Is it a bad practice or should I have a single form with one submit button by article? The value of this submit button can't be "X" but only the ID of my article I want to delete, and this is bad for the display.

5
  • "The value of this submit button can't be "X" but only the ID of my article I want to delete, and this is bad for the display" -> What do you mean ? Commented Mar 5, 2019 at 18:55
  • In the code I gave if I submit I will have only the last id and not the one I'm clicking. I can change that by putting the value of my article on the value of my submit button but the display of the button will change by it's number but I want a cross. Commented Mar 5, 2019 at 18:57
  • 1
    Possible duplicate of Two submit buttons in one form. The solution is very simple: just make sure you give each button a unique name and/or unique ID. That's it :) Commented Mar 5, 2019 at 18:58
  • @paulsm4 I still need to put the value for the button which will erase my cross. Commented Mar 5, 2019 at 19:08
  • The multiple submit buttons with different values answer to the duplicate should work, just put the $a['id_a'] value into the "value" attribute. Commented Mar 6, 2019 at 3:30

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

Use html element button : <button value="id_of_your_current_article">Delete article</button>

Then do the job with simple Javascript.

2 Comments

If possible I don't want to use javascript
What's the problem with Javascript ? Then go for the one form per article... But that's still kinda weird

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.