Wednesday

Create Dynamic input text fields in html using PHP

      Sometimes we need to create input fields in the form according to the need of users. Let it be dynamic,If you don't know the no.of fields required exactly. In this tutorial we see how to generate a dynamic input field  & how to print those values in a for loop using php.
Grab latest version php.net.

Get the Number of text fields to generate & pass it to main.php

   <form action="main.php" method="post">
        No.of Text fields <input type="text" name="nos" />
       <input type="submit" value="Submit" />
   </form>

  • $nos going to be no.of times the for loop iterate.

  •  
main.php

<form action="result.php" method="post">

    <?php
        $nos=$_POST['nos'];

        for($i=1;$i<=$nos;$i++)
          {
             ?>
   
       Variables:<input size="7" type="text" name="c<?=$i?>" class="input" value="" required="required"/>

           <?
          }
           ?>

      <input type="hidden" name="nos" value="<?=$nos?>" />
      <input type="submit" value="Submit" />
 </form>



      These values are submitted to result.php. Let us see how to print the values in result.php page continue reading .


Make a comment it will be helpful to improve the post.
__________________________________________________________________________________

0 comments:

Post a Comment

Don't Forget To Join US Our Community
×
blogger