Web Analytics Made Easy -
StatCounter Check box stay ticked - CodingForum

Announcement

Collapse
No announcement yet.

Check box stay ticked

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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">
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

    Comment


    • #3
      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.

      Comment


      • #4
        @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.
        Evolution - The non-random survival of random variants.
        Physics is actually atoms trying to understand themselves.

        Comment


        • #5
          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:
          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!

          Comment


          • #6
            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.

            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.
            PHP Code:
            header('HTTP/1.1 420 Enhance Your Calm'); 
            Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

            Comment


            • #7
              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.

              Comment


              • #8
                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

                PHP Code:
                header('HTTP/1.1 420 Enhance Your Calm'); 
                Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

                Comment


                • #9
                  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.

                  Comment

                  Working...
                  X
                  😀
                  🥰
                  🤢
                  😎
                  😡
                  👍
                  👎