简体   繁体   English

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

[英]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 是否可以在数组中存储多个文本框值,我有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;
}

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

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. count($grade)用于n no。 of textboxes . 文本框 You need to concatenate the values of variable in order to get the values of all the textboxes. 您需要连接变量的值以获取所有文本框的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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