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.

<?php
$radio = "";
if (isset($_GET["db"])) {
    $radio = $_GET["db"];
}

if ($radio == 'domestic') {
    echo 'domestic';
} else {
    echo 'awsome';
}
?>
<html>
    <head>
        <script>
            function autoSubmit()
            {
                var formObject = document.forms['dbForm'];
                formObject.submit();
            }
        </script>
    </head>
    <body>
        <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    
        </form>
    </body>
</html>

 

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.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on JEE Tutorials | TwitterFacebook Google PlusLinkedin | Reddit | Email Me

Leave a Reply

Your email address will not be published. Required fields are marked *