PHP: double quotes don't show in input value, but are shown in normal echo

无人久伴 提交于 2019-12-20 02:36:16

问题


I have some strings in the database like SNEAKERS "SUPER STAR" GOLDEN GOOSE. These are the titles for some products. When I output it normally inside a <p> it shows the quotes, but when I echo it inside an input value, <input type="text" value="<?= $product->title ?"> the string gets truncated before the first double quote, so the value becomes just SNEAKERS.

Is there a way I can output the double quotes inside the value of an input ?

EDIT: The closing tag was a typo, in the code it is closed.


回答1:


Use htmlspecialchars like so:

htmlspecialchars($product->title);

i.e

<input type="text" value="<?= htmlspecialchars($product->title) ?>">



回答2:


Evaluate this html, I think you'll see where the problem lies:

<input type="text" value="SNEAKERS "SUPER STAR" GOLDEN GOOSE">

If you look closely, you'll see that the double quotes from the string are closing the double quotes for the input. The solution to this as others have pointed out is to call htmlspecialchars and pass it the string prior to outputting it.

You're also missing the ending > for the closing PHP tag.




回答3:


You're missing your closing tag try this:

   <input type="text" value="<?= $product->title ?>"> 

Also you need to escapte the double quotes inside the html (as in Wayne answer)



来源:https://stackoverflow.com/questions/25164007/php-double-quotes-dont-show-in-input-value-but-are-shown-in-normal-echo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!