Jump to content

Trying to use multiple submit buttons in one form?


Emiiya

Recommended Posts

I'm creating an inventory page on a virtual pet site where your items are held, and right now I'm working on a Discard button so you can delete items. When you click [Discard], verification should show up at the bottom asking "Are you sure you want to permanently discard of this item?" and two more submit buttons are shown: Discard and Cancel. Each item has a certain ID number so that when you click Discard, it only discards that item and not every item, and I'm using php to grab the ID of the item you clicked Discard for so that the entry can be deleted from a table of items. I was able to successfully work discarding items before I added my own little verification system, but now that I added a second form, I can't get the buttons to work.

 

<?
$title2="Inventory";
include("mainlayout.php");
if ($notlogged==yes) {
echo "<center><h2><font class=\"header\">Sorry, this area is accessible only by members.</font></h2></center>"; }
else {
?>
<center>
<h1><font class="header">Inventory</font></h1>
<? 
$sql=mysql_query("SELECT * FROM inventory WHERE username='$username'");
if(mysql_num_rows($sql)==0){ echo "<center><font class=header>Your inventory is empty.</font></center>"; }
if(mysql_num_rows($sql) > 0){
echo "<table border=\"0\">";
echo "<tr>";
for($i=0;$i<=mysql_num_rows($sql);$i=$i+1){
while($row = mysql_fetch_array($sql)){
$itemcode = $row['itemcode'];                                              // These are just random variables used for making the
$category = $row['category'];                                               // images of the items show up.
$itemname = $row['itemname'];
$idofitem = $row['id'];                                                          // Here's where the item id is given a variable.
$i++;
if($i%6==0){
// use the operator to find the remainder of $i / 6 (%)
// if the remainder is 0; it is a multiple of 6, so make a new row.
echo "<td align=\"center\"><img src=\"http://midaevon.pcriot.com/images/items/$category/$itemcode.png\" /><br />$itemname<br />"; ?>
<form action="<? echo $PHP_SELF; ?>" method="post" name="discarditem">
<input type="hidden" name="idofitem" value="<? echo $idofitem; ?>">
<input type="submit" name="discarditem" value="[Discard]"></form>                       //first discard button
<? echo "</td></tr><tr>";
} else {
echo "<td align=\"center\"><img src=\"http://midaevon.pcriot.com/images/items/$category/$itemcode.png\" /><br />$itemname<br />"; ?>
<form action="<? echo $PHP_SELF; ?>" method="post" name="discarditem">
<input type="hidden" name="idofitem" value="<? echo $idofitem; ?>">
<input type="submit" name="discarditem" value="[Discard]"></form>                        //first discard button
<? echo "<td>";
}

}
}
echo "</tr></table>";
}
if (isset($_POST['discarditem'])) {                                                           // if you click discard for any item,
$idofitem2 = $_POST['idofitem']; ?>                                                       // new variable for the specific item discarded
<font color="red">Are you sure you want to permanently discard of this item?</font><br />
<form action="<? echo $PHP_SELF; ?>" method="post" name="yesorno">
<input type="submit" name="yesdiscard" value="Discard"> <input type="submit" name="cancel" value="Cancel"></form>
<? if (isset($_POST['yesdiscard'])) {                                                                  // if you click Discard,
mysql_query("DELETE FROM inventory WHERE id='$idofitem2'")                       // the item should be deleted
or die(mysql_error()); 
echo "This item has been permanently discarded.";
} 
else if (isset($_POST['cancel'])) {                                                                       // if you click cancel,
header( 'Location: redirect page goes right here' );                                          // you will be redirected back
} }
?>
</center>

</center>
<? } ?>
</div>
</body>

 

Neither the second Discard button nor the Cancel button work. When clicking the Cancel button, of course it redirects only because the form action is <? echo $PHP_SELF; ?>. If I delete the redirect part and just make it so that it echoes something when canceled, it doesn't work. Therefor, neither button works.

 

Any ideas?

 

I've tried to search for how to make a javascript confirm box when Discard is clicked, that sounds a lot easier, but I don't know much javascript. Any help with this option would be appreciated as well.

Link to comment
Share on other sites

I'm not sure if the same name will cause the issue, because they are techincally in both forms.  that would be like having two forms on a page, one for users to sign in ( name=username) and one to signup (name=username) etc...

 

It might be to do with the fact you're not closing hte <input>'s and thus its thinking that the second form is like a subform to the first, and thus getting rather confused.  Indenting code and html can often help with noticing when you've placed something in the wrong spot etc..

 

Also - it might help to add an onClick event to the buttons so you can alert() out where it's submitting to, that might help also.

 

Good luck.

Link to comment
Share on other sites

The first two Discard buttons are the same button - they are there twice because one of them is for all items in the first 5 columns of each row (there are 6 items per row in this table) and the second one is for the item which is the 6th in each row. Those aren't the buttons I'm talking about.

 

<form action="<? echo $PHP_SELF; ?>" method="post" name="yesorno">

<input type="submit" name="yesdiscard" value="Discard"> <input type="submit" name="cancel" value="Cancel"></form>

 

Those two buttons, which show up after you click [Discard] for any item, are the problem. :confused:

 

I'll try closing the input buttons, thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.