1

First user will enter that how many textboxes he want to create. for example he select 4 from list, then 4 textboxes will be created.
After that these, code will create textboxes dynamically in php and send its values to another php page. kindly help to complete this task.

6
  • 1
    Have you done some starting code to share with us? Commented Dec 12, 2015 at 18:36
  • That's the best way for us to help you Commented Dec 12, 2015 at 18:37
  • thanks for reply ... @nanocv ...some part is solved in this link stackoverflow.com/questions/22630864/… Commented Dec 12, 2015 at 18:44
  • but it need to be customize. Commented Dec 12, 2015 at 18:45
  • Ok, give me a moment and I'll write you a code, but I think the best way to create the "textboxes" (textareas I guess) dinamically is with Javascript. Commented Dec 12, 2015 at 18:51

1 Answer 1

1

I would do something like this.

<!DOCTYPE html>
<html>
    <head>
        <title>Page Title</title>
    </head>
    <body>
        <label for="inputTextareasNumber">How many textareas do you want?</label>
        <input type="number" id="inputTextareasNumber" min="1">
        <button type="button" id="butCreateTextareas">Create textareas</button>

        <form id="myDynamicForm" action="myPHPfile.php" method="post"></form>

        <script>
            /* HTML elements caching */
            var inputTextareasNumber = document.getElementById("inputTextareasNumber");
            var butCreateTextareas = document.getElementById("butCreateTextareas");
            var myDynamicForm = document.getElementById("myDynamicForm");

            /* Create textareas when click button */
            butCreateTextareas.addEventListener("click", createTextareas);

            function createTextareas() {
                /* First we empty the form so we can re-create it with a different number of textareas*/
                emptyElement(myDynamicForm);

                /* Here we get the number of textareas we want */
                var textareasNumber = inputTextareasNumber.value;

                /* We create and append to the form as many textareas as number we wrote in the input */
                for (var i = 1; i <= textareasNumber; i++) {
                    var newTextarea = document.createElement("textarea");
                    newTextarea.name = "myTextarea" + i;
                    myDynamicForm.appendChild(newTextarea);
                }

                /* We crate and append the submit button */
                var submitButton = document.createElement("input");
                submitButton.type = "submit";
                submitButton.value = "Send";
                myDynamicForm.appendChild(submitButton);
            }

            /* Empty any HTML element */
            function emptyElement(element) {
                while (element.lastChild) {
                    element.removeChild(element.lastChild);
                }
            }
        </script>
    </body>
</html>

Here you have it working: jsfiddle

A very very basic PHP receiver could be this (myPHPfile.php):

<?php

foreach ($_POST as $textareaValue) {
    echo "<p>$textareaValue</p>";
}
Sign up to request clarification or add additional context in comments.

10 Comments

Great! If you need any help with this code or with the PHP receiver just let me know
one more thing ... if you don't mind. kindly write code that ... How to receive theses textboxes values in another php page.
Sure. What do you want to do with the values? Just show them on the screen?
Actually i am making different web pages ...on the basis of textboxes
Ok, then the standard answer is: if you send the form with the "get" method, you will have an array variable called "$_GET" in the PHP receiver. The array will have as many positions as textareas you created.
|

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.