Jump to content

Using php variable in html select input forms name


firehawk777

Recommended Posts

Here's a question for all you great php programmers.

What I want to do is use a variable username as the name of a select input of a form.

I.E

echo"<select name='$user'>";

 

Of course this code doesn't work though I feel sure that there must be a way to do this.

Anyone know it? :confused:

So as to why its hard to explain but here goes.

I am basically embedding one form inside another.

First I call an array of users from the database and make a list that displays on the page.

I have a check box for each user for deleting the user in the list that is posted to an array.

Now I want to also have the option to be able to change the users level from the same list so I want to use the variable $user (username) as the name of each select input then check if that username is posted and update the level of the user from that.

 

If you still don't understand heres the complete code minus some of the css

function ViewUsers($theaction)
{
$query = "SELECT * FROM users";
$result=mysql_query($query) or die("Invalid Query : ".mysql_error());
$row=mysql_fetch_array($result);
$num=mysql_numrows($result); 
echo"<div class='post'>";
if ($num > 0){
echo "<h2>Users: $num</h2>";
}else{
echo "<h2>Users: No Users</h2>";
}

if(!mysql_num_rows($result)) 
        die("No users found.");   
$i=0;
echo"<ul>";
echo"<form name='levelform' method='post' action='$theaction'>";
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$user=mysql_result($result,$i,"user");
$email=mysql_result($result,$i,"email");
$rquery = "SELECT * FROM profiles WHERE pid='$id'";
$rresult=mysql_query($rquery) or die("Invalid Query : ".mysql_error());
$level=mysql_result($rresult,0,"level");

echo"<li>";
echo"<table border='0' width ='500'>";
echo"<tr>";
echo"<td width='100'>";
echo"<div id='meta' STYLE='color: #FF0000;'> User</div> </td><td  STYLE='color: #FF0000;'>$user";
echo"</td></tr>";
echo"<tr><td>";
echo"<div id='meta'>Email</div> </td><td>$email";
echo"</td>";
echo"</tr><tr><td>";
echo "<input type='checkbox' value='$id' name='userdelete[]' />"; 
echo "Delete:</td><td>$user";
echo"</td></tr>";
echo"<tr><td>";

echo"<select name='";
echo"$user";
echo"'>";
if ($level == 1){
echo"<option value='1' selected='selected'>1</option>";
}else{
echo"<option value='1'>1</option>";
}
if ($level == 2){
echo"<option value='2' selected='selected'>2</option>";
}else{
echo"<option value='2'>2</option>";
}
if ($level == 3){
echo"<option value='3' selected='selected'>3</option>";
}else{
echo"<option value='3'>3</option>";
}
if ($level == 4){
echo"<option value='4' selected='selected'>4</option>";
}else{
echo"<option value='4'>4</option>";
}
echo"</select>";


echo"</table>";
echo"</li>";
$i++;
}
echo"</ul>";
echo"<input type='hidden' name='level'value='level' />";
echo"<input type='submit' size='50' name='deleteuser' value='delete users' />";
echo "</form>";
echo"</div>";
}

 

So if you know how I make the $user variable the select name I would be very thankful!

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.