Skip to content Skip to sidebar Skip to footer

Multiple Submit Buttons On A Form

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 f

Solution 1:

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'].

Solution 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:

<formname='myForm'><inputtype='hidden'id='hiddenSubmit'name='hiddenSubmit'value=''/><!-- Lots of fields here --><inputtype='button'name='action1'onclick='changeHiddenSubmit(this.name)'/><!-- Some more fields --><inputtype='button'name='action2'onclick='changeHiddenSubmit(this.name)'/>

    // etc...
</form>

JavaScript:

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

PHP:

<?phpswitch($_POST['hiddenSubmit'){

        case'action1':

        break;

        case'action2':

        break;
    }
?>

Solution 3:

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:

<?phpif (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.';
    }
?><formaction=""method="post"><inputtype="submit"name="b1" /><inputtype="submit"name="b2" /><inputtype="submit"name="b3" /></form>

Post a Comment for "Multiple Submit Buttons On A Form"