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.

Zeyukan Ich'
  • 651
  • 7
  • 21
  • "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 ? – PHPnoob Mar 05 '19 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. – Zeyukan Ich' Mar 05 '19 at 18:57
  • 1
    Possible duplicate of [Two submit buttons in one form](https://stackoverflow.com/questions/547821/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 :) – paulsm4 Mar 05 '19 at 18:58
  • @paulsm4 I still need to put the value for the button which will erase my cross. – Zeyukan Ich' Mar 05 '19 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. – Nick Mar 06 '19 at 03:30

2 Answers2

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.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
2

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

Then do the job with simple Javascript.

PHPnoob
  • 586
  • 3
  • 7