Jump to content

Recommended Posts

I have another question. when I got for example in a form a submit button I usually use the action in the form and thats in the HTML to allow it to go for the page that I would like.

I am wondering what if I have two buttons that they do two different actions. How I can handle this.

Link to comment
https://forums.phpfreaks.com/topic/5403-more-than-one-button-into-one-form/
Share on other sites

[!--quoteo(post=356884:date=Mar 21 2006, 08:44 AM:name=firedrop)--][div class=\'quotetop\']QUOTE(firedrop @ Mar 21 2006, 08:44 AM) [snapback]356884[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I have another question. when I got for example in a form a submit button I usually use the action in the form and thats in the HTML to allow it to go for the page that I would like.

I am wondering what if I have two buttons that they do two different actions. How I can handle this.
[/quote]


Hi,

don't use any action or use action = "<?echo $PHP_SELF?>" to call the same script. Then use an if ... elseif ... else - construct like

[code]

<?php

# Submit - Button #1
if (isset($_POST[submit_button_1]) {

   # action for submit - button #1

} // if

# Submit -  Button #2
elseif (isset($_POST[submit_button_2]) {

   # action for submit - button #2

} // if

# Standard - form
else {

   ?>

   <form method = "POST">
  
        .......

   </form>

   <?

} // else

?>

[/code]

Give buttons the same name then check the value to get clicked button

[code]<?php

if (isset($_POST['submit'])) {

    switch ($_POST['submit']) {
            case 'A':
                  echo 'Button A was pressed<br>';
                  break;
            case 'B':
                  echo 'Button B was pressed<br>';
                  break;
    }

}
?>
<FORM METHOD='POST'>
<INPUT TYPE='SUBMIT'  name='submit' value='A'>
<INPUT TYPE='SUBMIT'  name='submit' value='B'>
</FORM>[/code]
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.