html - Check-box form validation and submitting the form itself in PHP -
the following program php i've written asks user select languages know, , displays selected languages in same page itself. able display error message if user has not selected languages. need display selected check-box items i.e, lanaguages result in same page itself. example: languages know english, french. can in this?
<?php $languages_error = $languages_result = $languages_list = ""; if($_server['request_method'] == 'post') { if(empty($_post["languages"])){ $languages_error = "*select languages know"; } else { $languages_result = "languages selected are"; } } ?> <!doctype html> <html> <head> <title>check box page</title> </head> <body> <div id="contact"> <h1>what languages know?</h1><br> <form action="checkbox.php" method="post"> <fieldset> <input type="checkbox" name="languages[]" id="languages" value="english"><label = "gender">english</label></br> <input type="checkbox" name="languages[]" id="languages" value="spanish"><label = "gender">spanish</label></br> <input type="checkbox" name="languages[]" id="languages" value="french"><label = "gender">french</label></br> <input type="checkbox" name="languages[]" id="languages" value="germany"><label = "gender">germany</label></br> <input type="checkbox" name="languages[]" id="languages" value="chinese"><label = "gender">chinese</label></br> <?php echo $languages_error, $languages_result ?></br> <input type="submit" value="submit"> </fieldset> </form> </div> </body> </html>
the selected languages (checkboxes) submitted , available php array
.
to access these, can loop through them, , echo such:
$languages_result = $_post['languages']; foreach ($languages_result $language) { echo $language . '<br />'; } // result is: // english // telegu // kanada
you can combine multiple elements within array, in string
, using delimiter:
$languages_result = implode(', ', $_post['languages']); echo $languages_result; // result is: // english, telegu, kanada
Comments
Post a Comment