SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    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>";


    Any help would be appreciated.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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:
    Code:
    <input type=text name = "a_name" value = "I just don't care" />
    You probably have
    [code]
    <input type=text name = "a_name" value = I just don't care />
    [code]

    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)
    Code:
    <input type=text name = 'a_name' value = 'I just don't care' />

    Something like this:
    PHP Code:
    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 here for a discussion on sprintf, heredoc and templates.

  3. #3
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Classic error, you are not quoting the string value correctly in your html.
    Thank you - I probably had too many quoted strings and was getting myself confused. Works perfectly now!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •