Medi0cr3
Medi0cr3

Reputation: 15

jQuery apostrophes from php to an input field

I'm taking database information that contains a character of blah^s (I replaced the ' with a ^, so I could identify where in the row I had apostrophes). I'm retrieving and using preg_replace to place an apostrophe back in the string. Its all working great, until I try to use jquery to update an input text field.

$name = preg_replace('/(\^)/', ' ', '\'');
$name = "<input type='text' size='20' id='2' name='2' value='$name'>&nbsp;";

I'm JSON encoding it fine with all of my other data.

$json = array(
'name' => $name
);
echo json_encode($json);

This returns, in the console utilizing jquery, "blah". Instead of "blah's" inside my input text field of which is what I need..

Does anybody have any ideas?

Upvotes: 0

Views: 331

Answers (1)

Felix Kling
Felix Kling

Reputation: 816232

Look at the generated HTML. You are using ' to delimit the attributes value. So you are generating something like

value='blah's'

As you can see, the value ends after h.

You'd have to use an HTML entity for ' instead. You can do that with htmlentities:

echo htmlentities($str, ENT_QUOTES);

or htmlspecialchars:

echo htmlspecialchars($str, ENT_QUOTES);

Upvotes: 1

Related Questions