1

I know I should be using htmlentities for all my form text input fields but this doesn't work:

<?php
echo "<tr>
        <td align=\"right\">".Telephone." :</td>    
        <td><input type=\"text\" name=\"telephone\" size=\"27\"
            value=\"htmlentities($row[telephone])\"> Inc. dialing codes
        </td>    
</tr>";
?>

It simply shows the input value as "htmlentities(0123456789)" in the form? What have I done wrong please?

1
  • You should use htmlspecialchars, not htmlentities. of course as long as you are implementing utf-8. Commented Oct 17, 2008 at 14:59

7 Answers 7

5

try using

value=\"" . htmlentities($row[telephone]) . "\"

there. Currently, your string simply contains the htmlentities string and splices the variable in. You need to get out the string, call the function and put it's result in place, as above.

Sign up to request clarification or add additional context in comments.

3 Comments

+ is not the concat operator in PHP, it's .
should be: value=\"" . htmlentities($row[telephone]) . "\"
changed... it's been a while since I've used actual PHP :)
3

You can't call a function in the middle of a string. You need to get the return value from the function call and then include that in the string.

However...

<tr>
    <td align="right">
        <label for="telephone">Telephone:</label>
    </td>    
    <td>
        <input type="text" 
               name="telephone" 
               id="telephone"
               size="27" 
               value="<?php 
                   echo htmlentities($row[telephone]); 
               ?>"> 
        Inc. dialing codes 
    </td>
</tr>

... would be cleaner.

As would getting rid of the deprecated presentational markup and use of tables for layout.

Comments

1

This will work:

<?php
echo "    <tr>
                    <td align=\"right\">Telephone :</td>    
                    <td><input type=\"text\" name=\"telephone\" size=\"27\" value=\"".htmlentities($row[telephone])."\"> Inc. dialing codes</td>    
            </tr>";
?>

BTW, I also corrected some very strange syntax you have going on here, like where you concatenate the constant "Telephone", which really should be inside the string. These kinds of details are important and will break your code easily.

Also, I suggest using single quotes, instead of double, around a string like this so that you don't have to escape all of the double quotes inside the string.

Comments

1

@workmad3: that won't work as he's doing PHP.

<?php echo '<tr>
                <td align="right">' . Telephone . ' :</td>    
                <td><input type="text" name="telephone" size="27" value="' . htmlentities($row[telephone]) . '" /> Inc. dialing codes</td>    
        </tr>';

Comments

1

if you're just looking for making-your-output-safe-in-hml; You should use htmlspecialchars() instead, since its 'only' an telephone number.

htmlspecialchars($row[telephone], ENT_QUOTES);

htmlentities() is a bit slower and not as good with multibyte characters. But I'm guessing you're not getting to those problems just jet.

1 Comment

I agree only htmlspecialchars is needed. of course i hope you are implementing utf-8 too.
1

If you want to combine a large section of HTML and PHP variables there are two things you can do.

One, use a HEREDOC construction.

$txt = <<<HERETEXT
Put your HTML here.
HERETEXT;

echo $txt;

Second, use a first class variable to name a function, then use that in the HEREDOC.

$he = 'htmlentities';

$txt = <<<HERETEXT
{$he($string, ENT_QUOTES, 'UTF-8')}
HERETEXT;

echo $txt;

However, HTML should not be handled in very large chunks, owing to the increase risk of nasty errors. Also, you might repeat yourself needlessly.

Comments

0

First of all, don't echo your HTML in a string. Separate code from markup.

<tr>
    <td align="right">Telephone :</td>
    <td><input type="text" name="telephone" size="27"
        value="<?php echo htmlentities($row['telephone']); ?>"> Inc. dialing codes</td>
</tr>

3 Comments

His HTML is inside a PHP string, so the tags won't work. Also, as an aside, using short tags ("<?= ?>") is a bad idea, since they have to be enabled in php.ini, which you may not have access to.
Right, I removed short tags. Should not be putting markup in a string though, made note of that.
Of course his HTML inside of PHP tags will work because he has an echo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.