Unique ID in html for generating Buttons

205 Views Asked by At

sorry if the title is misleading.

I'm having the following problem. I am creating multiple rows in HTML using Genshi. For each row I have a button at the end of the row for delete purposes.

The code looks like this:

<form action="/deleteAusleihe" method="post">      
<table>
  <tr>
    <th>ID</th>
    <th>Person</th>
    <th>Buch</th>
    <th></th>
  </tr>

<tr py:for="v in verleihen">
  <input type = "hidden" value="v.id" name="toDelete"/>
          <td py:content="v.id">Vorname der Person</td>
          <td py:content="v.kundeID">Name der Person</td>
          <td py:content="v.buchID">Straße der Person</td>
          <td>
          <input type="submit" name="submit" value="Löschen"/>
          </td>
          <br/>  
</tr>
</table>
</form>

The input type ="hidden" should store the value of each id so I am able to identify the row later on.

When I try to delete now, and lets assume I have 2 rows filled, I get 2 id's as a paramater, which is logical to me, but I don't know how to solve it.

The deleteAusleihe function looks like this:

@expose()
def deleteAusleihe(self,toDelete,submit):
    Verleih1 = DBSession.query(Verleih).filter_by(id=toDelete)
    for v in Verleih1:
        DBSession.delete(v)
        DBSession.flush()
        transaction.commit()
    redirect("/Verleih")

Thanks in advance for your help!

2

There are 2 best solutions below

0
Daniel Roseman On BEST ANSWER

The issue is that all the hidden inputs inside the <form> element get submitted at once.

There are various ways you could solve this. Probably the easiest would be to move the form tag inside the loop, so that there are multiple forms and each one only wraps a single input and button.

0
3bu1 On
<table>
  <tr>
    <th>ID</th>
    <th>Person</th>
    <th>Buch</th>
    <th></th>
  </tr>

<tr py:for="v in verleihen">
<form action="/deleteAusleihe" method="post"> 
  <input type = "hidden" value="v.id" name="toDelete"/>
          <td py:content="v.id">Vorname der Person</td>
          <td py:content="v.kundeID">Name der Person</td>
          <td py:content="v.buchID">Straße der Person</td>
          <td>
          <input type="submit" name="submit" value="Löschen"/>
          </td>
          <br/>  
</form>
</tr>
</table>
</form>

Here is the code.