【问题标题】:Passing HTML hidden variable through a PHP script通过 PHP 脚本传递 HTML 隐藏变量
【发布时间】:2016-03-29 18:51:16
【问题描述】:

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

$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>';

结果如下所示:

当我尝试在 messageReply.php 脚本中读取 $_POST["Mtitle"] 时,我收到一条错误消息,指出此类索引不存在。

【问题讨论】:

  • 你不能在 php 中使用 PHP.. echo ''; - 你在之前的代码中做对了,为什么突然改变主意了?
  • @Crt:如果您认为您的问题已得到解决,请选择最佳答案并标记为已接受,因为这将对面临相同问题的其他人提供充分的帮助。

标签: php html


【解决方案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>';

【讨论】:

  • 这很好,如果您是 SO 新手,请接受最佳答案,这将有助于其他面临此问题的人。 @Crt
  • 当然,没问题。抱歉,很忙。你或其他人能解释一下为什么会这样吗?
  • @crt:为此我建议阅读单引号和双引号是如何工作的......你会得到基本的想法兄弟
【解决方案2】:

注意:您在 php.ini 中使用 php。请在下面的代码中更改它:

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

【讨论】:

    猜你喜欢
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多