Note that there are some explanatory texts on larger screens.

plurals
  1. POProcessing dynamically generated text boxes which have variable names
    text
    copied!<p>I'm working on a script that writes out a form based on an uploaded CSV file. The CSV file contains sections that are separated by a line containing only ',,,,'. Each block contains information about a server that is going to be kickstarted. It looks like this:</p> <pre> host,tctivt2r6ra03,,, int,eth0,10.153.196.248,255.255.255.0,10.153.196.1 int,eth1,10.153.157.113,255.255.255.128,10.153.157.1 int,eth2,10.153.157.241,255.255.255.128,10.153.157.129 int,eth3,00:50:56:ac:69:cb,, part,/home,10,, part,swap,10,, part,/opt,60,, part,/data,30,, ,,,, </pre> <p>This input is written to a div which contains a table. Each row of input being a unique row in the table. Some values end up in text boxes.</p> <p>The script generates output that looks like <a href="https://dl.dropbox.com/u/116457/upload_file_ouput.PNG" rel="nofollow">this</a>.</p> <p>Each box is generated dynamically and consists of the host ID starting at 0 and the box number also starting at 0. For example:</p> <pre><code>&lt;tr&gt;&lt;th&gt;eth0: &lt;/th&gt;&lt;td&gt;&lt;input type="text" value="10.153.196.248" name="host0Box0" /&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" value="255.255.255.0" name="host0Box1" /&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" value="10.153.196.1" name="host0Box2" /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt; </code></pre> <p>The question I have is how to process a form that has a variable number of boxes. Specifically, when processing the 'part' lines of the input there can be six boxes or more.</p> <p>Example with four partitions:</p> <pre><code>&lt;tr&gt;&lt;th&gt;Partition name:&lt;/th&gt;&lt;td&gt; /&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;th&gt;Partition name:&lt;/th&gt;&lt;td&gt; /var&lt;/td&gt;&lt;td&gt;&lt;input type="text" value="10" name="host0Box9" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;th&gt;Partition name:&lt;/th&gt;&lt;td&gt; /home&lt;/td&gt;&lt;td&gt;&lt;input type="text" value="10" name="host0Box10" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;th&gt;Partition name:&lt;/th&gt;&lt;td&gt; swap&lt;/td&gt;&lt;td&gt;&lt;input type="text" value="10" name="host0Box11" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;th&gt;Partition name:&lt;/th&gt;&lt;td&gt; /opt&lt;/td&gt;&lt;td&gt;&lt;input type="text" value="60" name="host0Box12" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;th&gt;Partition name:&lt;/th&gt;&lt;td&gt;&lt;input type="text" value="/data" name="host0Box13" /&gt;/td&gt;&lt;td&gt;&lt;input type="text" value="30" name="host0Box14" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;th&gt;Number of Disks: &lt;/th&gt;&lt;td&gt;&lt;input type="text" value="1" name="host0Disks" /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt; </code></pre> <p>Example with six partitions:</p> <pre><code>&lt;tr&gt;&lt;th&gt;Partition name:&lt;/th&gt;&lt;td&gt; /&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;th&gt;Partition name:&lt;/th&gt;&lt;td&gt; /var&lt;/td&gt;&lt;td&gt;&lt;input type="text" value="10" name="host1Box9" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;th&gt;Partition name:&lt;/th&gt;&lt;td&gt; /home&lt;/td&gt;&lt;td&gt;&lt;input type="text" value="10" name="host1Box10" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;th&gt;Partition name:&lt;/th&gt;&lt;td&gt; swap&lt;/td&gt;&lt;td&gt;&lt;input type="text" value="10" name="host1Box11" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;th&gt;Partition name:&lt;/th&gt;&lt;td&gt; /opt&lt;/td&gt;&lt;td&gt;&lt;input type="text" value="60" name="host1Box12" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;th&gt;Partition name:&lt;/th&gt;&lt;td&gt;&lt;input type="text" value="/data" name="host1Box13" /&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" value="30" name="host1Box14" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;th&gt;Partition name:&lt;/th&gt;&lt;td&gt;&lt;input type="text" value="/test1" name="host1Box15" /&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" value="30" name="host1Box16" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;th&gt;Partition name:&lt;/th&gt;&lt;td&gt;&lt;input type="text" value="/test2" name="host1Box17" /&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" value="20" name="host1Box18" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;th&gt;Number of Disks: &lt;/th&gt;&lt;td&gt;&lt;input type="text" value="1" name="host0Disks" /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt; </code></pre> <p>This is a work in progress that I know I'm going to have to change in order to properly process each host section. In the meantime, I'm trying to sort out this one issue.</p> <p>My question right now is, when I I eventually do get to the point of processing the form, what approach should I use to account for dynamically allocated item names when the $_POST array is populated? Do I pull the array values out and place them in a different array? Can I iterate over the $_POST array like a normal array? Is there another option that I'm not aware of?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload