Skip to content Skip to sidebar Skip to footer

Put Input Type Text In An Array

I have some text inputs. The inputs are produced from a while loop. Now I want put the values in an array. Like this: array values mark['0'] input1 mark['1'] input2 mark[

Solution 1:

You should likely have only 1 form element, not one for every row you are trying to output, and certainly not a separate one for the form submission button.

Your problem is that the actual form you are submitting has only one element in it - the submit button. Thus there are no input fields at all to post.

You should generate your form like this:

<formclass="form1"name="form1"method="post"><?phpwhile($row=mysql_fetch_array($result)){
?><inputtype="text"name="mark[]"/><?php
}
?><buttontype="submit"name="correction"></submit></form>

Solution 2:

Change your form to this:

<formclass="form1"name="form1"method="post"><?phpwhile ($row = mysql_fetch_array($result)) {
      echo'<input type="text" name="mark[]" />';
    }
  ?><inputtype="submit"name="correction"value="Submit" /></form>

And then:

if (isset($_POST['correction'])) {
  $grade = 0;   
  $mark  = $_POST['mark'];

  foreach ($markas$key => $value) {
    $grade += $value;
  }

  echo$grade;
}

Solution 3:

What you saying the last paragraph is correct, you are submitting the form1 which contains only the submit button, so mark doesn't exist in the PHP script that handles the POST.

so change the HTML to:

<formclass="form1"name="form1"method="post"><?phpwhile($row=mysql_fetch_array($result)){
?><inputtype="text"name="mark[]"/><?php
}
?><buttontype="submit"name="correction"></submit></form>

Post a Comment for "Put Input Type Text In An Array"