tags:

views:

64

answers:

4

When I use PHP to set the value of a HTML form input element, it works fine provided I don't have any spaces in the data.

Here's the code:

<input type="text" name="username"
<?php echo (isset($_POST['username'])) ? "value = ".$_POST["username"] : "value = \"\""; ?> />

If I enter "Jonathan" as the username, it is repeated back to me as expected. If I enter "Big Ted", however, I only get "Big" repeated back when I submit the form...

Note that the $_POST["Username"] variable is correct. (I.e. when I echo it using PHP, it is set to Big Ted.)

+1  A: 
<input type="text" name="username"
<?php echo (isset($_POST['username'])) ? "value = '".$_POST["username"]' : "value = ''"; ?> />

You have to wrap the variable result with quotes, so that the browser can know what's the content of the input.

Cristian
add a quote to your name and you'll end up with same problem :)
Col. Shrapnel
+1  A: 
<input type="text" name="username"
<?php echo (isset($_POST['username'])) ? ('value = "'.$_POST["username"].'"') : "value = \"\""; ?> />

Be aware of your quote usage.

meder
add a quote to your name and you'll end up with same problem :)
Col. Shrapnel
@Col Shrapnel - This was a very trivial question and I'm much too used to frameworks doing the work for me to bother advising that, but sure...
meder
Why not to bring an example with your favorite framework use then?
Col. Shrapnel
Because it'd be out of the scope of this simple question and just overcomplicate things.
meder
Well why bother to answer at all? If you fail to answer even this trivial question properly?
Col. Shrapnel
His problem was he wasn't wrapping double quotes around a string which had a space in it ( properly coding an attribute having a value in HTML ), the solution was to wrap double quotes around it so it was valid HTML, thus answering his question. You're overly being nitpicky about something that isn't so major. Thank you for the downvote.
meder
+5  A: 

Quote it. Otherwise the space will just become an attribute separator and everything after spaces will be seen as element attributes. Rightclick page in webbrowser and view source. It should not look like this (also see syntax highlight colors):

<input value=Big Ted>

but rather this

<input value="Big Ted">

Not to mention that this would still break when someone has a quote in his name (and your code is thus sensitive to XSS attacks). Use htmlspecialchars().

Kickoff example:

<input value="<?php echo (isset($_POST['username']) ? htmlspecialchars($_POST['username']) : ''); ?>">
BalusC
XSS has nothing to do here. Encoding required by standard. Even if you edit fully allower HTML in the admin area, you have to htmlencode values, including textarea content.
Col. Shrapnel
@Col: where did I said that XSS is related to the particular problem? :)
BalusC
ah yes I've overlooked it. Focused on the key word, you know :)
Col. Shrapnel
+1  A: 

As you see its not PHP5 or even PHP question at all.
Basic HTML knowledge is obligatory for one who want to be a PHP user.

And with using templates it looks way more neat:

Getting data part code:

$username = "";
if isset($_POST['username'])) $username = htmlspecialchars($_POST["username"]);

And template code:

<input type="text" name="username" value="<?=$username?>">

If you divide your code to 2 parts it become way more supportable and readable.

Col. Shrapnel
It's not a good practice to use short open tags since the server can have them disabled though, I would advise against recommending that to the OP.
meder
@meder well just turn it on. Not a big deal. That's what configuration settings are for.
Col. Shrapnel
Why would you? It's a bad practice to use it in the first place. That and I wouldn't ever use PHP if I managed the server.
meder
@meder who said it's "bad practice"?
Col. Shrapnel
@Col Shrapnel - Most experienced PHP developers agree it is bad practice. They're also being phased out in PHP6, are they not?. http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use
meder
@meder no, they are not being phased out in PHP6, it's empty rumor. Spreaded by these "most experienced PHP developers". I don't see one there though. ZF uses short tags in their template system if you prefer fat authority as a proof.
Col. Shrapnel