Jump to content

Quotes and Double Quotes in forms


pault

Recommended Posts

A problem has arisen which puzzles me. I have forms which save data to MySql and retrieve it, showing it as the default data in the form. Naturally I escape any quotes before sending it to the database and remove the slashes when I retrieve it. But the form HTML code shows the data like this value="$variable" which is fine when only single quotes are used in the data but causes a problem when the user uses double quotes. So data of John \"Jack\" Smith would be output as value="John "Jack" Smith" with obvious problems. If I use value='...' then that would cause problems with single quotes.

 

I haven't seen the answer in any of my books. The only things I can think of is changing all double quotes to single before saving to DB or converting them with htmlspecialcharacters so they are no longer actual quotes.

Link to comment
https://forums.phpfreaks.com/topic/216081-quotes-and-double-quotes-in-forms/
Share on other sites

htmlspecialcharacters is the way I go.

 

I like to echo input fields without concatenation, so I single-quote tag values:

 

$field1_value = htmlspecialchars($field1_value, ENT_QUOTES);
echo "<input type='text' name='field1' value='$field_1_value' />";

 

alternatively, if you're echoing within the HTML:

 

<input type='text' name='field1' value='<?php echo htmlspecialchars($field_1_value, ENT_QUOTES); ?>' />

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.