如何在数组中存储多个文本框值

时间:2022-04-25 06:28:53

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

是否可以在数组中存储多个文本框值,我有N个文本框

<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;
}

3 个解决方案

#1


2  

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

除了从0开始,如果你有3个文本框,它应该在2结束。

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:

这也应该工作,它稍微清洁(避免-1)并使用pow()函数:

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

#2


2  

Try this one...

试试这个......

<?php

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

?>

#3


0  

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.

count($ grade)用于n no。的文本框。您需要连接变量的值以获取所有文本框的值。

#1


2  

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

除了从0开始,如果你有3个文本框,它应该在2结束。

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:

这也应该工作,它稍微清洁(避免-1)并使用pow()函数:

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

#2


2  

Try this one...

试试这个......

<?php

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

?>

#3


0  

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.

count($ grade)用于n no。的文本框。您需要连接变量的值以获取所有文本框的值。