我通过在单独的循环中将所有值添加到数组来解决此问题。感谢您的帮助。
$_POST['val1']
变量正在传递到刷新页面,但$_POST['val2']
不是。每个代码都是相同的,它们都在同一个表单中。欢迎任何想法。在这一点上,我认为我真的需要一个新的眼睛。
我只是包含代码的片段,因此更容易查看。程序中的其他所有内容都正常运行,我没有收到任何特定错误......只是$_POST['val2']
的值没有打印。
以下是定义$_POST
变量的代码:
// foreach ($line as $col_value) ...
if ($counter == 1):
echo "\t\t<input type='hidden' name='val1' value='$col_value' />";
elseif ($counter == 2):
echo "\t\t<input type='hidden' name='val2' value='$col_value' />";
endif;
以下是使用它们的代码:
for($i=1; $i<6; $i++) {
echo "\t<tr>\n";
echo "\t\t<td>";
if($i == 1){
echo "id";
echo "</td>\n";
echo "\t\t<td>" . $_POST['val1'] . "</td>\n";
} elseif($i == 2){
echo "name";
echo "</td>\n";
echo "\t\t<td>" . $_POST['val2'] . "</td>\n";
答案 0 :(得分:0)
我可能会建议分成两种形式,看看你想要完成什么
你想在输出中显示什么?
这段代码似乎是一个自我发布
还可以将隐藏文本框更改为普通文本框,以便您可以查看代码发生的情况。
<form action="" method="post">
<?php
$counter = 1; //default values are always good to have
$col_value = 0;
$col_value = @$_POST['val1'] ;
if ($counter == 1)
echo "\t\t<input name='val1' value='$col_value' />";
else
echo "\t\t<input name='val2' value='$col_value' />";
//I would rather write it like this:
$line = 1;
$counter = 1; //give it a default value to start off with.
$col_value = @$_POST['val1'] ;
$col_value = 0;
$line = array(1, 2,4);
foreach ($line as $col_value) {
echo "Current value of \$line: $col_value.\n";
echo "<input name='val1' value='$col_value' />";
echo "<input name='val2' value='$col_value' />";
}
?>
<input type="submit" name="submit" value="Submit" />
<br>
<br>
<table>
<?php
$val1 = $_POST['val1'] ;
$val2 = $_POST['val2'] ;
echo "POSTval1: ".$val1."<br>";
echo "POSTval2: ".$val2."<br>";
for($i=1; $i<6; $i++) {
echo "\t<tr>\n";
echo "\t\t<td>";
if($i == 1){
echo "id";
echo "</td>\n";
echo "\t\t<td>" . $val1 . "</td>\n";
} elseif($i == 2){
echo "name";
echo "</td>\n";
echo "\t\t<td>" . $val2 . "</td>\n";
}
} //don't forget the closing brackets.
?>
</table>
</form>
答案 1 :(得分:0)
这里有一个详细的答案,我会回答我自己的问题。
这是我如何解决这个问题的一个例子。此循环在表单之前完成:
if($table == 'language'){ //this cycles through query to get value of $language... but it only gets last value of language from search query
$numRow = 0;
//results are good so output them to HTML
while ($line1 = pg_fetch_array($result, null, PGSQL_ASSOC)){
$counter = 0;
foreach ($line1 as $col_value){ // then add all data for attributes in succeeding columns
if($counter == 1){
$language[$numRow] = $col_value;//array($numRow => $col_value);
$counter++;
}
else
$counter++;
}
$numRow++;
}
这就是信息在表单中的添加方式:
echo "\t\t<input type=\"hidden\" name=\"language\" value=\"$language[$numRow]\" />";