Forms – Dynamic input names - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Forms – Dynamic input names (/showthread.php?tid=2497) |
Forms – Dynamic input names - El Forum - 08-08-2007 [eluser]webdude[/eluser] My form input names are generated using a product code held in my database. As I don’t know their names until the page loads how do I get their values once the page has been submitted. Code: <?=form_open($_SERVER['REQUEST_URI']."add");?> outputs Code: <form action="http://www.some-domain-name.co.uk/products/solenoid-valves/direct-solenoid-valve/add" method="post"> 6B37-A3-S-P</strong><br /> Forms – Dynamic input names - El Forum - 08-09-2007 [eluser]deviant[/eluser] The way I did something like this was to prefix all of the dynamically named fields with a certain string, then when processing the submitted form I would search through the POST variables for all indexes that started with that prefix. However, in your case, if your product codes are always going to be of that format (or similar) you could just use regular expressions to figure out which fields are dynamically named. Forms – Dynamic input names - El Forum - 08-09-2007 [eluser]obiron2[/eluser] In the example you have given, every field would be a product code so it doesn't matter. This is a bit fiddly but is probably the best way. In your code that generates the form, include hidden fields with name = Pcode and value = your product code. When you submit the form, I think (and I'm sure someone will correct me here) $_POST will effectively make an array of the PCode names and you can iterate through the array and get the value of $_POST['Pcode'][x] and then search for a $_POST['{$Foundvalue}'] You will need to check the syntax though. Forms – Dynamic input names - El Forum - 08-09-2007 [eluser]deviant[/eluser] Well I was posting that on the assumption he might want to add fields for other things in the future, plus the form submit button is included in $_POST and that at the very least needs ignored. As far as I know your method would only result in a single PCode value in $_POST rather than a sub-array. Forms – Dynamic input names - El Forum - 08-09-2007 [eluser]Code Arachn!d[/eluser] I've run into this when I was building my last cms and wanted the forms built dynamically. So my approach was a little lengthy but if done right you can get crazy with it. - Step 1 involves creating a hidden field that contains all the field names in the form that you want evaluated. - Step 2 builds the inputs along with another hidden field for each form field containing the database's column name. - Step 3 to evaluate all the forms you take the hidden field with the field list and then loop through each one of those values which calls the field with the data. Code: // setup hidden fields for form open tag Then to process the results when the form is submitted - I'm including what I built for the update script but this could easily be done for inserts as well it just involves formatting the sql a little differently... Code: function update_data() Forms – Dynamic input names - El Forum - 08-09-2007 [eluser]deviant[/eluser] Thats awesome if you have no idea what the field names could be, but if the field names are going to be as structured as the OP's then I don't see the point in 1) the added size of the HTML page and 2) the extra processing once it has all been submitted. Code: $data = array(); That would work with no changes to his form. Obviously thats at the very simplest leaving out validation etc. and the regular expression would need changed depending on exactly what the product codes are like (I suck at regex anyway). Forms – Dynamic input names - El Forum - 08-09-2007 [eluser]Code Arachn!d[/eluser] That's a good point - but you may run into issues where you don't want certain fields coming through or need to update multiple tables - or in the case of different browsers the submit button is recognized as a form field and sent through as a post var... so my recommendation is that you'd want at least some validation or control... The other benefit the structured way provides is just build another hidden field where you setup validation for your form fields... |