2

Possible Duplicate:
Multiple submit buttons in an HTML form

I've put together this form which, as you can see at the bottom of the page, allows a user to add and delete image files associated with the main record.

The problem that I have is that there are multiple buttons that have a 'Submit' action behind them, namely: 'Upload this File', 'Delete Selected Image', 'View Selected Image', and 'Submit' and because of this, immaterial of the button I use, the only action performed is a complete record submission.

How can I make sure that the 'Submit' action behind each button works independently of each other?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
IRHM
  • 1,326
  • 11
  • 77
  • 130
  • You are setting values to certain inputs with each button. Looks like you're always sending the form request to the same URL. So, what are you exactly worried about? – Alfabravo Dec 21 '11 at 16:35
  • Split your form into several different forms. – bfavaretto Dec 21 '11 at 16:36
  • 2
    Give them a `name` attribute and write your PHP code based on the presence of that query string item. – kapa Dec 21 '11 at 16:41
  • @bazmegakapa, many thanks for your reply. I must admit to being relatively new to PHP. I understand how to allocate a name to each button, but could yopu perhaps please elborate how I would tell the PHP to look for the name? – IRHM Dec 21 '11 at 16:43
  • 1
    @IRHM `if (isset($_POST['upload_file'])) { your code }` for example if your button has `name="upload_file"`. – kapa Dec 21 '11 at 16:44

3 Answers3

2

If you put every button in a separate <form></form> it should be fine. That may be possible in your current setup.

Assuming you can not do the above:

Form:

<form name='myForm'>
    <input type='hidden' id='hiddenSubmit' name='hiddenSubmit' value=''/>

    <!-- Lots of fields here --> 
    <input type='button' name='action1' onclick='changeHiddenSubmit(this.name)'/>

    <!-- Some more fields --> 
    <input type='button' name='action2' onclick='changeHiddenSubmit(this.name)'/>

    // etc...
</form>

JavaScript:

<script>
   function changeHiddenSubmit(name){
       document.getElementById('hiddenSubmit').value = name;
       document.myForm.submit();
    }
</script>

PHP:

<?php
    switch($_POST['hiddenSubmit'){

        case 'action1':

        break;

        case 'action2':

        break;
    }
?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Derk Arts
  • 3,432
  • 2
  • 18
  • 36
  • Hi, many thanks for taking the time to reply to my post. I'm not sure whether it's possible to use individual forms, is there no other way around this please? – IRHM Dec 21 '11 at 16:40
  • A solution could be to change them to regular buttons, and `onClick` set a hidden input to the name of the button that was clicked, and then `form.submit()`. In PHP check the value of the hidden field, and act accordingly. Do you follow? – Derk Arts Dec 21 '11 at 16:43
  • thanks for taking the time to send me your suggestion. I think I understand what you're saying. I'd have to have a look at this a little closer to fully get to grips with it. Kind regards – IRHM Dec 21 '11 at 16:46
  • Check my update answer, that should get you started – Derk Arts Dec 21 '11 at 16:51
  • I don't really see why should he use Javascript. In what way does this solution offer more than simply setting a `name` on the buttons (other than breaking your app if JS is turned off)? – kapa Dec 21 '11 at 16:54
  • I was under the impression that this is not foolproof, e.g. not all browsers handle the submission of submit button names in the same way. But I could be wrong. Too bad about the work though :) – Derk Arts Dec 21 '11 at 16:56
2

You don't need to use a separate form for each button. You can just use a different value in the "name" of the submit buttons. Then on the server you can examine the value of 'form_action'

Example:

<input name="form_action[delete]" type="submit" value="Delete">
<input name="form_action[update]" type="submit" value="Update">

Put as many of these buttons as you want. On the server, examine the value of $_POST['form_action'].

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dmitri Snytkine
  • 1,096
  • 1
  • 8
  • 14
  • I would say you should not check the value (with a multi-language site, it could change, etc)... Simply look for the existence of `$_POST['form_action']['delete']` for example. – kapa Dec 21 '11 at 17:10
  • Does `form_action` become an array in PHP? Can you [add](https://stackoverflow.com/posts/8593384/edit) some references to official documentation to your answer and/or elaborate on this? (But ***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today.) – Peter Mortensen Sep 18 '21 at 20:34
1

If you give your submit buttons a unique name, you can tell on the server side which one of them was clicked (resulting in the submission of the form).

Try this example:

<?php
    if (isset($_POST['b1'])) {
        echo 'Button 1 was pressed.';
    }
    if (isset($_POST['b2'])) {
        echo 'Button 2 was pressed.';
    }
    if (isset($_POST['b3'])) {
        echo 'Button 3 was pressed.';
    }
?>
<form action="" method="post">
    <input type="submit" name="b1" />
    <input type="submit" name="b2" />
    <input type="submit" name="b3" />
</form>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kapa
  • 77,694
  • 21
  • 158
  • 175
  • All, many thanks for taking the time to reply to my post. I've certainly received some fanstastic suggestions. The common theme seems to be to add a name to each button and then call the name in the PHP, so this is the approach I'm going to use. Once again amny thanks and kind regrads. – IRHM Dec 21 '11 at 16:57
  • Couldn't it be simpler? Using [the array `$_POST['name']`](https://stackoverflow.com/a/3314581)? – Peter Mortensen Sep 18 '21 at 20:53