Skip to content Skip to sidebar Skip to footer

Can You Submit A Form With An Array Of Text Input?

i am dynamically building a page. This page needs to read information from input tags but it is dynamic. Should i set up stuff to be an array since i am looking at I want to pres

Solution 1:

There are at least 2 way to do that.

Without javascript, you cate a form with array of element like this

<inputtype="text" name="input[]"/>
<inputtype="text" name="input[]"/>
<inputtype="text" name="input[]"/>
<inputtype="text" name="input[]"/>

in php

$inputs = $_POST['input'];
for($inputsas$inp){

}

With ajax and jquery, you can just simply serialize your form and post to backend

Solution 2:

You can achieve that by using the name attribute in your inputs. Like so:

<inputtype="text" name="pilots[]" />

Then you would probably want to keep track of how many pilots you are adding that way you can send an indexed array. Like so:

<inputtype="text" name="pilots[0][minumumCollatural]" />
<inputtype="text" name="pilots[0][reward]" />
<inputtype="text" name="pilots[0][volume]" />

That way when you submit your form to the server, your array of pilots will look something like:

$pilots = $_POST['pilots'];

// Which looks like
array(
   [0] => array
          (
             [minumumCollatural] => // Some number
             [reward] => // Some number
          )
    [1] => array
          (
             [minumumCollatural] => // Some number
             [reward] => // Some number
          )
)

Solution 3:

Try utilizing the hidden input tag to send data in whatever fashion you please. For instance:

<inputtype="hidden" name="myinput"id="myinput" />

Now in JS:

$("form").submit(function() {
    $("input").not("#myinput").each(function() {
        $("#myinput").val($("#myinput").val()+$(this).val());
        // you can format anyway you want this is just an example
    });
});

Hope this helps!

Post a Comment for "Can You Submit A Form With An Array Of Text Input?"