janlindso
janlindso

Reputation: 1239

Confirm submit to specific page

I have a form that has two different submit-buttons that should submit to different pages.

HTML:

<form id="campaign" method="post" enctype="multipart/form-data">

<input type="submit" value="Promote!" name="ap_promote" onsubmit="promote('?page=campaigns&id=<?php echo $campaigns_id?>&edit=true&test=2')" id="ap_promote">

</form>

Javascript:

function promote(action)
    {
        if (confirm('Are you sure you want to promote this campaign?'))
        {
            document.getElementById('campaign').action = action;
            document.getElementById('campaign').submit();
        }
        else
        {
            return false;
        }
    }

As you see, it should send the form to ?page=campaigns&id=#&test=2. The problem is that it doesn't show any confirmation box and it just sends the form to itself, and not to the specified url.

Upvotes: 0

Views: 87

Answers (3)

dfsq
dfsq

Reputation: 193261

Buttons don't have onsubmit event, it's a form event. Since you plan to have different actions per depending on clicked button, you can use combination of button onclick and form onsubmit events. Check it out:

<form id="campaign" method="post" enctype="multipart/form-data" onsubmit="return promote()">
    <input type="submit" value="Promote!" name="ap_promote" onclick="this.form.action='one'" id="ap_promote" />
    <input type="submit" value="Promote!" name="ap_promote" onclick="this.form.action='two'" id="ap_promote" />
</form>

And JS code becomes as simple as:

function promote() {
    return confirm('Are you sure you want to promote this campaign?');
}

Upvotes: 2

raji ramamoorthi
raji ramamoorthi

Reputation: 443

I hope this will help you. You should use the button instead of submit button

<form id="campaign" method="post" enctype="multipart/form-data">

<input type="button" value="Promote!" name="ap_promote" onclick="promote('?page=campaigns&id=<?php echo $campaigns_id?>&edit=true&test=2')" id="ap_promote">

</form>

Upvotes: 0

Laserson
Laserson

Reputation: 533

Change onsubmit="promote(..." to onsubmit="return promote(..." in your button click handler

Upvotes: 1

Related Questions