Check boxex checked

Discussion in 'PHP' started by Shaimaa, Feb 16, 2010.

  1. #1
    Dear all,

    I have check boxes

    like:
    
         <input type="checkbox" name="day" value="Saturday" /><br/>
         Satur
         
         <input type="checkbox" name="day" value="Sunday" /><br/>
         Sun
         
         
         <input type="checkbox" name="day" value="Monday" /><br/>
         Mon
         
         
         <input type="checkbox" name="day" value="Tuesday" /><br/>
         Tues
         
         
         <input type="checkbox" name="day" value="Wednesday" /><br/>
         Wednes
         
         
         <input type="checkbox" name="day" value="Thursday" /><br/>
         Thurs
         
         
         <input type="checkbox" name="day" value="Friday" /><br/>
        Fri
    
    PHP:
    I want to get all check boxes to be used in next page using POST method... How can I do that?

    Thank You in advance
     
    Shaimaa, Feb 16, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    
    <?php
    //Get the value of day.
    $day = $_POST['day'];
    ?>
    PHP:
    
    <?php
    //check if day is Sunday...
    if($_POST['day']=="Sunday"){
    //proceed...
    }
    ?>
    PHP:
    
    <?php
    //check if day is selected...
    if($_POST['day']=="on"){
    //proceed...
    }
    ?>
    PHP:
     
    danx10, Feb 16, 2010 IP
  3. Darkhodge

    Darkhodge Well-Known Member

    Messages:
    2,111
    Likes Received:
    76
    Best Answers:
    1
    Trophy Points:
    185
    #3
    If I remember correctly you need to treat the POSTed variable as an array. But in order to do that you first need to change the name of the checkboxes to "day[]":

    
    <input type="checkbox" name="day[]" value="Saturday" /><br/>
         Satur    
    <input type="checkbox" name="day[]" value="Sunday" /><br/>
         Sun    
    <input type="checkbox" name="day[]" value="Monday" /><br/>
         Mon
    <input type="checkbox" name="day[]" value="Tuesday" /><br/>
         Tues
    <input type="checkbox" name="day[]" value="Wednesday" /><br/>
         Wednes
    <input type="checkbox" name="day[]" value="Thursday" /><br/>
         Thurs
    <input type="checkbox" name="day[]" value="Friday" /><br/>
        Fri
    
    HTML:
    The PHP code:

    
    <?PHP
    
    // Get POSTed array
    $day = $_POST['day'];
    
    ////////////////////////
    // EXAMPLE USAGE //
    ////////////////////////
    
    // If at least one element is present in the array
    if (count($day) > 0){
    
      // For each element
      foreach ($day AS $dayName){
    
        // Print it out
        print "$dayName Checked!<br>";
      }
    } 
    // Else notify the user that no elements were selected
    else {
      print "No days were checked!";
    }
    ?>
    
    PHP:
    Just remember to validate values for stuff like this if you're using a DB to prevent SQL injection etc...

    EDIT: I've attached the above script in a PHP file for you so you can see it in action. I didn't bother wasting time to format it etc... but it'll give you the gist of it.
     

    Attached Files:

    • test.php
      File size:
      817 bytes
      Views:
      40
    Last edited: Feb 16, 2010
    Darkhodge, Feb 16, 2010 IP