Forum Moderators: coopster

Message Too Old, No Replies

Echoed value is truncated - why?

echo to populate form fails

         

GGR_Web

2:57 pm on Jul 22, 2009 (gmt 0)

10+ Year Member



Hi.

First the code:

<?php while ($row = mysql_fetch_array($form)) {
echo '<option value='.$row['site'].'>'.$row['site'].'</option>';
};

This snippet takes a list of names from a table (which is $form) and populates a drop down list. The problem I am having is that the variable for the option value is being tunicated at the first space.

E.G
'Big Jumpers' becomes 'Big'.

I suspect the problem is the > character between the two variables but I can't seem to find a way round it.

Suggestions?

janharders

3:45 pm on Jul 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



is it really truncated in the html-source of the page or is the data truncated that is transmitted to your script when you submit the form?
in the latter case, add quotes around the value to indicate where it starts and ends, that is:

echo '<option value="'.$row['site'].'">'.$row['site'].'</option>';

also, you might want to look into escaping your strings for html usage, depending on charsets etc, look at htmlentities() in php.

GGR_Web

3:55 pm on Jul 22, 2009 (gmt 0)

10+ Year Member



Ahh. That made it work.

I thought I'd already tried that approach - I must of messed it up somewhere.

Am I alone in thinking echo is the hardest function to use in php? I seem to have to relearn it every time I use it.

andrewsmd

4:32 pm on Jul 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Once you understand the difference between " and ' then it's not too bad. However, understanding all of the different ways you can use them is a bit tricky.