简体   繁体   English

如何从我将输入命名为的表单中接收帖子 <?php echo $var ?> ?

[英]How do I receive post from a form where I name an input as <?php echo $var ?>?

I tried $_POST['<?php echo $var ?> ] but I should have known that it wouldn't be that easy. 我试过$_POST['<?php echo $var ?> ],但我应该知道它不会那么容易。

The reason why I try to do is because I have several input boxes with values I take from a database and I'm trying to create an updation script where any of the input box values can be changed. 我尝试这样做的原因是因为我有几个输入框,其中包含我从数据库获取的值,并且我正在尝试创建一个更新脚本,其中可以更改任何输入框值。

for example 例如

<form action="process.php" method="post">
<?php
while($variable=mysql_fetch_array($sqlconnec))
{
?>   
<input type="text" name="<?php echo $variable['col1']?>" value="<?php echo   $variable['val'] ?>" />
<?php 
}
?>
<input type="submit" />
</form>

Any help is appreciated. 任何帮助表示赞赏。

You need to do like this: 你需要这样做:

<form action="process.php" method="post">
<?php
$variable = mysql_fetch_assoc($sqlconnec);
foreach($variable as $col => $val)
{
?>
   <input type="text" name="<?php echo $col; ?>" value="<?php echo $val; ?>" />
<?php
}
?>
<input type="submit" />
</form>

Now, mysql_fetch_assoc gets you the database row in a associative array. 现在, mysql_fetch_assoc将获取关联数组中的数据库行。 Then, the code iterates each column in the row and displays the name/value pair for it. 然后,代码迭代行中的每一列并显示它的名称/值对。 And yes, you were not closing the value tag correctly. 是的,你没有正确关闭价值标签。

foreach($_POST as $k=>$v) {
//do something with $v or $_POST[$k]
}

I think that you want to change the name of the input to something that is constant. 我认为您想要将输入的名称更改为常量。

For example: 例如:

<input type="text" name="testname" value="<?php echo $variable['val'] ? />

And then retrieve your variable like so: 然后像这样检索你的变量:

$_POST['testname']

For example you could print the variable you sent in the input to test it like so: 例如,您可以打印输入中发送的变量来测试它,如下所示:

echo $_POST['testname'];

I think that what you need is: 我认为你需要的是:

<input type="text" name="<?php echo $col; ?>" value="<?php echo $val; ?>" />

$_POST[$col] //this will have the input value defined above.

In process.php you have to do the same query as mentioned above. 在process.php中,您必须执行与上述相同的查询。 If you iterate through those results $_POST[$col] will contain the posted values. 如果您遍历这些结果,$ _POST [$ col]将包含已发布的值。

你没有用“。”关闭输入'value'标签。另外你的第二个php结束标签是不正确的。

<input type="text" name="<?php echo $variable['col1']?>" value="<?php echo $variable['val'] ?>" />

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

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