문제

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
도움이 되었습니까?

해결책

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>";

}

다른 팁

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>";
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top