Submit form JavaScript onChange

This tutorial example shows how to submit form automatically when a radio button is selected. The below example has two radio buttons and when one of them is selected then the form gets submitted and displays the corresponding value.

Sometimes we need to submit the form automatically and then we need to do some funther processing. We can submit form automatically on onClick, onChange javascript events which can be applied on many tags like <select/>, input type radio, input type checkbox etc.

$radio = "";
if (isset($_GET["db"])) {
    $radio = $_GET["db"];

if ($radio == 'domestic') {
    echo 'domestic';
} else {
    echo 'awsome';
            function autoSubmit()
                var formObject = document.forms['dbForm'];
        <form name='dbForm' id='dbForm'>
            <input type="radio" name="db" <?php if ($radio == 'domestic') { ?>checked='checked' <?php } ?>
                   value="domestic" onChange="autoSubmit();" />Domestic
            <input type="radio" name="db" <?php if ($radio == 'international') { ?>checked='checked'
                   <?php } ?> value="international" onChange="autoSubmit();" /> International    


The above code is straight forward and very easy to understand.

That’s all. Thanks for your patience. Please do not forget to leave a comment.

Leave a Reply

Your email address will not be published.