View Full Version : Html form multiple inputs with almost same name trick

09-02-2014, 08:35 PM
Hello all, i know this is the simple "trick" but still usable and sometimes make life easier !
What it does? for example you need to create 10 inputs with name input_value1 , input_value2 , input_value3 and so on for that use simple php cycle While() for example i need 10 inputs with that names.
Code will be :

$i = 0;

$times = 10;

While ($i++ < $times) {

echo "<input type = 'text' name = 'input_value$i' placeholder = 'input_value$i'/>";


Now comments :

$i = 0; - to shut up our error that this undefined variable in cycle.

$times = 10; - Etner here how much times we want to copy our input and save it to variable

While ($i++ < $times) { // simple cycle that told server do the code inside to do the same while $i < $times

echo "<input type = 'text' name = 'input_value$i' placeholder = 'input_value$i'/>"; + Our $i variable will be $i++ or $i +1 every time this code will be executed, that means our $i values gets something like AUTO INCREMENT , and we can just put it in and see the magic ! :D

} // close cycle, syntax...

Output will be :


Why i tell that is usable? Think now for example we are need to calculate something or just fill in values and we have 10 select inputs with 30 option values inside, better to use that trick or write 10 times the same? of course it's easy to copy paste but think about your "Code" style and the file you making, i think this way more clean and faster.
Hope you enjoy and like this !
Thank you for your attention in this topic !
Best Regards, Vitrex.