php checkbox help

Discussion in 'PHP' started by sg552, Jul 31, 2008.

  1. #1
    Hello,

    I need help with my code.
    Can somebody take a look at my code... :p

    What I want to do is when user select the checkbox, the script execute and print Sunday. If not selected the script will print Not Sunday.

    This is the form code. I only use 1 checkbox.

    <form>
    Tell me day:<input type="checkbox" value="sunday" name="day"><?php echo $_GET['day'];?>
    </form>
    PHP:


    the php form code

    $day = $_POST["day"];
    
    foreach ($day as 1)
          if ( $day == 1 ) {
    	echo "Sunday";
    } else {
    	echo "Not Sunday";
    }
    PHP:
    I think my php form code has error in it. I don't know becuase I'm not very good with php:p. Please help.

    Thanks in advance :)
     
    sg552, Jul 31, 2008 IP
  2. rob_v

    rob_v Peon

    Messages:
    72
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    not sure what you are trying to do but this will echo sunday :


    <form method=post action="test.php">
    <input type="checkbox" value="sunday" name="day">
    <input type="submit">
    </form>

    <?
    $day = $_POST["day"];
    echo "<BR>day = $day<BR>";

    if($day)
    echo "Sunday";
    else
    echo "Not Sunday";
    ?>
     
    rob_v, Jul 31, 2008 IP
    sg552 likes this.
  3. sg552

    sg552 Peon

    Messages:
    187
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thank you +rep added :)
     
    sg552, Jul 31, 2008 IP
  4. Pos1tron

    Pos1tron Peon

    Messages:
    95
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    EDIT: Beaten to the answer!
    But anyway, bug correction, while still echoing sunday when it is sunday, and not sunday when it isn't...

    <?php
    $day = $_POST["day"];
    
    if (strtolower($day) == 'sunday') {// loop removed because unnecessary, and this if statement made to mean something
        echo "Sunday";
    } else {
        echo "Not Sunday";
    }
    ?>
    PHP:
     
    Pos1tron, Jul 31, 2008 IP
    sg552 likes this.
  5. sg552

    sg552 Peon

    Messages:
    187
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5

    Hey thanks. I'm using your code instead and it's works perfect :D + rep to you!!

    Thanks :)
     
    sg552, Aug 3, 2008 IP