...

View Full Version : Checkbox clicked Not required



siabanie
08-19-2011, 01:47 AM
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:



<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>




..
..
$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

BluePanther
08-19-2011, 01:52 AM
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.

siabanie
08-19-2011, 01:57 AM
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?

siabanie
08-19-2011, 05:40 PM
I have come up with something e.g:



<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>




..


$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?

Nightfire
08-19-2011, 06:54 PM
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.

siabanie
08-19-2011, 07:19 PM
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.



<tr>
<td><input name="noemail" type="checkbox" value="ON">
Do not want to get the E-mail address</td>
</tr>





..
if (($_POST['email1'] == "") && ($_POST['noemail'] == "ON")) {
$error = 1;
$errormsg .= "Please enter your requested email address<br>";
$errornum[3] = 1;
}
..

BluePanther
08-19-2011, 07:38 PM
checkboxes are parsed by checking first if it is empty() ( I THINK SOMEONE PLEASE CONFIRM THIS :D ). So, checking for your checkbox is done by


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"?

siabanie
08-19-2011, 07:46 PM
checkboxes are parsed by checking first if it is empty() ( I THINK SOMEONE PLEASE CONFIRM THIS :D ). So, checking for your checkbox is done by


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:



..
..
if ( empty($_POST['email1']) || !check_text($_POST['email1']) && ($_POST['noemail']=="ON") ){
..


or even like this:



if ( empty($_POST['email1']) || !check_text($_POST['email1']) && ($_POST['noemail'] == "y")) {
..
..


but none of these are working......:(

BluePanther
08-19-2011, 08:12 PM
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:



..
..
if ( empty($_POST['email1']) || !check_text($_POST['email1']) && ($_POST['noemail']=="ON") ){
..


or even like this:



if ( empty($_POST['email1']) || !check_text($_POST['email1']) && ($_POST['noemail'] == "y")) {
..
..


but none of these are working......:(

Try this:


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

siabanie
08-22-2011, 03:09 PM
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...



..
<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
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!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum