Home > Mobile >  Create text-input on button click in form
Create text-input on button click in form

Time:10-24

I want to create another input field, everytime the button " " inside the form is clicked. I searched for a solution but i can't find anything. The input field should be created inside the form over the " " button.

Here is my current code:

<div class="row">
        <div class="col-md-12">
          <form method="post" id="create-form" action="create.php" enctype="multipart/form-data">
            <?php
            /* getting sample questions */
            /* checks result and creates variables from result */

            if ($sampleamount > 0) { // amount
              $samplequery->bind_result($questionid_sample, $question_sample);
              while ($samplequery->fetch()) { // while page can use this variables
                /* echo text-box, value from db */
                $required = $questionid_sample === 1 ? "required" : ""; // one question is always required
                echo "<input class='question-box' type='text' name='" . $questionid_sample . "' placeholder='Write your question in here.' maxlength='255' size='70' value='" . $question_sample . "'" . $required . "> <br>";
              }
            } else {
              /* no result (db=sample_question) */
              header('Location: ../index.php');
            }

            $samplequery->close();
            closeDB($conn);

            /* adds more input for user */
            for ($i = ($sampleamount   1); $i <= ($additionalquestions   $sampleamount); $i  ) {
              echo "<input class='question-box' type='text' name='" . $i . "' placeholder='Write your question in here.' maxlength='255' size='70'> <br>";
            }
            ?>
            <br>
            <input class="createsurvey2" type="button" id="addqbtn" name="addqbtn" value=" " >
            <br>
            <input class="createsurvey2" type="submit" name="btnSubmit" value="Create Survey!" >
          </form>
        </div>
      </div>
    </div>
    <script>
            function addquestion() {
              var counter = 2;
              var addqbtn = document.getElementById('addqbtn');
              var form = document.getElementById('create-form');
              var addInput = function() {
                counter  ;
                var input = document.createElement("input");
                input.id = 'additionalquestion-'   counter;
                input.type = 'text';
                input.class = 'question-box';
                input.name = 'name';
                input.placeholder = 'Write your question in here.';
                form.appendChild(input);
              };
              addqbtn.addEventListener('click', function(){
                addInput();
              }.bind(this));
            };
          </script>

CodePudding user response:

<html>
<body>
<button onclick="createNewFiled()" id="incrementBtn" type="button">Click here</button>
    <form id="form" action="">
    </form>
</body>
</html>

<script type="text/javascript">

    var counter = 0;
    var incrementBtn = document.getElementById('incrementBtn');
    var form = document.getElementById('form');

    var createNewFiled = function() {
        counter  ;

        var input = document.createElement("input");
        var br = document.createElement("br");  // If you need new line after each input field

        input.id = 'input-'   counter;
        input.classList.add("inputClass");
        input.type = 'text';
        input.name = 'name' counter;
        input.placeholder = 'Input field '   counter;

        form.appendChild(input);
        form.appendChild(br);  // If you need new line after each input field
    };
    
</script>
  • Related