通过PHP脚本传递HTML隐藏变量

时间:2015-12-23 10:48:21

标签: php html

我有一个HTML表单,它是从我的php脚本中回应的。当我尝试通过脚本传递隐藏变量时,代码无法按预期工作。

$threadNumber=$row["Tid"];
echo "<center><b>Message# ", $messageNumber , ": </b><br>";
echo $row["Mtitle"] , " in Thread# " , $row["Tid"] , "<br>";
echo "The Message was Written on " , $row["Mdate"] , '<br>';
echo $row["Mbody"];
echo "<br><br>";
echo '<form action="messageReply.php" method="post">';
echo '<textarea name="reply" rows=5 cols=30 placeholder="Reply to the Message?"></textarea>';
echo '<input type="hidden" name="Mtitle" value="<?php echo $row["Mtitle"] ?>">';
echo '<input type="submit" value="Send Message">';
echo '</form>';

结果如下:

enter image description here

当我尝试在messageReply.php脚本中读取$ _POST [“Mtitle”]时,我收到一条错误,指出这样的索引不存在。

2 个答案:

答案 0 :(得分:1)

试试这个:

echo '<input type="hidden" name="Mtitle" value='.$row['Mtitle'].'>';

您的完整代码(已修改):

$threadNumber=$row["Tid"];
echo "<center><b>Message# ", $messageNumber , ": </b><br>";
echo $row["Mtitle"] , " in Thread# " , $row["Tid"] , "<br>";
echo "The Message was Written on " , $row["Mdate"] , '<br>';
echo $row["Mbody"];
echo "<br><br>";
echo '<form action="messageReply.php" method="post">';
echo '<textarea name="reply" rows=5 cols=30 placeholder="Reply to the Message?"></textarea>';
echo '<input type="hidden" name="Mtitle" value='.$row['Mtitle'].'>';
echo '<input type="submit" value="Send Message">';
echo '</form>';

答案 1 :(得分:1)

注意:你在php中使用php。请在代码下面更改:

<?php
echo '<input type="hidden" name="Mtitle" value="'.$row["Mtitle"].'">';
?>