Search code examples
phpjavascriptcheckboxpostdata

PHP/Javascript Checkbox to change form action when checked


This is what I got:

<form action="invoiceCreate.php" method="post">
<input type="checkbox" name="business" id="business" vaulue="yes" />

Basically when I check the "business" checkbox, I want the form action to change to BusinessInoiveCreate.php instead of InvoiceCreate.php.

What would be the best way to do so?


Solution

  • $('#business').on('change', function(){
        if ($(this).is(':checked')) {
            $('form').attr('action', 'BusinessInoiveCreate.php');
        } else {
            $('form').attr('action', 'invoiceCreate.php');
        }
    });
    

    Working demo: http://jsfiddle.net/eV7vY/