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
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Creating text element for a form with createElement makes text element not work

    My code is this:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="content-type" content="text/html;charset= utf8_unicode_ci">

    <title>add textboxes</title>

    <script type="text/javascript">

    function add_text() {

    var form=document.getElementById('texts');
    var textbox=document.createElement('input');
    var rand=Math.random()

    textbox.setAttribute ('type','text');
    textbox.setAttribute ('name','txt[]');
    textbox.setAttribute ('value',rand);

    form.appendChild(textbox);

    }
    </script>

    </head>
    <body>

    <?php

    if (filter_has_var (INPUT_POST,"txt")){

    $txt=filter_input(INPUT_POST,"txt");
    print count($txt);

    }
    else
    {
    print <<<HERE

    <input type="button" value="Add text field" onclick="javascript:add_text()">
    <form id="texts" method="post" action="">
    <input type="submit">
    </form>

    HERE;
    }
    ?>
    </body>
    </html>

    In a few words it creates a button which when it is clicked it launches the add_text script. Add text script creates a text element for a form and puts a random value for the textbox. Textbox also takes a name which is of array type name="txt[]". Also form has the post method.

    When I press e.g. 4 times the add text field button 4 text boxes are added.

    But when I click the submit button I get a result of 1 instead of 4 when the
    print count($txt) is executed.

    Copy paste the code to see the problem.

  • #2
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    You can't filter an array like that.

    This is a PHP issue, not Javascript, so I'm moving the thread to the PHP forum.
    .My new Javascript tutorial site: http://reallifejs.com/
    .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
    .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    you will at least require the flag FILTER_REQUIRE_ARRAY. this is either passed as 4th parameter or under the "flags" key of the additional options array.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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