简体   繁体   中英

How to prevent buttons from submitting forms when value is not selected. Without using javascript

Is there anyway of preventing buttons from submitting forms when the value is not selected, without using javascript?

Here is my code.

<!-- form -->
<form name="aform" action="second_page.php" method="POST">
    <div id="collegelane">
         Please choose only one from the following rooms:
    </div>

    <?php

         // connects to database
         require("user_connection.php");


         $q = mysqli_query($connect, "SELECT * FROM `campus`");
         while ($line = mysqli_fetch_assoc($q)) {
         echo '<input id="onclick" type="checkbox" name="car" 
            value="'.$line['room'].'"><label>'.$line['room'].'</label></br>';
        }

    ?>

    </br>
    </br>

    <div id="next">
        <input type="submit" name="next" Value="next"/>
    </div>

</form>

as it has already been suggested add a 'required' attribute on your checkbox as follows, it should work. Not sure about browser support for that attribute, i suspect it might not work on some version of IE.

<?php

             // connects to database
             require("user_connection.php");


             $q = mysqli_query($connect, "SELECT * FROM `campus`");
             while ($line = mysqli_fetch_assoc($q)) {
             echo '<input id="onclick" required type="checkbox" name="car" 
                value="'.$line['room'].'"><label>'.$line['room'].'</label></br>';
            }

        ?>

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