For a variable inside a echo that contains HTML, where would I add slashes to escape the double quotes?
Example:
echo "<input type=\"hidden\" name=\"id\" value=".$row['id']." />";
This part:
value=".$row['id']."
For a variable inside a echo that contains HTML, where would I add slashes to escape the double quotes?
Example:
echo "<input type=\"hidden\" name=\"id\" value=".$row['id']." />";
This part:
value=".$row['id']."
Some tips on outputting HTML with PHP:
htmlspecialchars()
to properly escape any "rogue" values you may have.Example using echo
:
echo '<input type="hidden" name="id" value="', htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8'), '" />';
Or printf()
:
printf('<input type="hidden" name="id" value="%s" />',
htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8')
);
Or, in HTML mode:
?>
<input type="hidden" name="id" value="<?php echo htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8'); ?>" />
<?php
htmlspecialchars
should suffice for a double-quoted value
attribute so you can safely leave off the second and third arguments. –
Memo the defaults for htmlspecialchars should suffice for a double-quoted value
Nope, at least for me I need a ENT_QUOTES
for htmlspecialchars
to work –
Bahaism Use htmlentities
:
echo "<input type=\"hidden\" name=\"id\" value=\"".htmlentities($row['id'])."\" />";
How about use single quotes so you don't have to escape any quotes. Like so:
echo '<input type="hidden" name="id" value="'.$row['id'].'" />';
© 2022 - 2024 — McMap. All rights reserved.
htmlspecialchars
– Chartism