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 9 of 9
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Check box stay ticked

    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:

    PHP 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
    Code:
    ..
    ..
     <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:

    PHP Code:
      <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.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,694
    Thanks
    25
    Thanked 657 Times in 656 Posts
    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">

  • #3
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by siabanie View Post
    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.

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,694
    Thanks
    25
    Thanked 657 Times in 656 Posts
    @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.

  • #5
    New Coder
    Join Date
    Apr 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    @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:
    Quote Originally Posted by webdev1958;
    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:
    Code:
    <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!

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by sunfighter View Post
    @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.

    Quote Originally Posted by siabanie View Post
    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:
    Code:
    <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:
    PHP Code:
    <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.

  • #7
    New Coder
    Join Date
    Apr 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    PHP Code:
    <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.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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:
    PHP Code:
    if( !isset($_POST['noemail']) && (empty($_POST['email1']) || !check_text($_POST['email1'])) ) { 
    If you leave email1 empty and check the noemail, this evaluates as:
    Code:
    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:
    Code:
    if (false || true)
    Which is true.
    What you want is:
    Code:
    if (false AND true || [true||false])
    if (false AND true)
    if (false AND true)
    if (false)
    Which would be a placement of brackets:
    PHP Code:
    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


  • #9
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by sunfighter View Post
    @webdev1958 sessions are cookie dependent.
    Sessions will still work with cookies disabled. Have a look at Fou-Lu's explanation in post 6.


  •  

    Posting Permissions

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