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 3 of 3
  1. #1
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts

    Checkbox always "on"

    Hi all,

    Consider the following code:

    HTML snippet, submits to test.php:
    Code:
    <form name="myform" method="post" action="test.php">
      <input type="checkbox" name="test" />
      <input type="submit" onclick="alert(document.forms['myform'].elements[0].value);" />
    </form>
    And test.php is as follows:
    PHP Code:
    <?php echo $_REQUEST["test"]; ?>
    The PHP displays on when the checkbox is checked and nothing at all when it isn't, as you would expect.

    However, the javascript alert always displays on regardless of whether or not the checkbox is checked.

    Strange, n'est pas? Pourquoi?!

    TIA,

    Mike.
    Last edited by mlse; 09-25-2008 at 06:01 PM.

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,535
    Thanks
    3
    Thanked 513 Times in 500 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <form name="myform" method="post"  action="test.php">
      <input type="checkbox" name="test" />
      <input type="submit" onclick="alert(document.forms['myform'].elements[0].checked);" />
    </form>
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • Users who have thanked vwphillips for this post:

    mlse (09-25-2008)

  • #3
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    Danke shoen! Das ist einfach

    (Mike is in a multi-lingual mood today!).


  •  

    Posting Permissions

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