1

I need your help with my form. I'm trying to build a dynamic forms, whereby a specific integer entered in a previous form sp1.php is used to display the number of input boxes.

The variables from the first forms are $state and $number. Then on the handling page sp2.php, the value of $number is put into a for loop to display the input boxes.

What I'm trying to do now is that the values entered into the tinput boxes are inserted into a mysql table.

The error I'm getting are

1) Undefined Index : DISTRICT 2) Invalid Argument supplied for foreach()

Please how can I make this work. Thank you.

My Code is below.. I'll be more than happy to show other parts of the code, if required. Thank you.

<?php 

 $state=htmlspecialchars(($_POST['state']))       ; 
 $number = intval(($_POST['number']));

  for ($i = 0; $i < $number ; $i++ ) { 

  echo "
  <form action='sd2.php' method='post'>
    <label for='name'>Districts</label>
    <input type='text' name='district[]'>
    <br/><br/>
  </form>"; 

    }
    ?>   
    <?php     

 foreach($_POST['district'] as $senatorial) {
     $query = "INSERT INTO  state  ( `state_id`, `state`, `senatorial`)
     VALUES (NULL, '".$state."', '".$senatorial."') "; 
     mysql_query($query) or die (mysql_error());  
 }
 ?>
1
  • So you know: The for attribute of the <label> tag should be equal to the id attribute of the related element to bind them together. Commented Jun 19, 2012 at 20:35

1 Answer 1

2

This must work:

$count = count($_POST['district']);
for ($i=0; $i<$count; $i++){
    $district = $_POST['district'][$i];
    //do this
    //do that
}
Sign up to request clarification or add additional context in comments.

4 Comments

Try to print the values of $_POST and check that if you get the district or anything else.
Try this: print_r($_POST);
I have done that.. Now the array is formed and showing like this array([distinct]=> Array ([0]=> [1]=>)) [submit]=>Register) which is what I'm expecting. But I still get undefined Index for District.
Was "array([distinct]=>" in your last comment a typo, or is it actually distinct, not district?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.