【问题标题】:Display entered text with echo after input PHP输入 PHP 后显示带有回显的输入文本
【发布时间】:2012-01-28 15:21:35
【问题描述】:

我想做一些非常简单的事情。用户在 HTML 文本框中输入文本然后点击提交后,PHP 回显输入的文本。我搜遍了,我不知道该怎么做!一定有什么办法!我想要这样的东西,除了 $foo 是输入的文本。

<html>
<input type="text" name="something" value="$foo"/>
<html>
<?php
echo $foo = "ben";
echo "foo is $foo"; // foo is foobar


?>

【问题讨论】:

  • 您想在不重新加载页面或提交页面时同时执行此操作?
  • 我猜你的答案在下面。这里的诀窍是您需要将input 包装在form 中。如果您想同时实现这一点,则需要使用 JavaScript。

标签: php html text textbox


【解决方案1】:

FTFY

<html>
<head><title>some title</title></head>
<body>
  <form method="post" action="">
    <input type="text" name="something" value="<?= isset($_POST['something']) ? htmlspecialchars($_POST['something']) : '' ?>" />
    <input type="submit" name="submit" />
  </form>

<?php
if(isset($_POST['submit'])) {
  echo 'You entered: ', htmlspecialchars($_POST['something']);
}
?>
</body>
<html>

【讨论】:

  • +100 for htmlspecialchars - 我使用它,但在这种情况下我肯定会忘记这一点! :-)
  • 我希望 PHP 在一个单独的文件中。我该如何更改它才能正常工作??
  • 将表单的action属性改为指向脚本
  • 简单而伟大!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-08
  • 2013-08-14
  • 1970-01-01
  • 2021-12-06
  • 2013-05-24
  • 2013-05-17
  • 1970-01-01
相关资源
最近更新 更多