1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to get the value of a checkbox in PHP?

Discussion in 'PHP' started by eritrea1, Aug 31, 2012.

  1. #1
    Hi
    I am trying to create a, check-box for a user registration form.
    When user fills all fields, he/she will be given a check box to confirm, that says " I agree with the terms and conditions, But, before registering, I need PHP to check the value whether the checkbox was submitted or not.

    This is the HTML
    
    <form method="POST" action="testing.php" >
    I agree with the terms <input type="checkbox" name="chbox" value="1" /><br/>
    <input type="submit" name="check" />
    
    Code (markup):
    And this is the PHP

    
    <?php
    
    
    if(isset($_POST['check'])){
    
    
    $result = $_POST['chbox'];
    echo $result;
    
    
    }
    
    Code (markup):
    Now, this script will echo out, 1 if the checkbox is checked or Undefined index... if not.
    I need to know, if I am doing something wrong, because I don't want the error to appear if the checkbox is not checked. I can suppress the error by adding, @ before the variable, but I don't think that is a good codding practice.
     
    eritrea1, Aug 31, 2012 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    
    
    if (isset($_POST['check'])):
    
        if (isset($_POST['chbox'])):
            echo 'You need to select an option';
        else:
            $result = $_POST['chbox'];
            echo $result;
        endif;
    
    endif;
    
    
    PHP:
     
    ThePHPMaster, Aug 31, 2012 IP
  3. eritrea1

    eritrea1 Active Member

    Messages:
    182
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    70
    #3
    Thanks but if I click submit, while the checkbox is unchecked if gives me this error.

    Notice: Undefined index: chbox in C:\xampp\htdocs\DIRECTORY\TESTING\testing.php on line -- Refering this this code
      $result = $_POST['chbox'];
    Code (markup):
     
    eritrea1, Sep 1, 2012 IP
  4. Deltazon

    Deltazon Member

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    Because the code @ThePHPMaster gave you was wrong way round. You can either do !isset for the second if or change the statements.
     
    Deltazon, Sep 1, 2012 IP
  5. hassanahmad2

    hassanahmad2 Active Member

    Messages:
    243
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #5
    
    <?php
    
    if (isset($_POST['check'])) {
    
    	$result = false;
    
    	if (isset($_POST['chbox'])) {
    		$result = $_POST['chbox'];
    	}
    				
    	echo $result;
    }
    
    ?>
    
    Code (markup):
     
    hassanahmad2, Sep 1, 2012 IP
  6. eritrea1

    eritrea1 Active Member

    Messages:
    182
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    70
    #6
    Thanks guys. Specially hassanahmad2

    I twicked the code like this
     if (isset($_POST['check'])):
    
        if (!isset($_POST['chbox'])):
            echo '0';
        else:
            $result = $_POST['chbox'];
            echo $result;
        endif;
    
    
    endif;
    
    
    ?> 
    Code (markup):
    Hope it is secure.
     
    eritrea1, Sep 1, 2012 IP
    hassanahmad2 likes this.
  7. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #7
    You are correct, it should be !isset and not isset. Good catch!
     
    ThePHPMaster, Sep 1, 2012 IP
  8. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #8
    This is secure and I cut out what is not needed:

    Note that empty() checks if it is set and that it is null.
     
    DomainerHelper, Sep 1, 2012 IP
  9. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #9
    Would not stay formatted when I used the php shortcodes so had to use quotes.
     
    DomainerHelper, Sep 1, 2012 IP