簡體   English   中英

如何從我將輸入命名為的表單中接收帖子 <?php echo $var ?> ?

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

我試過$_POST['<?php echo $var ?> ],但我應該知道它不會那么容易。

我嘗試這樣做的原因是因為我有幾個輸入框,其中包含我從數據庫獲取的值,並且我正在嘗試創建一個更新腳本,其中可以更改任何輸入框值。

例如

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

任何幫助表示贊賞。

你需要這樣做:

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

現在, mysql_fetch_assoc將獲取關聯數組中的數據庫行。 然后,代碼迭代行中的每一列並顯示它的名稱/值對。 是的,你沒有正確關閉價值標簽。

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

我認為您想要將輸入的名稱更改為常量。

例如:

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

然后像這樣檢索你的變量:

$_POST['testname']

例如,您可以打印輸入中發送的變量來測試它,如下所示:

echo $_POST['testname'];

我認為你需要的是:

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

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

在process.php中,您必須執行與上述相同的查詢。 如果您遍歷這些結果,$ _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