简体   繁体   中英

HTML dynamic number of elements in a form with post

I have code that basically uploads a spreadsheet of data, could have 5 columns or 10 columns or whatever. Each column needs to have a drop down box specifying the data type, and the data types selected should be submitted via post with a form to another php site. How would I do this, this is the code that creates the elements.Is there a way to dynamically create elements, like delimist . attCount, and how would i put this loop into an html form to submit the data? Is there any way to put the values of all of these into an array for convince? Thanks.

EDIT: Ok thanks for the dynamic stuff, but how would i put this into a form and submit this via post to another page. Or is there another way to post information. I dont know much about php.

    while($attCount < count($attArray)) {
        echo "<select name=\"delimList\">";
        echo "<option value=1>tab</option>";
        echo "<option value=2>comma</option>";
        echo "<option value=3>semi-colon</option>";
        echo "<option value=4>ampersand</option>";
        echo "<option value=5>pipe</option>";
        echo "</select>";
        $attCount = $attCount + 1;
     }

You can put all the value of all of these into an array… by putting all the values of these into an array!

$myArray = ['tab','comma','semi-colon','amperstand','pipe','another-value'];
echo '<select name="delimList">';
for ($i = 0; $i < count($myArray); $i++) {//Loop thru all
    echo '<option value="'.$i.'">'.$myArray[$i].'</option>';
}
echo "</select>";

Using a foreach loop and assigning the first index in your array will give you what you're expecting:

$options = [1=>'tab','comma','semi-colon','amperstand','pipe','another-value'];
echo "<select name=\"delimList[]\">\r\n";
foreach($options as $val => $option){
    echo "<option value=\"$val\">$option</option>\r\n";
}
echo "</select>\r\n";

output:

<select name="delimList[]">
<option value="1">tab</option>
<option value="2">comma</option>
<option value="3">semi-colon</option>
<option value="4">amperstand</option>
<option value="5">pipe</option>
<option value="6">another-value</option>
</select>

To sumbit the page you'll need to put everything inside of

<form method="post"> // you may want to add an action="..."
....                 // if you want to post to another page,
</form>              // instead of the same one

You'll also need a submit button too...

<input type="submit" name="submit" value="submit" />

After submit you can check the values like this:

if(isset($_POST['delimList'])){
    foreach($_POST['delimList'] as $key => $value){
        echo "delimList[$key] = $value";
    }
}

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