James Burton
James Burton

Reputation: 94

HTML elements not being shown after using echo in PHP

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\"\n" .
"</div>";

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

Thanks in advance :)

Upvotes: 1

Views: 117

Answers (2)

Panda
Panda

Reputation: 6896

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\" />\n' .
'</div>';

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

Upvotes: 2

Shadow Wizzard
Shadow Wizzard

Reputation: 66398

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\" />\n" 

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

Upvotes: 4

Related Questions