0

Hi I have a text box dynamically created to get a value from a database. The specific line in the code is as follows and it works fine;

<input name='routename'  class='colr' type='text' id='routename' size='20' maxlength='40' value=".$row['route']."></td>

How ever my database has a value 'colombo/ srilanka' and when the result is loaded to the text box it captured only 'colombo/' and 'srilanka' is missing. In other words text after the space is not loaded to the textbox. Can someone help me with a workaround?

Thanks for looking!

2 Answers 2

1

You missed the quotes of the value, and don't forget using htmlspecialcharor htmlentities.

"<input name='routename'  class='colr' type='text' id='routename' size='20' maxlength='40' value='".htmlspecialchar($row['route'])."'></td>"
2
  • Bingo! :D it worked! also xdazz would you mind explaining the difference circumstances htmlspecialchar or htmlentities should use or both deliveres the same protection?
    – Amw Amw
    Oct 4, 2012 at 3:53
  • You could use either of them. The difference is what gets encoded, the special char or the entity.
    – xdazz
    Oct 4, 2012 at 4:00
1

Try htmlentities():

echo "<input name='routename'  class='colr' type='text' id='routename' size='20' maxlength='40' value='".htmlentities($row['route'])."' /></td>";

When you echo anything from PHP into HTML, you should always wrap the string with htmlentities to make sure the outputted string is safe for HTML to display (without inadvertently writing markup to the page instead).

3
  • Nope Stegrex! still I dont get the rest with htmlentities()
    – Amw Amw
    Oct 4, 2012 at 3:49
  • As xdazz mentioned yes quotes are missing and your suggestions worked! Thank you!
    – Amw Amw
    Oct 4, 2012 at 3:51
  • @AmwAmw Try doing echo $row['route']; and check the source of the page in a browser (not just what you see on the page, basically hit Ctrl+U). Also not that you need the single quotes to wrap around the value right after "value=" as xdazz noted.
    – Stegrex
    Oct 4, 2012 at 3:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.