user3138025 user3138025 - 1 month ago 6
PHP Question

Trouble getting HTML Select data into POST.PHP

I have an input.php file with a form containing two types of lookup fields. One is populated from a series of

<option>
tags ($grade_1) and the other field is populated from a column in a database ($role_1). Both lookup fields display fine in the input.php form. I'm having trouble getting the user's selection based on database input ($role_1)into the post.php. I used ISSET as test code to see if I could get the count and data for $role_1. I can echo the Count ($nroles), but the echo of the data element ($role_1) doesn't display. The variable $grade_1 works fine.
Here's part of the input form:

<?php $link = new mysqli("localhost","USER","PASSWORD", "DATABASE");
if (mysqli_connect_errno())
{ printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if (!$link->set_charset("utf8"))
{ printf("Error loading character set utf8: %s\n", $link->error);
exit();
}
$role_sql = "SELECT role FROM lu_role";
$role_result2 = mysqli_query($link, $role_sql) or die (mysqli_error($link));
$options = "";
while($row2 = mysqli_fetch_array($role_result2))
{
$options = $options."<option value=''>$row2[0]</option>";
}
?>
<form action="post.php" method="post">
<table class="table_600_reg">
<tr>
<td width="120">
<select name="grade_1" style="background-color:#EFD381;">
<option value="0">Choose Grade</option>
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
<option value="4">Fourth</option>
<option value="5">Fifth</option>
<option value="6">Sixth</option>
<option value="7">Seventh</option>
<option value="8">Eighth</option>
</select>
</td>
<td width="200" align="left">
<select name="role_1" style="background-color:#EFD381;">
<?php echo $options;?>
</select>
</td>
</tr>
</table>
</form>


A snippet from the Post.PHP file is:

$link = new mysqli("localhost","USER","PASSWORD", "DATABASE");
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//* change character set to utf8 */
//Print an error if utf8 can not be loaded
if (!$link->set_charset("utf8"))
{
printf("Error loading character set utf8: %s\n", $link->error);
exit();
}
$role_1 = mysqli_real_escape_string($link, $_POST[role_1]);
$grade_1 = mysqli_real_escape_string($link, $_POST[grade_1]);
if(!isset($role_1))
{
echo("<p>You didn't select any role_1!</p>\n");
}
else
{
$nroles = count($role_1);
echo("<p>You selected $nroles roles: ");
for($i=0; $i < $nroles; $i++)
{
echo($role_1[$i] . " ");
}
echo("</p>");
exit;
}


Why can't I see the data from the $role_1 variable?
Thanks for looking at this.

Answer
$options = $options."<option value=''>$row2[0]</option>";

value is Empty String