Two submit-button in one form

Hello,

in the code below I use radion buttons to EDIT the entries in a mysql table.

My question:
When I have a second submit-Button, DELETE: How can I use the same radio-button for both, EDIT and DELETE?

<form action=“editjoke.php” method=“post”>
<table>
<tr><th>Joke Text</th><th>Options</th></tr>

<?php
$jokes = @mysql_query($select . $from . $where);
if (!$jokes) {
echo ‘</table>’;
exit(‘<p>Error retrieving jokes from database!<br />’.
'Error: ’ . mysql_error() . ‘</p>’);
}

while ($joke = mysql_fetch_array($jokes)) {
//echo "<tr valign=‘top’>
";
$id = $joke[‘id’];
$joketext = htmlspecialchars($joke[‘joketext’]);
echo "<input type=‘radio’ name=‘id’ value=‘$id’ />$joketext<br />
";
echo "</tr>
";
}
?>

</table>

<input type=“hidden” name=“id” value=“<?php echo $id; ?>” />
<input type=“submit” value=“EDIT” />

A form can have 2 submits of you want it. Just give them different names, so you can’t differentiate them in your php code

e.g:


<form action="" method="POST">
  <input type="text" name="blah"/><br/>
  <input type="submit" value="submit1" name="submit1"/> <input type="submit" value="submit2" name="submit2"/>
</form>

Then in your php code you do this


if(is_set($_POST['submit1'])
{
  // do one thing with the form data
}
else if (is_set($_POST['submit2'])
{
  // do something different with the form data
}
else
{
  // uhm, print form?
}

If you have two submit buttons one with a value of edit and the other with a value of delete, then a simple branching statement can be used. You can give both buttons the same name and the when you check the post variable just check to see if $_PosT[‘submit’] is equal to edit or delete.

Is there a function in PHP is_set()? It should be isset() i think.

Yes this is the proper way to achieve the goal as far as i know too.


<input type="submit" name="btnSubmit" id="btnSubmit" value="Save Changes" />
<input type="submit" name="btnSubmit" id="btnSubmit" value="Delete Post" />

And in your PHP:


if($_POST['btnSubmit'] == 'Save Changes'){
    # do something here to save
}
if($_POST['btnSubmit'] == 'Delete Post'){
    # do something here to delete
}

Hope this makes you more clear.

Thank you for the answers!

I have two files: editjoke.php and deletejoke.php.

The variabe ‘id’ shoud be send to editjoke.php with the first submit-button and with the second to deletejoke.php.

How do I have to start the code?
Where do I write the second ‘action’?

Could it be something like this:
<form action=“editjoke.php” method=“post”>
<form action=“deletejoke.php” method=“post”>

you can also think of following way:

for submitting form you need to use DOM + javascript
i.e
To submit first form:

document.form[0].submit(); It will post the variable to set action file in <form>.

Ahh… No!

You should handle this in different way. Like you can have a simple button for Delete instead of Submit. And use javascript document.location to redirect to the delete page to delete along with the ID (deleting row ID).


<input type="submit" name="btnSubmit" id="btnSubmit" value="Save Changes" />
<input type="button" name="btnDelete" id="btnDelete" value="Delete Post" onClick="document.location='deletejoke.php?id=<?php echo $row_id;?>';" />

But be careful if the JS is disabled in the browser, it wont work.

Why not follow the same above method having two submit buttons and having only one script file which does both functions Edit and Delete??

Sorry,
but I have to go back at the beginning of my question. The script I posted on the 10th of November no more works. I was so happy but now I am very frustrated. I am a newbie and it is difficult for me but I would like to go on but I need some more help:

Now, please, at first talk once again about only one ‘edit’ button and one script file ‘editjoke.php’ I want to send the variabe ‘id’. The code I posted was:

<form action=“editjoke.php” method=“post”>
<table>
<tr><th>Joke Text</th><th>Options</th></tr>

<?php
$jokes = @mysql_query($select . $from . $where);
if (!$jokes) {
echo ‘</table>’;
exit(‘<p>Error retrieving jokes from database!<br />’.
'Error: ’ . mysql_error() . ‘</p>’);
}

while ($joke = mysql_fetch_array($jokes)) {
//echo "<tr valign=‘top’>
";
$id = $joke[‘id’];
$joketext = htmlspecialchars($joke[‘joketext’]);
echo "<input type=‘radio’ name=‘id’ value=‘$id’ />$joketext<br />
";
echo "</tr>
";
}
?>

</table>

<input type=“hidden” name=“id” value=“<?php echo $id; ?>” />
<input type=“submit” value=“EDIT” />

What’s wrong with it?

It should work like: echo "<td><a href=‘editjoke.php?id=$id’>Edit</a>

Thank you.