user1727110
user1727110

Reputation: 23

Adding custom PHP source into HTML

Need some help to add PHP into HTML. I am trying to embed PHP code snippet in HTML however output is showing the snippet itself in the text box instead of evaluating it.

In HTML (register.php), added the below lines of code

<tr>
    <td>
        <?php utils::addControl('Name','label'); ?>
    </td>
    <td>
        <?php utils::addControl('username','text'); ?>
    </td>
</tr>

Here is the source of utils::addControl()

    public function addControl($ctlName, $type) {

    switch ($type) {
        case 'text':
            echo <<<EOT
            <input type='text' name='$ctlName' value='&lt?php echo htmlspecialchars($ctlName); ?&gt'>
   EOT;
            break;
        case 'label':
            echo "<label for id='lbl$ctlName'>$ctlName</label>";
            break;
    }

Output is showing <?php echo htmlspecialchars(username); ?> in the text box.

What should be done in order to get the PHP snippet evaluated properly. TIA.

Upvotes: 2

Views: 154

Answers (1)

Quentin
Quentin

Reputation: 943579

Servers are typically only configured for a single pass over a script while looking for PHP…

Source → PHP → Output

Not a double pass:

Source → PHP → PHP → Output

If it did perform a double pass then you would have to jump through hoops if you wanted to include the sequence <?php in the output (or even <? if short tags were turned on).

Write your PHP so it generates that output you want in the first place, don't try to generate PHP from PHP.

$htmlCtrlName = htmlspecialchars($ctlName);
echo "<input type='text' name='$htmlCtlName' value='$htmlCtrlName'>"

Upvotes: 2

Related Questions