Jump to content

Checkbox clicked Not required


siabanie

Recommended Posts

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

Link to comment
Share on other sites

if ( empty($_POST['email1']) || !check_text($_POST['email1']) && empty($_POST['checkbox']) )

 

English -

If either $_POST['email1'] is empty, or check_text() returns FALSE (the brackets mean either of those could return true) and $_POST['checkbox'] is empty as well.

Link to comment
Share on other sites

if ( empty($_POST['email1']) || !check_text($_POST['email1']) && empty($_POST['checkbox']) )

 

English -

If either $_POST['email1'] is empty, or check_text() returns FALSE (the brackets mean either of those could return true) and $_POST['checkbox'] is empty as well.

 

Thanks for your reply xyph, but the code

if ( empty($_POST['email1']) || !check_text($_POST['email1']) && empty($_POST['noemail']) )

 

did not give me as I wanted - What I am trying to achieve is: If user tick the box, they do NOT have to fill in the email address field but if user did NOT tick the box, then they MUST fill in the email address otherwise an error message will shown.

 

I was thinking to add something like this giving a value to my checkbox:

 

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

 

 

And here is the error message if user not tick the box and not fill in the email address:

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

 

but it did not work...any idea what I did wrong?

 

Link to comment
Share on other sites

*sigh*

 

Good luck from here. My code sample does work. And it's VERY basic logic. Work on wrapping your head around it before moving on to more complex functionality, or you're going to find yourself lost.

 

<?php 

if( (empty($_POST['email']) || !check_text($_POST['email'])) && empty($_POST['checkbox']) ) {
echo 'You have not entered an email or hit the check box';
} elseif( !empty($_POST['checkbox']) ) {
echo 'You have hit the check box';
} else {
echo 'You have entered an email';
}

function check_text() {
return TRUE;
}

?><br>
<form action="#" method="post">
<label for="email">Email:</label>
<input type="text" name="email" id="email"><br>
<label for="checkbox">I don't want to enter an email</label>
<input type="checkbox" name="checkbox" id="checkbox"><br>
<input type="submit">
</form>

Link to comment
Share on other sites

@xyph, that is incorrect logic. A checkbox that is not checked does not get passed in the POST data. So, even if empty() works it is the wrong test.

 

Since this is a single checkbox and you only want to know if it was checked or not, the value is insignificant. You simply need to see if it was is set. Although there was no problem with the order of the conditions, I changed them up slightly to what, I think, is a more logical order for comprehension. It is also more efficient from a code perspective. If the checkbox IS checked the other conditions won't be tested because of the AND clause.

 

if( !isset($_POST['checkbox']) && (empty($_POST['email']) || !check_text($_POST['email'])) ) {
echo 'You have not entered an email or hit the check box';

Link to comment
Share on other sites

Calling if( empty($var) ) is the equivalent of calling if( !isset($var) || $var == '' || $var == 0 || etc... ). How is that not correct logic? I suppose if his checkbox returned '0' when checked issues might show up, but setting a variable to 0 when you want to evaluate TRUE is funny logic.

My code will not throw any notice errors, because empty() first makes sure the variable is set.

 

Yes, I agree your order is more efficient, but I think the difference would be unnoticeable even under extreme loads. Though I suppose that varies based on what check_text() does.

Link to comment
Share on other sites

Maybe I used the wrong description. It's not that it won't work, it is just not the logically correct test. The test is to determine whether the checkbox was checked or not. If it is not checked then that POST value will not be set. So, isset() is the logically correct test. As you pointed out a value of "0" (or an empty string) would cause empty() to fail. Yes, it would be foolish to use "0" (or an empty string) for the value of a checkbox, but using isset() will work regardless of the value of the checkbox. You also have to consider that the person writing the code today isn't the person that will always be the person working on the code. Someone could come along and set the value of the checkbox to "0" in an attempt to use that value for some other logic.

 

My point is, if you want to know if something is empty use empty(), if you want to know if it is set use isset().

 

As for the reordering of the conditions, that wasn't directed to you. That was just a general statement based upon the original order. I have spent countless hours when going through old code to try and re-understand what I have done, so I try to always make my code logical and as easy to comprehend as I can (within reason). The little bit of extra effort save much more time on the back end.

 

And, as to the "performance" benefit. Yes, absolutely, you would never notice it in this instance. But, that doesn't mean it would never be a benefit, which is why I pointed it out. If, for example, you have a script that needs to process hundreds/thousands of records from a DB query or an import file that need to be processed than any tweak you can make to be more efficient is desireable.

Link to comment
Share on other sites

The reason I used empty is because I'm not sure if it's client or PHP behavior that doesn't pass empty form variables into the $_POST array.

 

If it's PHP that does it, then yes, my call is incorrect.

 

It is standard HTML behavior that checkboxes that are not checked are not passed in the POST data. But, it has nothing to do with being "empty". An empty text field IS passed in the POST data because an empty string IS a value. Unchecked checkboxes are not passed because, well, what would be passed? When a form field is passed in the POST data you get the field name and the field value. If the checkbox is checked you get the checkbox name and the checkbox value. So, if it is not checked you would get the field name and what? They could have passed an empty string, but the value could have been an empty string so you still wouldn't know if it was checked or not. A null value might have made sense, but I think the problem there is that different programming languages handle null differently. So, I assume, the decision was to not pass the field at all.

 

In 90% of cases the value of the checkbox is not needed since you are typically concerned with whether it is simply checked or not [using isset()]. Where I am usually concerned with the value is when I provide a list of checkboxes for the user to select from. In those instances I will create the checkboxes as an array. For example a list of categories with the category ID as the value. I can then determine which categories were checked using the values of the array passed in the POST data.

Link to comment
Share on other sites

I was curious about this and decided to do some research. Here are the W3C specs on checkboxes

 

http://www.w3.org/TR/html4/interact/forms.html#h-17.2.1

checkboxes

Checkboxes (and radio buttons) are on/off switches that may be toggled by the user. A switch is "on" when the control element's checked attribute is set. When a form is submitted, only "on" checkbox controls can become successful.

 

Several checkboxes in a form may share the same control name. Thus, for example, checkboxes allow users to select several values for the same property. The INPUT element is used to create a checkbox control.

Link to comment
Share on other sites

Thanks guys for the explanation, that's really help.

 

This code is work fine, and it gives me the error msg when the box did not tick.

if( !isset($_POST['checkbox']) && (empty($_POST['email_1']) || !check_text($_POST['emai_1_2l'])) ) {
echo 'You have not entered an email or hit the check box 1 ';

if( !isset($_POST['checkbox']) && (empty($_POST['email_2']) || !check_text($_POST['email_2'])) ) {
echo 'You have not entered an email or hit the check box 2';

 

<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 - 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>
..
</form>

 

When I try to extend it by adding another email - then when you tick the box but forget to fill in some other bits. How can we make sure the box stays ticked when you are taken back to the application page?

 

Can someone give me an idea please?

Thanks

Link to comment
Share on other sites

Hi guys,

 

I was trying to use session on this - I did try to put something like:

 

..
<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';
}
..

 

But it did not work....anyone can assist me what I did wrong?

Link to comment
Share on other sites

You don't need sessions. Again, to determine if a checkbox was checked you just check if the field was passed int he POST data, i.e. isset(). In fact, you can do the same for all the fields.

 

Personally, I hate putting PHP "logic" code within the HTML, but since that is what you are doing:

 

<form action="<?php echo $PHP_SELF;?>" method="post" name="reg1">

<tr>
  <td>
    <input name="noemail" type="checkbox" value="noemail"<?php if (isset($_POST["noemail"]) { echo  ' 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" value="<?php if (isset($_POST['email1'])) { echo $_POST['email1']; ?>" />
  </td>

  <td nowrap>Enter email - 2
    <input name="email_2" type="text" value="<?php if (isset($_POST['email_2'])) { echo $_POST['email_2']; ?>" />
  </td>
</tr>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.