在PHP中使用echo后未显示HTML元素

在PHP中使用echo后未显示HTML元素

问题描述:

I am creating a form to update a database and used PHP to query the database and get current values before filling them in as the values already in the input boxes. I used echo to create the HTML elements while inserting the PHP variables however, the elements created aren't being displayed. Here are a couple of the inputs that I tried to make:

// Album Price
echo "<div class=\"row horizontal-center\">" .
"<input type=\"number\" step=\"0.01\" value=\"" . htmlspecialchars($albumPrice) . "\" name=\"albumPrice\"
" .
"</div>";

// Album Genre
echo "<div class=\"row horizontal-center\">" . 
"<input type=\"text\" value=\"" . htmlspecialchars($albumGenre) . "\" name=\"albumGenre\"
" .
"</div>";

Thanks in advance :)

Your input elements are not properly closed with a > character. This code should work:

"<input type=\"number\" step=\"0.01\" value=\"" . htmlspecialchars($albumPrice) . "\" name=\"albumPrice\" />
" 

Note the /> added in the end of the line. (the / due to <input> not having a closing tag)

There's conflicting quotes in your HTML elements. Use single quotes for echo() and double quotes for HTML elements.

Also, you did not close the <input tag.

echo '<div class=\"row horizontal-center\">' .
'<input type=\"number\" step=\"0.01\" value=\"' . htmlspecialchars($albumPrice) . '\" name=\"albumPrice\" />
' .
'</div>';

echo '<div class=\"row horizontal-center\">' . 
'<input type=\"text\" value=\"' . htmlspecialchars($albumGenre) . '\" name=\"albumGenre\" />
' .
'</div>';

Tip: You should turn on Error Reporting by adding this code to the top of your PHP files which will assist you in finding errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);