将 php 变量放入 HTML 表单值中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3317730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:19:28  来源:igfitidea点击:

putting a php variable in a HTML form value

phphtml

提问by Skizit

I've got a php variable like so.. $name = $_REQUEST['name'];I'd like to put it in a HTML form field's value e.g in here.. <input type="text" name="name" value=(php variable here) />How would I do so?

我有一个像这样的 php 变量..$name = $_REQUEST['name'];我想把它放在一个 HTML 表单字段的值中,例如在这里..<input type="text" name="name" value=(php variable here) />我该怎么做?

Thanks.

谢谢。

回答by Quentin

value="<?php echo htmlspecialchars($name); ?>"

回答by Rich Adams

You can do it like this,

你可以这样做,

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

But seen as you've taken it straight from user input, you want to sanitize it first so that nothing nasty is put into the output of your page.

但是,当您直接从用户输入中获取它时,您希望首先对其进行清理,以便不会将任何令人讨厌的内容放入您的页面输出中。

<input type="text" name="name" value="<?php echo htmlspecialchars($name);?>" />