Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用生成的php变量作为html的名称<;输入>;循环中的对象_Php_Html - Fatal编程技术网

使用生成的php变量作为html的名称<;输入>;循环中的对象

使用生成的php变量作为html的名称<;输入>;循环中的对象,php,html,Php,Html,在第一页(index.php)上,我写道: <?php echo '<form action="test.php" method="get">'; $x=0; while($x<5){ $x++; $test='tester'.$x; echo '<input type="text" name="$test"><br>'; }; echo '<input type="submit" value="submit">'; ?>

在第一页(index.php)上,我写道:

<?php   
echo '<form action="test.php" method="get">';
$x=0;
while($x<5){
$x++;
$test='tester'.$x;
echo '<input type="text" name="$test"><br>';
};
echo '<input type="submit" value="submit">';
?>

在第二页(test.php)上,我写道:

<?php 
echo $_POST['tester1'];
echo $_POST['tester2'];
echo $_POST['tester3'];
echo $_POST['tester4'];
echo $_POST['tester5'];
?>

当我测试它时,我得到了这些错误

注意:第2行C:\xampp\htdocs\test.php中未定义的索引:tester1

注意:第3行C:\xampp\htdocs\test.php中未定义的索引:tester2

注意:第4行C:\xampp\htdocs\test.php中未定义的索引:tester3

注意:第5行C:\xampp\htdocs\test.php中未定义的索引:tester4

注意:第6行C:\xampp\htdocs\test.php中未定义的索引:tester5

下面的代码是供用户填写数字的真实代码的示例。我想在以后的计算中使用这些数字,所以它们都需要一个唯一的名称。
对象是通过循环生成的,循环运行的次数由数据库中的行数指定。

这就是它的工作方式(index.php):


只需将此代码附加到test.php的顶部即可

if(!isset($_POST['tester1']) || !isset($_POST['tester2']) ... etc){
     exit(0); //or do something to signal index.php of missing values
}
/* (the rest of the code */

在编写这样的动态页面时,您应该始终希望缺少一个变量并编写一个转义码,这样您就不会遇到这样的错误。

两个主要问题是,您在单引号中使用PHP变量,这不会传递实际的变量,而是传递实际的名称。例如

$foo = "bar";
echo 'This is $foo';
将打印

这是$foo

如果使用双引号,则会传递变量的内容

$foo = "bar";
echo "This is $foo"; // You can also do the following: echo 'This is '.$foo;
将打印

这是酒吧

其次,您正在表单中使用
method=“get”
,但试图将它们作为POST变量检索。这意味着您必须将其更改为
method=“POST”


另一种方法是创建一个元素数组,并在PHP中使用循环来检索值。下面给出一个例子。第一个代码段生成一个包含5个输入字段作为数组的表单

<form action="test.php" method="POST">
    <?php for ($x=0; $x<5; $x++) { ?>
    <input type="text" name="tester[]" />
    <?php } ?>
    <input type="submit" name="submit" />
</form>

在PHP中,循环遍历该数组

<?php
if (isset($_POST['tester']) {
    // If the values are set, to avoid Undefined Index notices
    foreach ($_POST['tester'] as $value) {
        echo $value."<br />";
    }
}
?>

在单引号中,PHP变量不会作为变量传递,但会输出实际文本。它应该看起来像
echo'
'
我们测试了这个,但它不起作用。你也有
method=“get”
,所以它们在URL中传递,而不是通过POST。将其更改为
method=“POST”
。应用我所评论内容的两个更改,应该可以。好的,谢谢,它可以工作。正如@Qirel所说,这些是您需要进行的更正,最重要的是,我希望您没有遗漏您的结束表单标记。不能使用
method=“get”
在PHP中尝试将它们作为POST检索时。是的,这是因为我使用方法get和POST进行了尝试,但在发布问题时没有正确更改
<?php
if (isset($_POST['tester']) {
    // If the values are set, to avoid Undefined Index notices
    foreach ($_POST['tester'] as $value) {
        echo $value."<br />";
    }
}
?>