dmikester1 Posted February 4, 2010 Share Posted February 4, 2010 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 Link to comment https://forums.phpfreaks.com/topic/190875-escaping-single-quote-of-string-echoed-to-input-text-box/ Share on other sites More sharing options...
Hussam Posted February 4, 2010 Share Posted February 4, 2010 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! Link to comment https://forums.phpfreaks.com/topic/190875-escaping-single-quote-of-string-echoed-to-input-text-box/#findComment-1006556 Share on other sites More sharing options...
The Little Guy Posted February 4, 2010 Share Posted February 4, 2010 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: "; Link to comment https://forums.phpfreaks.com/topic/190875-escaping-single-quote-of-string-echoed-to-input-text-box/#findComment-1006585 Share on other sites More sharing options...
Hussam Posted February 4, 2010 Share Posted February 4, 2010 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. Link to comment https://forums.phpfreaks.com/topic/190875-escaping-single-quote-of-string-echoed-to-input-text-box/#findComment-1006628 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.