Web Analytics Made Easy -
StatCounter getting values of checkboxes - CodingForum

Announcement

Collapse
No announcement yet.

getting values of checkboxes

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • getting values of checkboxes

    i have a many checkboxes named offer[] - each with a different value.
    I want to pull the values into an array.

    I am doing
    $arroffers=$POST['offer'];
    foreach ($arroffers as $offer)
    {
    echo "$offer";
    }

    but getting an error. what did I do wrong?

  • #2
    Originally posted by esthera
    but getting an error
    what error, exactly?

    Comment


    • #3
      I saw a write up on how to do a HTML Array to PHP Array via form elements. I wish i could remember how it was done. I will keep searching and try to find it.


      Found it: (Link for referrence)


      4. How do I get all the results from a select multiple HTML tag?

      The select multiple tag in an HTML construct allows users to select multiple items from a list. These items are then passed to the action handler for the form. The problem is that they are all passed with the same widget name. I.e. <select name="var" multiple="yes">

      Each selected option will arrive at the action handler as: var=option1
      var=option2
      var=option3

      Each option will overwrite the contents of the previous $var variable. The solution is to use PHP's "array from form element" feature. The following should be used: <select name="var[]" multiple="yes">

      This tells PHP to treat $var as an array and each assignment of a value to var[] adds an item to the array. The first item becomes $var[0], the next $var[1], etc. The count() function can be used to determine how many options were selected, and the sort() function can be used to sort the option array if necessary.
      Last edited by mymilkexpired; Aug 1, 2005, 10:57 AM.

      Comment


      • #4
        The form retrieval element should look like this:

        PHP Code:
        $arroffers $_POST['offer']; 

        Originally posted by esthera
        i have a many checkboxes named offer[] - each with a different value.
        I want to pull the values into an array.

        I am doing
        $arroffers=$POST['offer'];
        foreach ($arroffers as $offer)
        {
        echo "$offer";
        }

        but getting an error. what did I do wrong?

        Comment


        • #5
          that's what I am doing

          the error is Warning: Invalid argument supplied for foreach() in E:\hshome\\inboxphp\processform.php on line 21

          Comment


          • #6
            consider the following simple example:
            PHP Code:
            <?php
            if (!isset($_POST['submit']))
            {
                echo 
            '
                <form action="" method="POST">
                please check numbers:<br>
                <input type="checkbox" name="numbers[]" value="1"> 1<br>
                <input type="checkbox" name="numbers[]" value="2"> 2<br>
                <input type="checkbox" name="numbers[]" value="3"> 3<br>
                <input type="checkbox" name="numbers[]" value="4"> 4<br>
                <input type="checkbox" name="numbers[]" value="5"> 5<br>
                <input type="submit" name="submit" value="submit">
                </form>
                '
            ;
            }
            else
            {
                if (isset(
            $_POST['numbers']))
                {
                    echo 
            'you checked the following numbers:<br><br>';
                    foreach (
            $_POST['numbers'] as $value) {echo $value '<br>';}
                }
                else
                {
                    echo 
            'you did not check any numbers';
                }
            }
            ?>

            Comment


            • #7
              thanks -- i found my mistake.
              i was missing the _ before the post.

              thansk

              Comment

              Working...
              X