Text after space character cut off in input text field when editing form back

Hi I am trying to build a form to edit Mysql data, however when I retrieve the data and display it in a form input text box, all characters after the first space are cut off.
When I echo out the variable value immediately before inserting into the form, the data displays correctly (ie. full name instead of just the first name).
I have tried adding a   character before inserting into the form using the following code:

$record[‘skip’] = str_replace(’ '," ",$record[‘skip’]);

and this sort of works, however it actually adds a  character (ASCII 194) to the data.
An extract of my code is:

[I] while ($record = $result2->fetch()){

/* echo $record[‘skip’];
echo “<br />”;
echo $record[‘third’];
echo “<br />”;
*/
echo “<form action = db_update_team.html.php method = POST>”;
echo “<tr>”;
echo “<td>” . date(‘d M Y’, strtotime($record[‘gamedate’])) . “</td>”;
echo “<td>” . “<input type=text name=skip value=” . $record[‘skip’] . “> </td>”;
echo “<td>” . “<input type=text name=third value=” . $record[‘third’] . “> </td>”;[/I]

Any help would be appreciated.

Classic error, you are not quoting the string value correctly in your html.

The best way to understand fully what is going on is to look at the source code of your form.

Correctly formatted html should look like this:


<input type=text name = "a_name" value = "I just don't care" />

You probably have


<input type=text name = "a_name" value = I just don't care />

In that example the name attribute actually does not need quoting because it contains no spaces, whereas the value attribute does, so needs to be correctly quoted - either single or double quotes will do.

So you just add some extra quotes, HOWEVER, dont end up with this: (single quotes containing a single quote)


<input type=text name = 'a_name' value = 'I just don't care' />

Something like this:


echo '<td><input type=text name=third value="' . $record['third'] . '" /></td>';

Look into using something to correctly escape the values you are outputting to correctly escape double quotes too.

So you need to plan carefully which quotes you will be outputting where. Investigate other ways of doing it, my preference would be to use single quotes in PHP to join up the values, and output double quotes in HTML. I does not matter much which system you adopt but the thing is to stick with only one scheme.

Look at the string section of the manual for a fuller explanation as well as [URL=“http://stackoverflow.com/questions/1097135/escape-double-quotes-of-html-attributes-output-by-php”]here for a discussion on sprintf, heredoc and templates.

1 Like

Thank you - I probably had too many quoted strings and was getting myself confused. Works perfectly now!!