#1
  1. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,112
    Rep Power
    487

    This.form.name not working!


    Hello all
    Sorry to post what may be such an obvious or schoolboy problem but I can't see why this simple code works on one website I wrote, but not another!

    PHP Code:
        echo "<form method='post' name='customer' id='customer'>";
            echo 
    "<a href='#' onclick='alert(this.form.name);'>select all</a> <br />";
        echo 
    "</form>"

    When I click on the 'select all' link, nothing happens; if I change it to alert 'this.form', I get "undefined" but as you can see, I have named and ID'd my form!
    I have searched google and the syntax etc all appears to be correct (and as I mentioned above, I have used this code on other websites without problem).

    Does anyone have any suggestions please?!
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  2. #2
  3. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,008
    Rep Power
    2791
    Can you post the entire HTML document please.
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  4. #3
  5. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,112
    Rep Power
    487
    Originally Posted by Winters
    Can you post the entire HTML document please.
    Sure ...

    PHP Code:
    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
    <?php
    echo "<form method='post' name='customer' id='customer'>";
            echo 
    "<a href='#' onclick='alert(this.form.name);'>select all</a> <br />";
    echo 
    "</form>"
    ?>
    </body>
    </html>
    I was trying to add a Select ALL / NONE / Toggle to an existing page and when it decided not to work I wrote that code to test the code without any external "interference" and discovered that it refused to work when there was nothing else going on!

    I've also viewed page source and copied that into "newfile.html" (in effect dropping all PHP) and it still refuses to work!

    For what it's worth, here's the "view source" from the above PHP ...

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
    <form method='post' name='customer' id='customer'><a href='#' onclick='alert(this.form.name);'>select all</a> <br /></form></body>
    </html>
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  6. #4
  7. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,008
    Rep Power
    2791
    That seemed like a very eye-rolling "sure" I wanted to see the document type and charset.
    An anchor is not part of a form so it cannot reference a form as a form element parent child. It can be referenced as a parent node.
    Javascript Code:
    this.parentNode.name
    Last edited by Winters; September 26th, 2012 at 10:16 AM. Reason: additional
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  8. #5
  9. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,112
    Rep Power
    487
    I see, ok yes, that makes sense now!

    As for the 'sure ...' haha well the code I had posted was the entire page at the time of posting, I later added the rest of the HTML to see if that would make a difference ... apparently not.
    I'm on with something else now though but when I revisit the select all / none / toggle, I will use a button.

    ps. With regards to nicking the line from other code, I re-checked the original source and the onclick was taken from a submit button, doh

    THANK YOU for the reply and assistance, it's very much appreciated!
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  10. #6
  11. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,008
    Rep Power
    2791
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.

IMN logo majestic logo threadwatch logo seochat tools logo