简体   繁体   中英

Create User Defined Number of Inputs for Website

I am trying to make a website that accepts multiple inputs based on how many the user wants to enter. Lets say they want to enter 4 addresses. I want to be able to handle that input but also if they ask for 10 inputs without having to create pages for each instance. I am new to programming so any insight would be great. Also, I saw some examples of website that I believe use javascript to take in input, create more fields, and process the data without having to switch pages. How would I implement such a thing and would I have to change my data sourcing from php in anyway if I did? Thanks!

With jQuery

index.html

    <!DOCTYPE html>
    <html class="no-js" lang="en">
      <head>
          <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
          <script>
            $(function(){
                var c = 0;

                $("#button1").click(function(){
                  c = $("#inputs").val();

                  $("#mydiv").html("");

                  for(i=0;i<c;i++){
                        $("#mydiv").append('<input type="text" id="data'+i+'" name="data'+i+'" /><br/>');
                  }
                });

                $("#button2").click(function(){
                    $.post("file.php",$("#form1").serialize(),function(data){
                        alert(data);
                    });
                });

            });
          </script>
        </head>
        <body>
        <form id="form1">
          Type the number of inputs:
          <input type="text" id="inputs" name="inputs" />
          <input type="button" id="button1" value="Create" />
          <div id="mydiv"></div>
          <input type="button" id ="button2" value="Send" />
        </form>

        </body>
    </html>

If you need to process the new inputs with PHP:

file.php

    <?php
      for( $i=0; $i<$_POST["inputs"] ; $i++){
        echo $_POST["data".$i]."\n";
      }
    ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM