Jump to content

escaping single quote of string echoed to input text box


dmikester1

Recommended Posts

I'm really struggling here.  I need to ouput a last name to a input text box.  The name has a single quote in it.  I can only get the text before the quote to output.  Here is my php code:

echo "<div class='fifteen'>$i. First name:</div><input type='text' value='$firstName' id='$first' name='$first' size='12' />  Last name:$lastName <input type='text' value='$lastName' name='$last' id='$last' size='12' /> Year: ";

 

The $lastName variable has a single quote in it.  I've tried addslashes, stripslashes, and mysql_real_escape_string alone and in combination all to no avail.  Can someone help me?

Thanks.

Mike

The single quote in the variable $lastName is closing the single quote of the attribute value in the input tag in the html.

This problem can't be fixed using any function in php, you have to use double quotes in html to fix it.

 

good luck!

do htmlentities

 

Like this:

echo "<div class='fifteen'>$i. First name:</div><input type='text' value='".htmlentities($firstName)."' id='$first' name='$first' size='12' />  Last name:$lastName <input type='text' value='".htmlentities($lastName)."' name='$last' id='$last' size='12' /> Year: ";

True, this can be done by htmlentities but why using extra functions if the code can work without it.

 

This will certainly work:

 

echo '<div class="fifteen">'.$i.'. First name:</div><input type="text" value="'.$firstName.'" id="'.$first.'" name="'.$first.'" size="12" />  Last name:'.$lastName.' <input type="text" value="'.$lastName.'" name="'.$last.'" id="'.$last.'" size="12" /> Year:';

 

I used concatenation between strings, you got the idea.

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.