www.webdeveloper.com
Results 1 to 8 of 8

Thread: getting selected checkbox values

  1. #1
    Join Date
    Jun 2014
    Posts
    16

    getting selected checkbox values

    Here is some html that I have and I want to know how I can retrieve only the submitted checkboxes so that I can echo it onto a following page. Do I need to place the submit button inside of each checkbox form or can I place the <form> tag before the <table> and then have the submit button at the end of the <table>?

    HTML Code:
    <table>
    
         <tr>
        <td>
        <form>
    <input type="checkbox" name="checkbox[]">1</input>
    </form>
        </td>  
        <td>
         <form>
    <input type="checkbox" name="checkbox[]">2</input>
    </form>
        </td>
           <td>
         <form>
    <input type="checkbox" name="checkbox[]">3</input>
    </form>
        </td>
           <td>
         <form>
    <input type="checkbox" name="checkbox[]">4</input>
    </form>
        </td>
    </tr>
    </table>
    
    <form action="" method="post">
       <button type="submit" name="submit">Submit</button>
       </form>

  2. #2
    Uhm... why are you closing forms that aren't even open, and/or what forms are those input part of?!?

    Much less is that tables for layout? Why aren't you using the LABEL tag? Bad, just bad.

    IF there are no other form tags on the page, you need to get rid of EVERY SINGLE ONE of those <form></form> tags inside the table, and move the <form> tag BEFORE the table. As it sits right now, when you hit 'submit' the only thing being submitted for a value is $_POST['submit'] which won't even have a value. ... and I'm wondering why you're using a BUTTON for that... basically it looks like you have six separate forms on a page that should have only one.

    ... and you REALLY should learn some more HTML since it looks like you have no business even using a table there, and you're missing half the tags you should have in a form like LABEL, FIELDSET, LEGEND, INPUT[submit]...
    Last edited by deathshadow; 06-14-2014 at 09:31 PM.

  3. #3
    Join Date
    Jun 2014
    Posts
    16
    Okay so I changed my html and added some php code. Here is what I have:

    It works fine when submitted I can retrieve the selected checkbox value but my problem is that a user can go into the html and edit the value before they submit it. Is that something I should even worry about and if so what can I do to prevent that.

    HTML Code:
     <form action="" method="post">
        
    <input type="checkbox" name="checkbox[]" value="1"></input>
        </td>  
        <td>
    <input type="checkbox" name="checkbox[]" value="2"></input>
        </td>
           <td>
    <input type="checkbox" name="checkbox[]" value="3"></input>
        </td>
           <td>
    <input type="checkbox" name="checkbox[]" value="4"></input>
        </td>
              <td>
    <input type="checkbox" name="checkbox[]" value="5"></input>
        </td>
              <td>
    <input type="checkbox" name="checkbox[]" value="6"></input>
    
    
        </td>
        
        </tr>
    
        
    
        </table>
           <input type="submit" name="submit" value="submit"></button>
    PHP Code:
    <?php
    if(!empty($_POST['check_list'])) {
        foreach(
    $_POST['check_list'] as $check) {
                echo 
    $check;
        }
    }

  4. #4
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    851
    Still some pretty ugly html.

    no start table
    no end form
    orphanish end button tag - what the h for?
    no start row

    AND WHY are you even using a table, since you don't know how to code one and don't need one?

    AND your php is wrong. Don't you re-read what you type and sight-check your code?

    If this is the way you are going to write code - stop now!

  5. #5
    Join Date
    Jun 2014
    Posts
    16
    Quote Originally Posted by ginerjm View Post
    Still some pretty ugly html.

    no start table
    no end form
    orphanish end button tag - what the h for?
    no start row

    AND WHY are you even using a table, since you don't know how to code one and don't need one?

    AND your php is wrong. Don't you re-read what you type and sight-check your code?

    If this is the way you are going to write code - stop now!
    I copied and edited some of the code from a file on my computer when I uploaded it here. In the original file I do have the start table, row and </form>. The php is also correct in my original file I just forgot to change the $_POST['check_list'] to $_POST['checkbox'] when I uploaded it here.

    So can someone help me out with my previous question?

    "A user can go into the html and edit the value before they submit it. Is that something I should even worry about and if so what can I do to prevent that?"

  6. #6
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    851
    If you admit that the code you posted was wrong, how about posting the correct code now? That would be so helpful.

  7. #7
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    851
    How does this look?

    PHP Code:
    <?php
    $msg 
    '';
    if (!isset(
    $_POST['btn']))
    {
       
    DisplayPage();
       exit();
    }
    if (
    $_POST['btn'] <> 'Submit')
    {
        
    DisplayPage();
        exit();
    }
    if (empty(
    $_POST['checkbox']))
    {
        
    $msg "You didn't select any check boxes";
        
    DisplayPage($msg);
        exit();
    }
    $first true;
    echo 
    "You checked boxes:";
    foreach(
    $_POST['checkbox'] as $cb)
    {
        if (!
    $first)
            echo 
    ",";
        echo 
    $cb;
        
    $first = false;
    }
    exit();
    //************************
    //************************
    //************************
    function DisplayPage(
    $msg=null)
    {
        
    $code=<<<heredocs
        <!DOCTYPE HTML PUBLIC "
    -//W3C//DTD HTML 4.01//EN"
         
    "http://www.w3.org/TR/html4/strict.dtd">
        <
    html>
        <
    head>
        <
    title>Untitled</title>
        </
    head>
        <
    body>
        <
    h2>$msg</h2>
        <
    form method="POST">
        <
    input type="checkbox" name="checkbox[]" value="1"Item 1
        
    &nbsp;&nbsp;
        <
    input type="checkbox" name="checkbox[]" value="2"Item 2
        
    &nbsp;&nbsp;
        <
    input type="checkbox" name="checkbox[]" value="3"Item 3
        
    &nbsp;&nbsp;
        <
    input type="checkbox" name="checkbox[]" value="4"Item 4
        
    &nbsp;&nbsp;
        <
    input type="checkbox" name="checkbox[]" value="5"Item 5
        
    &nbsp;&nbsp;
        <
    input type="checkbox" name="checkbox[]" value="6"Item 6
        
    <br>
        <
    br>
        <
    input type="submit" name="btn" value="Submit">
        </
    form>
        </
    body>
        </
    html>
    heredocs;
        echo 
    $code;
        return;
    }
    Last edited by ginerjm; 06-15-2014 at 04:15 PM. Reason: left braces off of foreach loop...

  8. #8
    Join Date
    May 2014
    Posts
    77
    When I don't know what I'm doing, I always like to take the informative approach:

    PHP Code:
    <pre>
    <?php
    print_r
    ($_POST);
    ?>
    </pre>
    In this way you can see exactly how PHP is receiving and storing the data. I've been working with PHP for years and there's still nothing better for debugging than seeing what data you have available and how it's stored.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles