Sunday 13 October 2013

Generate text boxes dynamically using php

In this section i am providing this gyan to some techies. While developing web applications in php some times they must have come through the requirement to generate multiple text boxes dynamically using php. Here i will show when you can use this approach, we can use php to generate text boxes dynamically when we have fixed number of limit text boxes, this can be done by the following ways:
1) By finding out number of rows from resultset and generating text boxes (When we need to generate text boxes on the basis of resultset)
2) If user enters the number of boxes to be generated (In this case we can fulfill this requirement either by using text box or select box).

generate text boxes using resultset:

                $rs=mysql_query($query) or die(mysql_error());
$num=mysql_num_rows($rs);

for($i=1;$i<=$num;$i++)
{
echo "<input type='text' name='job_id[]' >";
}

To generate text using fix number, you need to set $num as constant
 $rs=mysql_query($query) or die(mysql_error());
$num=5;

for($i=1;$i<=$num;$i++)
{
echo "<input type='text' name='job_id[]' >";
}

Note: Always remember to name text box as an array.

..........................................................................Use well, use safe...........................................................

No comments:

Post a Comment