tags:

views:

54

answers:

4

Hi, this is a simple html question but I would like to know the correct way of doing it.

I have a table in my page, with tabular data, and it's a display of items in the database. I build the table dynamically with php, and just output html.

I want to add a column "delete this entry" with a button/link/picture on every line, and when it's clicked, my page refreshes and php knows which itemID should be deleted.

The question is not about php, but about the html part.

How can I make the delete-button compliant with standards? Here's an incorrect solution:

<table>
<form action='#' method='post'>
<tr>
<td>Item 1</td>
<td><input type='hidden' name='id' value='1'><input type='submit' value='X'></td>
</tr>
</form>
<form action='#' method='post'>
<tr>
<td>Item 2</td>
<td><input type='hidden' name='id' value='2'><input type='submit' value='X'></td>
</tr>
</form>
</table>

Anyone an idea on a good solution? I'd prefer not to use much javascript, if it can be avoided. Thanks in advance!

Edit: actually, I have a secret second question too: what if i were to want to do inline editing (after pressing an Edit button), as in: make the cells with the values contain boxes, and a submit button at the last cell?

+1  A: 

You could go with a simple GET procedure, like this:

<a href="showResults.php?action=delete&item=112">Delete</a>

Note the action and item values in the URL. These would be evaluated before the database is called and the results are printed in showResults.php. Your PHP (on showResults.php) would start with a block of logic that checks to see if any actions were requested, such as delete. If an action is present, it checks to see if an item is as well. I will then do whatever authentication necessary to ensure the user has sufficient rights to do this action to this item.

Next it would query the database (now lacking said item assuming the deletion was successful) and redraw the page. The links would be built within your loop used to create the many table-rows:

<?php foreach ($products as $product) { ?>
<tr>
  <td>
    <a href="sr.php?action=delete&item=<?php print $product->id; ?>">Delete</a>
  </td>
  <td>
    <p><?php print $product->title; ?></p>
  </td>
</tr>
<?php } ?>
Jonathan Sampson
Yes, I could, but that's bad because google accesses that link when it scrapes. I admit it probably won't scrape dynamic pages, but still it's not a good solution in my opinion.
Tominator
Tominator, don't show the link unless the user browsing is authenticated.
Jonathan Sampson
If the link is only available to authenticated users, Google will never know it exists.
Jonathan Sampson
It's true what you say about google. This is probably the best solution. Thanks :) Would have an idea about the 'secret question' I added in the edit?
Tominator
Tom, your secret question isn't that difficult. Ask it next, as an entirely new question :)
Jonathan Sampson
Actually one of the other repliers gave me the solution to the 'secret', so I can combine all of you into one solution in my head. Thanks!
Tominator
Excellent. Good work, Tominator!
Jonathan Sampson
A: 

Add a submit button to each form and send the operation you want to execute as value attribute of each button

<table>
    <form action='#' method='post'>
        <tr>
            <td>Item 1</td>
            <td><input type='hidden' name='id' value='1'><input type='submit' value='X'></td>
            <td><button type="submit" value="remove" name="op">Remove</button></td>
            <td><button type="submit" value="edit" name="op">Edit</button></td>
        </tr>
    </form>
</table>
Juraj Blahunka
Thanks, but the reason I said my presented solution is bad, is that it's not allowed to put a <form> tag between a table and a tr.
Tominator
what about wrapping the whole table in a form and then add for each tr a simple button with value="del-1231" or value="edit-1231" which is easily parsable, this is complete valid solution
Juraj Blahunka
True, it's a good solution indeed
Tominator
Did you know that in IE, it submits the text between the <button></button>, and not the value=''? And in an input type=submit, I can't say value='edit-1234' because that would look ugly..
Tominator
well you can still distinguish between various inputs with "name" attribute (name=edit value=123, name=delete value=123)
Juraj Blahunka
but the value part is what will be shown to the user..?
Tominator
you're right, completely forgot about that.. :-D
Juraj Blahunka
+1  A: 

Why not isolate the forms and pass the ID to the action page?

<table>
    <tr>
        <td>Item 1</td>
        <td><form action='action.php?id=1' method='post'><input type='submit' value='X'></form></td>
    </tr>
    <tr>
        <td>Item 2</td>
        <td><form action='action.php?id=2' method='post'><input type='submit' value='X'></form></td>
    </tr>
</table>
Agent_9191
This works, but I have a secret second question: what if i were to want to do inline editing, as in: make the <td> cells with the values contain <input type='text'> boxes, and a submit button at the last cell?
Tominator
Will the input boxes be rendered on the page from the server, or will it be a javascript function to make the cell contents editable? Just wondering because it can change the way to make this work.
Agent_9191
On the server in this case.. the ajax solution will be later on :) But I've gathered the required intel already from the other posters, but thanks anyway!
Tominator
A: 

It's kind of messy, but going off of what Agent said, you could do something like this to get a text input in there too:

<form action='action.php?id=1' method='POST'>
<table>
    <tr>
       <td>Item 1</td>
       <td><input type="text" value="" /></td>
       <td><input type='submit' value='X'></td>
    </tr>
</table>
</form>

<form action='action.php?id=2' method='POST'>
<table>
    <tr>
       <td>Item 2</td>
       <td><input type="text" value="" /></td>
       <td><input type='submit' value='X'></td>
    </tr>
</table>
</form>
munch
I doubt this would keep my layout intact, but Agent suggested to use 1 form, with the table in it, and multiple submit buttons, each with a unique value-string, to parse in php later on. Thanks for the help though!
Tominator
you can **always** match the design to fit the functionality ;)
munch