...

View Full Version : Check box stay ticked



siabanie
08-27-2011, 04:43 AM
Hi guys,

I have some confusion here - I am trying to create a check box where when you tick the box but forget to fill in some other fields - How can you make sure the box stays ticked when we are taken back to the application page?

Here is snippets of the code:



..
$error = 0;
$errormsg = "";
..
if( !isset($_POST['noemail']) && (empty($_POST['email1']) || !check_text($_POST['email1'])) ) {
$error = 1;
$errormsg .= "Please enter your requested email address 1<br>";
$errornum[3] = 1;
}
if( !isset($_POST['noemail']) && (empty($_POST['email2']) || !check_text($_POST['email2'])) ) {
$error = 1;
$errormsg .= "Please enter your requested email address 2<br>";
$errornum[3] = 1;
}..



The Form


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

<tr>

<td nowrap>First Choice
<input name="email1" type="text" <?php if ($_POST['action'] == "register") { echo 'value="'.$_POST['email1'].'"'; } ?>>
</td>
</tr>
<tr>

<td nowrap>Second Choice
<input name="email2" type="text" <?php if ($_POST['action'] == "register") { echo 'value="'.$_POST['email2'].'"'; } ?>>
</td>
</tr>



I tired used session e.g:



<td><input name="noemail" type="checkbox" value="noemail"<?php echo ($_SESSION["noemail"]=='noemail' ? ' checked="checked"' : '');?>>


but it is still did not work - can anyone please assist me what I need to edit or did wrong.

Thanks.

sunfighter
08-27-2011, 04:55 PM
Can't see from example what you want to trigger the checkbox, but this should do it:

<td><input name="noemail" <?php if(some action here){echo 'checked="checked"';} ?> type="checkbox" value="noemail">

webdev1958
08-28-2011, 12:05 AM
How can you make sure the box stays ticked when we are taken back to the application page?

The easiest way I find to do this is to have a session variable on the processing page to store the status of the checkbox. If the user is taken back to the application page, it checks the value of the checkbox's session variable and then sets the checked attribute accordingly.

Another way is to use cookies instead of a session variable but I would discourage using cookies for this scenario since cookies can be turned off in the user's browser.

sunfighter
08-28-2011, 03:06 PM
@webdev1958 sessions are cookie dependent. Your computer is identifies and linked to the session variables by a number stored in a cookie.

The only way around this is if your using ASP.NET and can change the web.config file.

There maybe another way, but I don't know of it. And I can't change the web.config either.

siabanie
08-28-2011, 05:23 PM
@webdev1958 sessions are cookie dependent. Your computer is identifies and linked to the session variables by a number stored in a cookie.

The only way around this is if your using ASP.NET and can change the web.config file.

There maybe another way, but I don't know of it. And I can't change the web.config either.

Thanks sunfighter, what want to trigger is "How can we make sure the box stays ticked when we are taken back to the application page?" And I am not using ASP.net

as webdev1958 said:


If the user is taken back to the application page, it checks the value of the checkbox's session variable and then sets the checked attribute accordingly.


I tried to do this:


<td><input name="noemail" <?php if($_POST['action'] == "noemail"){echo 'checked="checked"';} ?> type="checkbox" value="noemail">

but it still did not work...can you give me some idea how I can achieve this please?

Thanks!

Fou-Lu
08-28-2011, 05:44 PM
@webdev1958 sessions are cookie dependent. Your computer is identifies and linked to the session variables by a number stored in a cookie.

The only way around this is if your using ASP.NET and can change the web.config file.

There maybe another way, but I don't know of it. And I can't change the web.config either.

This is misleading.
Sessions are not cookie dependent. And ASP.NET will suffer from the same problem. It is a limitation of HTTP which does not allow you to create persistent connections such as sessions. You have to provide a request with something if you want to use a persistent type of communication, and your options are limited. Cookies, get and post are the most common in PHP. If a cookie is not set, the constant SID will become usable which you can append to your form's action as a GET, the form's input tags typically hidden, or header redirect querystring.


Thanks sunfighter, what want to trigger is "How can we make sure the box stays ticked when we are taken back to the application page?" And I am not using ASP.net

as webdev1958 said:


I tried to do this:


<td><input name="noemail" <?php if($_POST['action'] == "noemail"){echo 'checked="checked"';} ?> type="checkbox" value="noemail">

but it still did not work...can you give me some idea how I can achieve this please?

Thanks!

This code looks to me that it is a self posting form. You can simply use the $_POST to get this:


<input type="checkbox" name="noemail"<?php echo isset($_POST['noemail']) ? ' checked="checked"' : ''; ?> />

I assumed that this was a self posting form since you have a check on the $_POST['action'] within the form. Don't do that btw, check each field independently. This is especially true if the action is provided by a submit input which may be ignored by IE since it doesn't conform to standards properly.

siabanie
08-28-2011, 06:09 PM
<input type="checkbox" name="noemail"<?php echo isset($_POST['noemail']) ? ' checked="checked"' : ''; ?> />

I assumed that this was a self posting form since you have a check on the $_POST['action'] within the form. Don't do that btw, check each field independently. This is especially true if the action is provided by a submit input which may be ignored by IE since it doesn't conform to standards properly.

Thanks Fou-Lu, yes it is self posting form - That suggested code work but as I have more than one email address email1 and email2 it gives me stranger outcome -even though I ticked or not ticked the box the error message for email1 still showing.

What do you mean by
Don't do that btw, check each field independently. This is especially true if the action is provided by a submit input which may be ignored by IE since it doesn't conform to standards properly.

Fou-Lu
08-28-2011, 06:49 PM
IE doesn't conform to standards properly. Its unreliable to check the isset status of an input type that is submit. You must verify each field independently.
You are receiving errors still since you're branch evaluation is incorrect:


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

If you leave email1 empty and check the noemail, this evaluates as:


if (false && true || unknown)
if (false || unknown)
if (false || [true||false])

Assuming that check_text will return false when provided with an empty result, this would indicate that you're result is:


if (false || true)

Which is true.
What you want is:


if (false AND true || [true||false])
if (false AND true)
if (false AND true)
if (false)


Which would be a placement of brackets:


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

webdev1958
08-29-2011, 12:17 AM
@webdev1958 sessions are cookie dependent.

Sessions will still work with cookies disabled. Have a look at Fou-Lu's explanation in post 6.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum