php 如何设置 HTML 值属性(带空格)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3078192/
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 08:38:01  来源:igfitidea点击:

How to set HTML value attribute (with spaces)

phphtmlforms

提问by Kim Prince

When I use PHP to set the value of a HTML form input element, it works fine provided I don't have any spaces in the data.

当我使用 PHP 设置 HTML 表单输入元素的值时,只要数据中没有任何空格,它就可以正常工作。

<input type="text" name="username"
<?php echo (isset($_POST['username'])) ? "value = ".$_POST["username"] : "value = \"\""; ?> />

If I enter "Jonathan" as the username, it is repeated back to me as expected. If I enter "Big Ted", however, I only get "Big" repeated back when I submit the form.

如果我输入“Jonathan”作为用户名,它会按预期重复给我。但是,如果我输入“Big Ted”,则在提交表单时只会重复返回“Big”。

Note that the $_POST["Username"]variable is correct; when I echo it using PHP, it is set to "Big Ted".

注意$_POST["Username"]变量是正确的;当我使用 PHP 回显它时,它被设置为“Big Ted”。

回答by BalusC

Quote it. Otherwise the space will just become an attribute separator and everything after spaces will be seen as element attributes. Rightclick page in webbrowser and view source. It should not look like this (also see syntax highlight colors):

引用它。否则,空格将成为属性分隔符,空格之后的所有内容都将被视为元素属性。右键单击网页浏览器中的页面并查看源代码。它不应该是这样的(另请参阅语法高亮颜色):

<input value=Big Ted>

but rather this

而是这个

<input value="Big Ted">

Not to mention that this would still break when someone has a quote in his name (and your code is thus sensitive to XSSattacks). Use htmlspecialchars().

更不用说当有人在他的名字中有引号时这仍然会中断(因此您的代码对XSS攻击很敏感)。使用htmlspecialchars().

Kickoff example:

开场示例:

<input value="<?php echo (isset($_POST['username']) ? htmlspecialchars($_POST['username']) : ''); ?>">

回答by Cristian

<input type="text" name="username"
<?php echo (isset($_POST['username'])) ? "value = '".$_POST["username"]' : "value = ''"; ?> />

You have to wrap the variable result with quotes, so that the browser can know what's the content of the input.

你必须用引号将变量 result 括起来,这样浏览器才能知道输入的内容是什么。

回答by amruzzzzzzzzzi

just make sure you put the colon after the field for example :

只需确保将冒号放在字段之后,例如:

  <option value="'.$row['name'].'">

回答by meder omuraliev

<input type="text" name="username"
<?php echo (isset($_POST['username'])) ? ('value = "'.$_POST["username"].'"') : "value = \"\""; ?> />

Be aware of your quote usage.

请注意您的报价使用情况。

回答by Your Common Sense

As you see its not PHP5 or even PHP question at all.
Basic HTML knowledge is obligatory for one who want to be a PHP user.

如您所见,它根本不是 PHP5 甚至 PHP 问题。
对于想成为 PHP 用户的人来说,基本的 HTML 知识是必不可少的。

And with using templates it looks way more neat:

使用模板,它看起来更整洁:

Getting data part code:

获取数据部分代码:

$username = "";
if isset($_POST['username'])) $username = htmlspecialchars($_POST["username"]);

And template code:

和模板代码:

<input type="text" name="username" value="<?=$username?>">

If you divide your code to 2 parts it become way more supportable and readable.

如果您将代码分成 2 部分,它会变得更加可支持和可读