我正在尝试使用复选框输入字段末尾的$row['file_name']
内容,并将其用于value=""
。
<?php
echo "<form action=\"process.php\" method=\"post\">";
while($row = mysql_fetch_array($result))
{
echo "<input type=\"checkbox\" name=\"opt[]\" value=\" \" /> " . $row['file_name'];
echo "<br />";
}
echo "<br><br>";
echo "<input type=\"submit\" name=\"formSubmit\" value=\"Send\" />";
echo "</form>";
?>
答案 0 :(得分:4)
$variable = "world";
echo "Hello, $variable";
//or
echo "Hello, {$variable}"
//or
echo 'Hello, '.$variable.'!'; // .(dot) concatenates strings
使用双引号(“)你可以直接在字符串中使用变量(ex 1),使用单引号(')变量内容不会:
echo 'Hello, $variable'; //OUTPUT: Hello, $variable
echo 'Hello, '.$variable; //OUTPU: Hello, world
答案 1 :(得分:3)
您可以像这样回显字符串中的变量
echo "<input type=\"checkbox\" name=\"opt[]\" value=\"$row[file_name]\" /> $row[file_name]";
为了做到这一点(在字符串中嵌入任何php变量),定义字符串的引号必须是双引号(就像你拥有它一样)。
另请注意,在将数组元素嵌入到字符串中时,必须删除在数组中定义字符串索引的引号,或者用大括号括起数组元素,如下所示:
echo "{$row['file_name']}";
答案 2 :(得分:3)
你可以做到
echo "<input type=\"checkbox\" name=\"opt[]\" value=\"{$row['file_name']}\" /> " . $row['file_name'];
答案 3 :(得分:2)
如果让html打印自己并使用PHP进行模板化
,您的代码看起来会更清晰<form action="process.php" method="post">
<?php while($row = mysql_fetch_array($result)): ?>
<input type="checkbox" name="opt[]" value="<?php echo $row['file_name'] ?>" />
<br />
<?php endwhile; ?>
<br /><br />
<input type="submit" name="formSubmit" value="Send" />
</form>