posting form data from dynamically created form elements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gubbachchi
    New Member
    • Jan 2008
    • 59

    #1

    posting form data from dynamically created form elements

    Hi,

    I have a form whose elements are created dynamically on selection, i.e. the form has only text boxes and the number of text boxes depends on the users selection, if user selects 3 then 3 dynamic text boxes are created and if user selects 4 then 4 dynamic text boxes are created and so on. Here is the code I am using to dynamically create the text boxes:

    Code:
    echo ("<form name='exam' action='exam_2.php' method='POST'>");
    for ($j=0;$j<sizeof($namearr);$j++){
    	
    	echo ("<tr>");
    	echo ("<td>".$namearr[$j]."</td>");
    	echo ("<td>");
    	echo ("<input type='text' name='element' size='8'>");
    	echo ("</td>");
    }
    echo ("<input type='submit' name='exam_sub' value='submit'>");
    echo ("</form>");
    My problem is I want to send the form elements to next page i.e. exam_2.php when submit button is clicked, I am using below code on exam_2.php page to get the data of text boxes:

    Code:
    $element = $_POST['element']; 
    echo $element;
    The $element contains nothing, should I treat $element as an array, I need all the values of textboxes, how will I get it.

    Any help is greatly appreciated. Please give me some suggestions or coding examples.

    With regards,
    gubbachchi
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Change line 7 as:
    Code:
    echo ("<input type='text' name='element[]' size='8'>");
    In exam_2.php, see what is being posted with code:
    Code:
    <?php
    echo "<pre>";
    print_r($_POST);
    echo "</pre>";
    ?>

    Comment

    • gubbachchi
      New Member
      • Jan 2008
      • 59

      #3
      Thanks hsriat, its working!!!

      Comment

      Working...