Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Variable containing a space

Status
Not open for further replies.

Momozone

IS-IT--Management
Dec 2, 2004
103
IE
I have a form that populates itself from mysql. One of the feilds is called name.

I am displaying the text feild like below with the variable in it.

echo "</td> </tr> <tr> <td> <input type='text' name='newname' value=".$info['name']." size=40 maxlenght=40> </td> </table>";

However, when I view this if the value in the DB for name is "Mr X" I only see Mr in the form.

How can I display the whole variable including anything after the space.

MoMoZoNe
 
</td>
</tr>
<tr>
<td>
<input type="text" name="newname" value="<?=$info['name']?>" size="40" maxlenght="40" />
</td>
</tr>
</table>

Olav Alexander Mjelde
Admin & Webmaster
 
As i said the variable is working. The variable has a space in it and any data after the space is not being displayed.

My code does exactly what your does... doesnt it?

MoMoZoNe
 
Hi

MoMoZoNe, please do not just read Olav's post, but also try it.

Assuming you did not took care of them previously, consider using for example the htmlspecialchars() function to encode characters with special meaning for HTML :
Code:
<input type="text" name="newname" value="<?php echo htmlspecialchars($info['name']); ?>" size="40" maxlenght="40">

Feherke.
 
feherke,

ITs obvioud from his post it would not work. To Clarify I did try it and it was not useful.

I have done this and it resolved it:

echo "</td> </tr> <tr> <td> <input type='text' name='newname' value=\"".htmlspecialchars($info['name'],ENT_QUOTES)."\" size=40 maxlenght=40> </td> </table>";

Anyway guys thanks for your attempts.

MoMoZoNe
 
Hi,
1) you where missing html tags
2) you had errors in your html tags

There should be no problem parsing a variable containing a space, but you can try to parse it through htmlentities() and strip_tags.

You will need to have strip_tags as the inner function.

eg.
Code:
function parseVarSafe($variable) {
  return htmlentities(strip_tags($variable));
  }

Then you can call it like so:
Code:
<?php
echo parseVarSafe($info['name']);
?>

ps. instead of echoing your entire table, you can echo vars like so:
Code:
<input type="text" name="name" value="<?=$value?>" />
This makes the readability way better and I would think it also parses faster.

Olav Alexander Mjelde
Admin & Webmaster
 
Attributes in HTML like the value attribue ina text box need to be enclosed by quotes, otherwise, the Browser will take the value up to the first space it finds and then the rest it considers as additional attributes.

So from your code do this:

Code:
echo "</td> </tr> <tr> <td> <input type='text' name='newname' value=[red]'[/red]".$info['name']."[red]'[/red] size=40 maxlenght=40> </td> </table>";

Notice the red single quotes surrounding your variable.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top