Forum Moderators: coopster
Got a delete button that deletes a row from the table.
For each row i have also put a button in to each row.
So when pressed it deletes that particular row.
however, what ever button is pressed, always the last row is deleted.
The code is below:
When the button is pressed it calls another script called deleteShoppingSelection.php using the:
print "<td><INPUT TYPE=hidden NAME=variableName VALUE=".$row["shopid"]." SIZE=5>";
print" <td><input type=submit value='Delete This Record'";
print" <td><input type=button value='Back' onClick='history.go(-1)'>";
print "\n";
it then deletes the row based on the shopid it was passed, but like i said always the last one.
how to i tell it that if the delete button on the 3rd row is pressed then delete that one.
Thanks in advance.
I think your problem is that you're using the hidden field to determine the shopid number but if the variable name is the same then it gets assigned the last one.
Maybe somebody has a better way but I would think it would be best just to provide a link (or maybe your own image link).
<a href="deleteShoppingSelection.php?shopid="<?php echo $row['shopid']?>">Delete</a>
If you HTML looks a little like this, if I'm not mistaken, the form will send the last value of "VALUE."
<form action="delete.php">
...
<INPUT TYPE=hidden NAME=variableName VALUE="1" SIZE=5>
<input type=submit value='Delete This Record'">
...
<INPUT TYPE=hidden NAME=variableName VALUE="2" SIZE=5>
<input type=submit value='Delete This Record'">
...
<INPUT TYPE=hidden NAME=variableName VALUE="3" SIZE=5>
<input type=submit value='Delete This Record'">
...
</form>
daisho.
Or around each "Delete" button and hidden variable add <form></form> tags.
There you go. I knew somebody would have the answer.
Personally I like tables that have the ability to delete more then one row at a time by simply clicking a check box in that row then clicking submit. A little bit more complicated to code but alot easier for the end user.
Thanks in advance
for($j=0; $j<$nrows; $j++)
{
echo ( '<form action="deleteShoppingSelection.php" method="post">' );
$row = pg_fetch_array ($result);
printf ("<tr><td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tr>", $myrow['shopid'],$myrow['shopname'], $myrow['shopaddresslineone'], $myrow['shopaddresslinetwo'], $myrow['shopcounty'], $myrow['shoppostcode'], $myrow['shoptelephone'], $myrow['shopfax'], $myrow['shopemail'], $myrow['shopwebsite']);
print "<tr><td>" . $row["shopid"];
print "<td>" . $row["shopname"];
print "<td>" . $row["shopaddresslineone"];
print "<td>" . $row["shopaddresslinetwo"];
print "<td>" . $row["shopcounty"];
print "<td>" . $row["shoppostcode"];
print "<td>" . $row["shoptelephone"];
print "<td>" . $row["shopfax"];
print "<td>" . $row["shopemail"];
print "<td>" . $row["shopwebsite"];
print "<td><INPUT TYPE=text NAME=variableName VALUE=".$row["shopid"]." SIZE=5>";
print" <td><input type=submit value='Delete This Record'";
print" <td><input type=button value='Back' onClick='history.go(-1)'>";
//print "<td><a href="deleteShoppingSelection.php?shopid=".$row['shopid'].">Delete</a>";
print "<td><a href=\"deleteShoppingSelection.php?shopid=".$row['shopid']."\">Delete</a>";
print "\n";
}
for($j=0; $j<$nrows; $j++)
{
// NOTE: took out form tag here.
$row = pg_fetch_array ($result);
printf ("<tr><td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tr>", $myrow['shopid'],$myrow['shopname'], $myrow['shopaddresslineone'], $myrow['shopaddresslinetwo'], $myrow['shopcounty'], $myrow['shoppostcode'], $myrow['shoptelephone'], $myrow['shopfax'], $myrow['shopemail'], $myrow['shopwebsite']);
print "<tr><td>" . $row["shopid"];
print "<td>" . $row["shopname"];
print "<td>" . $row["shopaddresslineone"];
print "<td>" . $row["shopaddresslinetwo"];
print "<td>" . $row["shopcounty"];
print "<td>" . $row["shoppostcode"];
print "<td>" . $row["shoptelephone"];
print "<td>" . $row["shopfax"];
print "<td>" . $row["shopemail"];
print "<td>" . $row["shopwebsite"];
print "<td><form action=\"deleteShoppingSelection.php\" name= \"form" . $row['shopid'] . "\" method=\"post\">
<INPUT TYPE=\"hidden\" NAME=\"variableName\" VALUE=\"" . $row['shopid'] . "\">
<input type=\"submit\" value=\"Delete This Record\"></form>";
print" <td><input type=button value='Back' onClick='history.go(-1)'>";
print "\n";
}
Well I hope that works. It's untested. You should put in closed </td> tags where necessary.