0

okay so here's my current script

<html>
<head>


    <script type="text/javascript">
        function textareaToArray(t){
        return t.value.split(/[\n\r]+/);
        }
        function showArray(msg){
        document.getElementById("message").innerHTML = msg.join("&#013;");
        }
    </script>


    </head>
        <title>
        Welcome To ....
        </title>
<body>
        <center>
        <h1> WELCOME TO .... </h1></center>

    </br>
    </br>
    </br>

<center>

        <form>
        <textarea rows="10" cols="60" name="alpha"></textarea>
        <br>
        <input type="button"
        value="show array"
        onclick="showArray(textareaToArray(this.form.alpha ))">
        </form>
</center>
    </br>
<center>
        <textarea id="message" rows="6" cols="60" name="message"></textarea>
</center>
</body>
</html>

as you can see the javascript function basically just outputs the array into the second text area.

how can I invoke a function for each line in the array

e.g.

a function to use line in array post to php script as post data then echo the result back after this it does the same for the next line can any one help me on how I would post each line to a php script and echo the response back into the text area

2
  • 2
    Please, use indentation. Nobody's going read that. Commented Feb 12, 2014 at 17:36
  • Just to fix your semantics, there are no "lines" in an array, an array is object that contains elements, what you should be asking is "how do I iterate through an array". Commented Feb 12, 2014 at 17:57

2 Answers 2

1

I redid your entire example, also fixed some other small errors in your html.

<html><head><title>Welcome To ....</title>
    <script type="text/javascript">
        function textareaToArray(t){
            return t.value.split(/[\n\r]+/);
        }
        function showArray(msg){
            for(i = 0; i < msg.length; i++) {
                // something per item
                alert(msg[i]);
            }
            // the old code
            document.getElementById("message").innerHTML = msg.join("&#013;");
        }
    </script>
</head>
<body>
    <h1> WELCOME TO .... </h1>
    <form>
    <textarea rows="10" cols="60" name="alpha"></textarea>
    <br>
    <input type="button" value="show array" onclick="showArray(textareaToArray(this.form.alpha ))">
    </form>
    <br>
    <textarea id="message" rows="6" cols="60" name="message"></textarea>
</body></html>

Where the "alert" happens is where you could add code for each item in the array. Now for the warning, there is no input checking.. this code is brutally insecure and if you are allowing to pass things from a textarea, this is definitely a vector for XSS attacks.

Sign up to request clarification or add additional context in comments.

Comments

0

Correct answer is: for (var i = 0; i < array.length; i++) alert(array[i])

1 Comment

Don't use a for...in loop to iterate over an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.