dtd14883
2011-02-01 05:33
浏览 159
已采纳

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

I tried $_POST['<?php echo $var ?>] but I should have known that it wouldn't be that easy.

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.

图片转代码服务由CSDN问答提供 功能建议

我试过 $ _ POST ['&lt;?php echo $ var?&gt; ] 但我应该知道它不会那么容易。

我尝试做的原因是因为我有几个输入框,其中包含我从数据库获取的值,而我是 尝试创建一个更新脚本,其中任何输入框值都可以更改。

例如

 &lt; form action =“  process.php“method =”post“&gt; 
&lt;?php 
while($ variable = mysql_fetch_array($ sqlconnec))
 {
?&gt;  
&lt; input type =“text”name =“&lt;?php echo $ variable ['col1']?&gt;”  value =“&lt;?php echo $ variable ['val']?&gt;”  /&gt; 
&lt;?php 
} 
?&gt; 
&lt; input type =“submit”/&gt; 
&lt; / form&gt; 
   
 
 

感谢任何帮助。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • dongxiong1935 2011-02-01 08:14
    已采纳

    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. If you iterate through those results $_POST[$col] will contain the posted values.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dtg25862 2011-02-01 05:37

    You are not closing your input 'value' tag with ". Also your second php closing tag is incorrect.

    <input type="text" name="<?php echo $variable['col1']?>" value="<?php echo $variable['val'] ?>" />
    
    评论
    解决 无用
    打赏 举报
  • douyangqian5243 2011-02-01 05:37

    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. 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.

    评论
    解决 无用
    打赏 举报
  • duanmei1894 2011-02-01 05:38
    foreach($_POST as $k=>$v) {
    //do something with $v or $_POST[$k]
    }
    
    评论
    解决 无用
    打赏 举报
  • dongyue8640 2011-02-01 05:38

    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'];
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题