SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Oct 2005
    Location
    Home
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Checking to see if a checkbox is checked...

    Say I have an input selction like this:
    <input type="checkbox" name="Sam" /><br />
    <input type="checkbox" name="John' />

    Then I want to check to see which was selected in PHP. Something with IF/ELSE statements I think.

  2. #2
    Maniacally depressed robot poncho's Avatar
    Join Date
    Dec 2004
    Location
    Belfast, N.Ireland
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have those inputs as they are and you select both, the $_POST will contain:

    Code:
    Sam = on
    John = on
    If you only check John, it will not contain Sam.

    If you want the user to be able to select Sam and John, I would change your form slightly:

    Code:
    <form method="post">
    	<label for="sam">Sam: <input type="checkbox" name="Sam" id="sam" value="1" /></label><br />
    	<label for="john">John: <input type="checkbox" name="John" id="john" value="1" /></label><br />
    	<input name="submit" type="submit" value="submit" />
    </form>
    Try this in PHP:

    PHP Code:
    if(isset($_POST['Sam']) && isset($_POST['John'])) {
        echo 
    'you selected both Sam and John';
    } else if(isset(
    $_POST['Sam'])) {
        echo 
    'you selected Sam';
    } else if(isset(
    $_POST['John'])) {
        echo 
    'you selected John';
    } else {
        echo 
    'you did not select anyone';

    If you want the user to select Sam or John, the way I would do it is to have a radio with name as "name" and set the value to Sam or John, like so:

    Code:
    <input type="radio" name="firstname" value="Sam" />
    <input type="radio" name="firstname" value="John" />
    Then with PHP, you will only get one value "$_POST['firstname']".

    Cheers;
    Poncho
    Perfecting the art of breaking stuff.
    Check 'em: CakePHP | TextMate

  3. #3
    Non-Member
    Join Date
    Oct 2005
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Couldyou do this with an array of check boxes?


Bookmarks

Posting Permissions

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