Alan Coromano Alan Coromano - 3 months ago 17
HTML Question

Different action url for a button within one form

There are some buttons within a form. I want one of them to use another url as an action url. But I want it to be within that form, I don't want to and can't create another form for that.

How can I do that?

Answer

You can use JavaScript for this

<form name="my_form" method="post">
   Name <input type="text" name="person_name" /><br />
   <input type="button" value="Button 1" name="button_1" onclick="return target_btn1();">
   <input type="button" value="Button 2" name="button_2" onclick="return target_btn2();">
</form>

<script>
function target_btn1() {
    document.my_form.action = "whatever.php"; //Trigger action
    document.my_form.submit(); //Submit action performed
    return true;
}

function target_btn2() {
    document.my_form.action = "whatever2.php"; //Trigger action
    document.my_form.submit(); //Submit action performed
    return true;
}
</script>
Comments