Pokemon Pro Pokemon Pro - 3 months ago 17
HTML Question

Pass PHP variable value into HTML label

Html Code

<ul><li><label class="control-label"><?php echo $name; ?></label></li></ul>


Php

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = "pizza";

$name = "";

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);


$email = "example@gmail.com";
$query = "Select customer.CustName from customer inner join login on customer.CustID = login.CustID where login.email = '$email'";
$result = mysql_query($query);
$num = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {
$name = $row["CustName"] ;
}

mysql_close($conn);?>


I have tried to echo the variable $name into the label, but the variable value didn't appear at the label. Please help me for this problem. The output i want is display the value "Example" into the label.

Error code : Notice: Undefined variable: name

Answer

<label> tags do not have a value attribute. The label text goes in between an opening and closing </label> tag.

<label class="control-label"><?php echo $name; ?></label>

See the MDN page for <label> to learn more about this tag.