Jump to content

Help with advancing this program

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
kentona

kentona

    Newbie

  • Members
  • PipPip
  • 19 posts
I am at the moment making a program where in a html file there are 5 checkboxes for the user to choose from. Each checkbox represents a letter so in mine it goes A, B, C, D, E. When the user clicks on the submit button it takes you to a php page that displays what numbers they checked.

Here is the html code ive done,


<html>

<div id="entryform">

    <form method="GET" action="phpprogram.php">

        <label for="option1"></label><input type="checkbox" name="option1" value="A"> A<br />

        <label for="option2"></label><input type="checkbox" name="option2" value="B"> B<br />

        <label for="option3"></label><input type="checkbox" name="option3" value="C"> C<br />

        <label for="option4"></label><input type="checkbox" name="option4" value="D"> D<br />

        <label for="option5"></label><input type="checkbox" name="option5" value="E"> E<br />

        <br />

        <input type="submit" name="submit" value="Show Letters" />    

    </form>

</div>


</html>


The php i haven't started because im really not sure how to do this. Many thanks if someone can get me started and show me how i would do this.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
I'll assume you're trying to use POST instead of GET (POST is the preferred method). First of all checkboxes can be placed into its own array for simplicity, notice how I use "options[]" to handle each option:
<html>
<div id="entryform">
    <form method="POST" action="phpprogram.php">
        <label for="option1"></label><input type="checkbox" name="options[]" value="A"> A<br />
        <label for="option2"></label><input type="checkbox" name="options[]" value="B"> B<br />
        <label for="option3"></label><input type="checkbox" name="options[]" value="C"> C<br />
        <label for="option4"></label><input type="checkbox" name="options[]" value="D"> D<br />
        <label for="option5"></label><input type="checkbox" name="options[]" value="E"> E<br />
        <br />
        <input type="submit" name="submit" value="Show Letters" />    
    </form>
</div>

</html>
phpprogram.php:
<?php 
//If form was sent
if(isset($_POST['options'])) {
    //Iterate through options[] array
    foreach($_POST['options'] as $opts) {
        //handle each option
        print $opts . " selected.<br/>";    
    }
}
?>
This will list each option selected.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
kentona

kentona

    Newbie

  • Members
  • PipPip
  • 19 posts
Thanks a lot for the help appreciate it. I'm starting to understand this a bit more so i shall play around with it a bit see what else i can do. :)

#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
Additionally, the label tag isn't really used the best way....
<input type="checkbox" name="options[]" value="A" id="option1"><label for="option1"> A</label><br />
i.e. the label shall be around the text connected to the checkbox, and the checkbox needs an id attribute so the label knows which input it's a label for
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall