Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Checkbox clicked Not required

    Hi all,

    I wonder if anyone could give me some idea: I have a form and one of the filed is an email (must field) if empty an error message will show up: - Now I want to add a CHECKBOX where when the CHECKBPX is clicked the "must field" email will become" not must field one" (Not required field).

    I have this code as my snippets:

    Code:
    <form action="<?php echo $PHP_SELF;?>" method="post" name="reg1">
    ..
    <tr>
    <td><input name="noemail" type="checkbox" value="noemail">
     Do not want to get the E-mail address</td>
    </tr>
    														<tr>
    	<td nowrap>Enter email 
    <input name="email1" type="text" <?php if ($_POST['action'] == "register") { echo 'value="'.$_POST['email1'].'"'; } ?>>
       </td>
    </tr>
    ..
    </form>
    PHP Code:
    ..
    ..
    $error 0
    $errormsg "";
    ..
    ..
    if (
    $_POST['action'] == "register") {

    if ((
    $_POST['email1'] == "") OR (!check_text($_POST['email1']))) {
        
    $error 1;
        
    $errormsg .= "Please enter your requested email address<br>";
        
    $errornum[3] = 1;
        }
    }
    .. 
    Can anyone assist me what shall I add so I can achieve this please?

    Thanks in advance.
    Siabanie

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    A wee bit unsure of what you want, but I think I get it. You want a checkbox that will say 'don't want to provide email'? Why not just make the email field not required. That way, if people want to leave an email, they can and if they don't, they leave it blank? Best solution in terms of usability, especially if you label the field "Email: (Leave blank if you don't want to list it)" or something.

  • #3
    New Coder
    Join Date
    Apr 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your reply BluePanther,

    Well ideally I'd like that email to be "must field" but some people do not have the email that I "specific" here unless they have registered it before... so for those have not register yet will be able to click the "checkbox" as not required.

    Means when the user clicked that checkbox the email field will become from "required field" to "not required field" - I think we can do it in JS or something? I have shown my code as above but have no idea how to achieve as I wanted here.

    Any idea?

  • #4
    New Coder
    Join Date
    Apr 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have come up with something e.g:

    Code:
    <form action="<?php echo $PHP_SELF;?>" method="post" name="reg1">
    ..
    <tr>
    <td><input name="noemail" type="checkbox" value="y">
     Do not want to get the E-mail address</td>
    </tr>
    														<tr>
    	<td nowrap>Enter email 
    <input name="email1" type="text" <?php if ($_POST['action'] == "register") { echo 'value="'.$_POST['email1'].'"'; } ?>>
       </td>
    </tr>
    ..
    </form>
    PHP Code:
    ..


    $error 0;
    $errormsg "";
    ..
    ..
    if (
    $_POST['action'] == "register") {
    ..
    ..
    if ((
    $_POST['email1'] == "")  && ($_POST['noemail'] == "y")) {
        
    $error 1;
        
    $errormsg .= "Please enter your requested email address<br>";
        
    $errornum[3] = 1;
        }
    }
    .. 
    .. 
    What I want to achieve is:

    If the user ticks box, they do not have to fill in the email address field, but if the box is not tick means the fill MUST fill in otherwise an error message will show.

    I tried the code above but it did not work...anyone please?

  • #5
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    I'm sure checkboxes are checked as either on or off. Can't remember off the top of my head.

    ($_POST['noemail'] == ON) means it's been checked/clicked, OFF is the opposite.

  • #6
    New Coder
    Join Date
    Apr 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Nightfire View Post
    I'm sure checkboxes are checked as either on or off. Can't remember off the top of my head.

    ($_POST['noemail'] == ON) means it's been checked/clicked, OFF is the opposite.
    Thanks for your reply Nightfire,

    I have tried as you suggested. But if didn't tick the box and tick the box the error message still showing up.

    I tried to put ON and OFF shown here but it did not seem to work - do I have to changed in the form as well e.g: value = "ON" and as well as value = "OFF". It is still giving me an error message either I click the checkbox or not.

    Code:
    <tr>
    <td><input name="noemail" type="checkbox" value="ON">
     Do not want to get the E-mail address</td>
    </tr>
    PHP Code:
    ..
    if ((
    $_POST['email1'] == "")  && ($_POST['noemail'] == "ON")) {
        
    $error 1;
        
    $errormsg .= "Please enter your requested email address<br>";
        
    $errornum[3] = 1;
        } 
    .. 

  • #7
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    checkboxes are parsed by checking first if it is empty() ( I THINK SOMEONE PLEASE CONFIRM THIS ). So, checking for your checkbox is done by
    PHP Code:
    if(!empty($_POST['noemail'])){
        
    // Checkbox was ticked, can confirm value now if you want

    But, I still maintain this feature isn't needed. Setting a field to 'non-required' by ticking a checkbox next to it seems like extra work for no gain. Why not just have the heading for the field something like "Email: If you're already registered, leave blank"?

  • #8
    New Coder
    Join Date
    Apr 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BluePanther View Post
    checkboxes are parsed by checking first if it is empty() ( I THINK SOMEONE PLEASE CONFIRM THIS ). So, checking for your checkbox is done by
    PHP Code:
    if(!empty($_POST['noemail'])){
        
    // Checkbox was ticked, can confirm value now if you want

    But, I still maintain this feature isn't needed. Setting a field to 'non-required' by ticking a checkbox next to it seems like extra work for no gain. Why not just have the heading for the field something like "Email: If you're already registered, leave blank"?
    Thanks BluePanther, well you see the purposed of the checkbox is to tell user if they have register or not.

    So the default would be:
    If the user tick the checkbox that means they do not have to fill the email address as they are not a register user.

    But if they did not tick the box means they are a register user therefore, they must fill in that email field otherwise an error message will show.

    I hope I make it clear why I need the checkbox here. I have tried putting the form value as value = "ON" or Value = "y"

    and code it like e.g:

    PHP Code:
    ..
    ..
    if ( empty(
    $_POST['email1']) || !check_text($_POST['email1']) && ($_POST['noemail']=="ON") ){
    .. 
    or even like this:

    PHP Code:
    if ( empty($_POST['email1']) || !check_text($_POST['email1']) &&  ($_POST['noemail'] == "y")) {
    ..
    .. 
    but none of these are working......

  • #9
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by siabanie View Post
    Thanks BluePanther, well you see the purposed of the checkbox is to tell user if they have register or not.

    So the default would be:
    If the user tick the checkbox that means they do not have to fill the email address as they are not a register user.

    But if they did not tick the box means they are a register user therefore, they must fill in that email field otherwise an error message will show.

    I hope I make it clear why I need the checkbox here. I have tried putting the form value as value = "ON" or Value = "y"

    and code it like e.g:

    PHP Code:
    ..
    ..
    if ( empty(
    $_POST['email1']) || !check_text($_POST['email1']) && ($_POST['noemail']=="ON") ){
    .. 
    or even like this:

    PHP Code:
    if ( empty($_POST['email1']) || !check_text($_POST['email1']) &&  ($_POST['noemail'] == "y")) {
    ..
    .. 
    but none of these are working......
    Try this:
    PHP Code:
    if(empty($_POST['email1'])){
        
    // Email field was empty
        
    if( empty($_POST['noemail']) ){
            
    // Checkbox wasn't clicked
        
    }
        else if(
    $_POST['noemail'] == 'y'){
            
    // Checkbox has correct value
        
    }
        else {
            
    // Unknown checkbox value
        
    }
    }
    else{
        
    // Something in email field
        // You could check for the checkbox being checked in here as well
        // Or, assume if they entered an email, they wanted it registered - your choice

    You could maybe sed

  • #10
    New Coder
    Join Date
    Apr 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi guys,

    Thank for the input - it did work great thanks!

    I am trying to add an additional email - but when I click the box but forget to fill in the other email (say email_2) box. How can we "make sure the box stays ticked" when you are taken back to the application page?

    I am thinking to use session here as I show on my code: But it did not work...

    PHP Code:
    ..
    <form action="<?php echo $PHP_SELF;?>" method="post" name="reg1">
    ..
    <tr>
    <td><input name="noemail" type="checkbox" value="noemail"<?php echo ($_SESSION["noemail"]=='noemail' ' checked="checked"' '');?>>
     Do not want to get the E-mail address</td>
    </tr>
                                                            <tr>
    <td nowrap>Enter email - 1
    <input name="email__1" type="text" <?php if ($_POST['action'] == "register") { echo 'value="'.$_POST['email1'].'"'; } ?>>
       </td>

    <td nowrap>Enter email - 2
    <input name="email_2" type="text" <?php if ($_POST['action'] == "register") { echo 'value="'.$_POST['email_2'].'"'; } ?>>
       </td>
    </tr>
    ..
    ..
    PHP Code:
    <?php
    session_start
    ();
    ?>
    ..
    if( !isset($_SESSION['noemail']) && (empty($_SESSION['email_1']) || !check_text($_SESSION['email_1'])) ) {
    echo 'You have not entered an email or hit the check box 1';
    }

    if( !isset($_SESSION['noemail']) && (empty($_SESSION['email_2']) || !check_text($_SESSION['email_'])) ) {
    echo 'You have not entered an email or hit the check box 2';
    }
    ..
    Can someone tell me what I did wrong....thanks in advance!


  •  

    Posting Permissions

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