Jump to content

[SOLVED] two submit buttons on one form and detect which one is pressed..?


Dragen

Recommended Posts

Hi,

My first question is more html than php but it links in with my next question so...

Is it possible to have two submit buttons on one form? I would use different names for them, one being name="submit" and the other being name="reset"..

Please note that it's not a reset button in the usual sense. I want it to submit the form as a submit button would, but as it's carrying a different name I want it to make the output different. They would both be type="submit"

 

This leads onto my second question.. is it possible to detect what the name of the submit button is?

I guess if I have two I could use something like:

<?php
if(isset($_POST['submit'])){
// code here
}elseif(isset($_POST['reset'])){
// code here
}
?>

 

would this work?

If you are trying to use 2 Submit Buttons you can use 2 separate forms for separate Submit Buttons.

e.g. <form><!-- Submit1 Button here --></form>

<form><!-- Submit2 Button here --></form>

And 2 form action will be Different or same.php?form=1 and same.php?form=2

 

thanks. It seems to be working how I thought though.. with the isset().

At the moment I've got:

<?php
if(isset($_POST['submit'])){
echo "submit";
}elseif(isset($_POST['reset'])){
echo "reset";
}else{
echo "<p align=\"center\">error: unspecified action</p>";
}
?>

which seems to work as when I click on the submit buttons it echoes the correct statement.

I'll have to see how it works once I add all my code in though!

The other way to do this is to give each "submit" button the same name, but different values:
[cpde]
<input type="submit" name="submit" value="Submit">
<input type="submit" name="submit" value="Reset">

 

Then in your script:

<?php
if (isset($_POST['submit']))
    switch($_POST['submit']) {
        case 'Submit':
               echo 'The "Submit" button was pressed';
               break;
        case 'Reset':
               echo 'The "Reset" button was pressed';
               break;
        default:
               echo 'Problem with the submit button -- wrong value -- ' . htmlentities($_POST['submit']);
               break;
   }
?>

 

Ken

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.