Question

How to pass one value of the current row of multiple hidden input in PHP. I have the following code:

foreach($portfolio as $portfolio){
echo  "<tr class ='table-comments'>";
echo  "<td>".$portfolio['portfolio_title']."</td>";
echo  "<td class = 'comment-content'>".$portfolio['portfolio_client']."</td>";
echo  "<td><a target = '_blank' href = ".$portfolio['portfolio_link'].">".$portfolio['portfolio_link']."</a></td>";
echo  "<td>";
echo  "<input type='hidden' name='portfolio_id' value='" . $portfolio['portfolio_id'] . "' />";
echo "<input type = 'submit'  value = 'Edit'>";
echo "<input type = 'submit' value = 'Move to Trash' class = 'action-button'>";
echo  "</td>";
echo  "</tr>";
}

I also have submit button each row that triggers the form. When I click the submit button, it submits all the value of the row of the hidden input. I only want the clicked button row value.

URL is like this:

/portfolio?portfolio_id=1&portfolio_id=2&portfolio_id=3&portfolio_id=4 and so on

I only want

/portfolio?portfolio_id=3
Was it helpful?

Solution

Have a new form for each row...

<form method="get">

</form>

Like this:

foreach($portfolio as $portfolio){

    echo  "<tr class ='table-comments'>";
    echo  "<td>".$portfolio['portfolio_title']."</td>";
    echo  "<td class = 'comment-content'>".$portfolio['portfolio_client']."</td>";
    echo  "<td><a target = '_blank' href = ".$portfolio['portfolio_link'].">".$portfolio['portfolio_link']."</a></td>";
    echo  "<td>";
    echo '<form method="get">';
    echo  "<input type='hidden' name='portfolio_id' value='" . $portfolio['portfolio_id'] . "' />";
    echo "<input type = 'submit'  value = 'Edit'>";
    echo "<input type = 'submit' value = 'Move to Trash' class = 'action-button'>";
    echo "</form>";
    echo  "</td>";
    echo  "</tr>";

}

OTHER TIPS

If all you're submitting is that ID, and you're using GET instead of POST you might as well not even use forms and use links instead. You can still style a link to look like a button if you want, but using a link would make a lot more sense semantically. Remove the hidden input, and have each link have the URL you want.

The form tag should be included on the looping.

change this

foreach ($portfolio as $portfolio) {
    echo  "<tr class ='table-comments'>";
    echo  "<td>".$portfolio['portfolio_title']."</td>";
    echo  "<td class = 'comment-content'>".$portfolio['portfolio_client']."</td>";
    echo  "<td><a target = '_blank' href = ".$portfolio['portfolio_link'].">".$portfolio['portfolio_link']."</a></td>";
    echo  "<td>";
    echo  "<input type='hidden' name='portfolio_id' value='" . $portfolio['portfolio_id'] . "' />";
    echo "<input type = 'submit'  value = 'Edit'>";
    echo "<input type = 'submit' value = 'Move to Trash' class = 'action-button'>";
    echo  "</td>";
    echo  "</tr>";
}

to:

foreach ($portfolio as $portfolio) {
    echo  "<tr class ='table-comments'>";
    echo  "<td>".$portfolio['portfolio_title']."</td>";
    echo  "<td class = 'comment-content'>".$portfolio['portfolio_client']."</td>";
    echo  "<td><a target = '_blank' href = ".$portfolio['portfolio_link'].">".$portfolio['portfolio_link']."</a></td>";
    echo  "<td>";
    echo  "<form action='process.php' method='get'>";
    echo  "<input type='hidden' name='portfolio_id' value='" . $portfolio['portfolio_id'] . "' />";
    echo "<input type = 'submit'  value = 'Edit'>";
    echo "<input type = 'submit' value = 'Move to Trash' class = 'action-button'>";
    echo "</form>";
    echo  "</td>";
    echo  "</tr>";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top