Textfield is having problems parsing its value?
So I got this form:
<form action="welcome.php" method="post">
<input type="hidden" name="secret" value=<?php echo getMyValue() ?> />
Name: <input type="text" name="txt" value=<?php echo getMyValue() ?> />
<input type="submit" />
</form>
You see the text input element? The value is the result of getMyValue()
. Such result is a line that contains HTML tags, like <b> <i>
etc... And, apparently, when the text contains such tags, my text input breaks. If the text doesn't have 开发者_JAVA百科such tags, everything is alright.
Any ideas to fix this parsing problem? (dunno if it is actually parsing the problem...)
EDIT Here is the function's code:
function getMyValue()
{
$con = mysql_connect("myConnectionDataStuff");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("myUsername", $con);
$result = mysql_query("SELECT * FROM DATA");
while($row = mysql_fetch_array($result))
{
return $row["TEXT"];
}
mysql_close($con);
Your value should be enclosed in quotes. It will enter everything properly if you enclose it in quotes, but the tags won't be parsed; they will simply be "as-is" in the text field.
Use this code instead:
<form action="welcome.php" method="post">
<input type="hidden" name="secret" value="<?php echo getMyValue() ?>" />
Name: <input type="text" name="txt" value="<?php echo getMyValue() ?>" />
<input type="submit" />
</form>
you could use htmlspecialchars()
Name: <input type="text" name="txt"
value="<?php echo htmlspecialchars(getMyValue(),ENT_QUOTES) ?>" />
be aware if getMyValue() function returns single or double quote your input text will be broken.
精彩评论