alertifyalertifyjs

Alertify confirm multiple submit form buttons


I would like to identify which button was clicked using Alertify confirm dialog. This is my form:

<form method='post' name='ImportExport' enctype='multipart/form-data'>
    <button onClick='return confirmAlert(event,this,\"Confirm IMPORT\")' type='submit' name='import' value='1'>Import</button>
    <button onClick='return confirmAlert(event,this,\"Confirm EXPORT\")' type='submit' name='export' value='1' >Export</button>
</form>

and my javascript:

function confirmAlert(e, elem, text) {
    var event = e || window.event;
    if(event) {
        event.preventDefault();
    }
    if(alertify) {
        alertify.confirm(text, function(e) {
            if(e) {
                if(elem.tagName === 'A') {
                    window.location = elem.href;
                } else if(elem.tagName === 'FORM') {
                    elem.submit();
                } else if(elem.tagName === 'BUTTON') {
                    elem.form.submit();
                }
                return true;
            } else {
                return false;
            }
        }).setting({
            'title': 'Dialog',
            transition: 'zoom',
            'labels': {
                'ok': 'OK',
                'cancel': 'Cancel'
            }
        });
    }
}

Form is submitted but $_POST on backend missing clicked button. How to submit form with Alertify containing which button was clicked?


Solution

  • I see that only one possible solution is add to form hidden input like this way:

    else if(elem.tagName === 'BUTTON') {
        jQuery('<input>').attr('type', 'hidden').attr('name', elem.name).attr('value', elem.value).appendTo(elem.form);
        elem.form.submit();
    }
    

    then it works as expected.