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
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); ?>' />

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.