简体   繁体   中英

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.

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?');
}

在按钮单击处理程序中将onsubmit="promote(..."更改为onsubmit="return promote(..."

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM