简体   繁体   中英

How to store multiple text box values in array

Is it possible to store multiple textbox values in array, i have N number of textboxes

<input type="text" name="grade[]" id="grade" />
<input type="text" name="grade[]" id="grade" />
<input type="text" name="grade[]" id="grade" />

i tried this code to add all the text box value but it returns only the last text box value.

    $grade=$_POST['grade'];
for($i=1;$i<=3;$i++)
{
    $per=$grade[$i]*$grade[$i];
    echo $per;
}

Besides of starting on 0, it should finish on 2 if you have 3 text boxes.

for($i=0;$i<=2;$i++)
{
  $per=$grade[$i]*$grade[$i];
  echo $per;
}

Or you could use the array length if you don't want to hardcode the number of iteractions. This should work:

for($i=0;$i<=count($grade)-1;$i++)
{
  $per=$grade[$i]*$grade[$i];
  echo $per;
}

EDIT

This should work too and it's slightly cleaner (avoiding the -1) and using the pow() function:

for($i=0;$i<count($grade);$i++)
{
  echo pow($grade[$i], 2);
}

Try this one...

<?php

    foreach ($_GET['grade'] as $grade){
        $per = $grade * $grade;
        echo $per;
    }

?>

Try using this

$per='';
$grade=$_POST['grade'];
for($i=0;$i<count($grade);$i++)
{
    $per .=$grade[$i]*$grade[$i];
    $per .='<<>>';
}
 echo $per;

count($grade) is used for n no. of textboxes . You need to concatenate the values of variable in order to get the values of all the textboxes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM