4

My database table (Related) contains 3 columns:

  • related_id
  • article_id
  • object_id

It's a table which keeps track of relationships between articles and objects. I have stripped the code. Now it just contains the delete button (x). If someone press that button I want the user to be redirected to if(isset($_POST['deleteRelated'])) to get a "Are you sure"-message etc. But the hidden ID isn't passed correctly. The last related_id in the table is 29. When I try to echo out the hidden ID i just get 29 for every delete button (x).

The complete version of the code below gives me a table with the article title, object title and the delete button (x). Because of a submit button can't pass a value by itself I need a hidden value. But when I pass it by pressing the delete button (x) i just gets 29 every time.

Code

if(isset($_POST['deleteRelated'])) {

    echo $_POST['hidden-id']; // Will just display the last 'related_id' value.

else {

    echo '
    <form method="post">';

    $stmt = $db->prepare("SELECT * FROM Related");
    $stmt->execute();
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

    foreach ($result as $related) {

        echo '
        <input type="hidden" name="hidden-id" value="' . $related['related_id'] . '">
        <input type="submit" name="deleteRelated" value="x">';

    }

    echo '
    </form>';

}

If I type:

<input type="submit" name="deleteRelated" value="' . $related['related_id'] . '">

It will display the correct value from the database instead of the x for the delete/submit button. But when I press the delete/submit button I just get the last related_id which is 29.

Can someone solve this problem? Should not be that hard?

Treps
  • 780
  • 3
  • 12
  • 28

5 Answers5

4

Explanation

You have one <form> for the entire table, which includes (say) a dozen <input type="hidden" name="hidden-id" value="...">s.

Those values will ALL be sent to the server when the form is submitted (how do you expect it to know to only send the hidden-id which is next to the specific submit button that was pressed?) This is why you are only seeing the last hidden-id – they are all being sent, so the final one overrides/wins.

Solution

One solution would be to have a <form> per row instead of one <form> for the whole table:

foreach ($result as $related) {

    echo '<form method="POST" action="...">';

    echo '
    <input type="hidden" name="hidden-id" value="' . $related['related_id'] . '">
    <input type="submit" name="deleteRelated" value="x">';

    echo '</form>';

}

That way, only the hidden-id value of the pressed button would be sent.

Shai
  • 7,159
  • 3
  • 19
  • 22
  • I just have one hidden value at the whole page. But it's working to put the
    inside the loop. Thank you very much mate. :)
    – Treps Oct 08 '14 at 14:29
  • @Treps Surely you have several hidden values on the page – one in each iteration of the loop = one in each row! – Shai Oct 08 '14 at 14:58
  • Aha, smart. I didn't tought about that. – Treps Oct 08 '14 at 20:42
2

Alternatively, you really don't need a form for each row, you could use a button here instead and ditching those hidden inputs.

Example:

foreach ($result as $related) {
    echo '<button type="submit" name="deleteRelated" value="' . $related['related_id'] . '">Delete</button>';
}

So now each value of that pressed button on each row will go to:

$related_id = $_POST['deleteRelated'];
Kevin
  • 41,694
  • 12
  • 53
  • 70
2

You will have to put the in the foreach. In your code, you have one form with a lot of button submit. Try this:

foreach ($result as $related) {

echo '
<form method="POST" action="...">
<input type="hidden" name="hidden-id" value="' . $related['related_id'] . '">
<input type="submit" name="deleteRelated" value="x">
</form>';}
VincentDEJ
  • 239
  • 2
  • 10
2

If I understood you right, why do you need multiple hidden inputs? you might want to put this input once:

<input type="hidden" name="hidden-id" value="" />

Next, change the submit button to something like this:

echo '<input type="submit" name="deleteRelated" value="x" onclick="setValue('" . $related['related_id'] . "'); />';

And use a js function:

setValue(val) {
    document.getElementsByName("hidden-id")[0].value = val;
}
Gargamel
  • 58
  • 4
1

Easy solution: Put your form tag inside foreach loop

$stmt = $db->prepare("SELECT * FROM Related");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($result as $related) 
{
    echo '<form method="post">';

    echo '
        <input type="hidden" name="hidden-id" value="' . $related['related_id'] . '">
        <input type="submit" name="deleteRelated" value="x">';

    echo '</form>';
}
George G
  • 7,443
  • 12
  • 45
  • 59