How can i pass a parameter array from PHP to a Javascript function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Miguel

    #1

    How can i pass a parameter array from PHP to a Javascript function

    Hello.

    I have a Javascript function that validates an specific form. As
    parameters this function receives an array of elements to be checked.
    Depending on the form in cause, the array could have variable lenght.
    I don't know how can i pass a dynamic array on 'onsubmit' event form
    PHP code to a JavaScript function.
    I would like to do something like:

    <script>
    function Valid_Form(Elem entsArray)
    {
    alert (ElementsArray[0].value);
    alert (ElementsArray[1].value);

    return (true);
    }
    </script>

    ....

    <?php
    ...
    echo "<form method='post' action='?oper=c onfirm' onSubmit='retur n
    Valid_Form(this .elements[0], this.elements[1])'>";
    ...
    ?>

    But it don't work!
    Anybody can help me please.

    Thanks
    Regards
    Miguel
  • Rico Huijbers

    #2
    Re: How can i pass a parameter array from PHP to a Javascript function

    Miguel wrote:[color=blue]
    > Hello.
    >
    > I have a Javascript function that validates an specific form. As
    > parameters this function receives an array of elements to be checked.
    > Depending on the form in cause, the array could have variable lenght.
    > I don't know how can i pass a dynamic array on 'onsubmit' event form
    > PHP code to a JavaScript function.
    > I would like to do something like:
    >
    > <script>
    > function Valid_Form(Elem entsArray)
    > {
    > alert (ElementsArray[0].value);
    > alert (ElementsArray[1].value);
    >
    > return (true);
    > }
    > </script>
    >
    > ...
    >
    > <?php
    > ...
    > echo "<form method='post' action='?oper=c onfirm' onSubmit='retur n
    > Valid_Form(this .elements[0], this.elements[1])'>";
    > ...
    > ?>
    >
    > But it don't work!
    > Anybody can help me please.[/color]

    That's because you're not passing an array, but rather a list of
    parameters. To wrap it in an array, you need to add an Array
    constructor, like so:

    Valid_Form(new Array(this.elem ents[0], this.elements[1]))

    - Rico

    Comment

    Working...