Hi everyone! I am new in PHP.

Is there a way to store a value from a combo box to an array?

Can you show me an example.

Thanks! :)

Member Avatar for diafol

Can't really get what you mean - store dropdown value to an array (via $_POST or $_GET) or create values from an array (php loop)? The first is a little unusual, the second a more obvious request.

Can't really get what you mean - store dropdown value to an array (via $_POST or $_GET) or create values from an array (php loop)? The first is a little unusual, the second a more obvious request.

Example I have this code:

<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>

<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>

I have this two combobox and on the 1st combobox, if the user selects Volvo,the value that has been selected will be stored on an array that has a variable name $cars, and on the second combobox, if the user select Audi, the value that has been selected will then be stored to $cars. That's what I meant.

Thank you for your prompt reply! :)

Member Avatar for diafol

Ah, OK, you want to produce an array for dropdowns:

$cars = array($_POST['dd1'], $_POST['dd2']);

However, I would usually sanitize the post inputs before placing them in the array. This requires that you have name="dd1" and name="dd2" as attibutes in the respective select tags.

If you are producing a large number of identical dropdowns, could this be simplified with a list of checkboxes instead? Just a thought.

Ah, OK, you want to produce an array for dropdowns:

$cars = array($_POST['dd1'], $_POST['dd2']);

However, I would usually sanitize the post inputs before placing them in the array. This requires that you have name="dd1" and name="dd2" as attibutes in the respective select tags.

If you are producing a large number of identical dropdowns, could this be simplified with a list of checkboxes instead? Just a thought.

Actually this is my code, I have two PHP files. Let me show you.
member.php

$strSQL = mysql_query("SELECT * FROM member");
  while($row = mysql_fetch_array($strSQL)){

$data .="<td>";
        $data .= "<select name='access[]'>";
        
        $data .="<option selected value='".$row['access']."'>".$row['access']."</option>";
        if($row['access'] != 'member'){
                $data .="<option value='member'>member</option>";
        }
        else{
                $data .="<option value='admin'>admin</option>";
        }

$data .="</select>";
$data .="</td>";
$data .= "</tr>";       
}

As you can see here, I have a database. It has a member table that has a name & access fields.
This is my view.php

foreach ($_POST['access'] as $value){
echo "Access: ".$value;
}

I'm getting too many values in the $_POST even if I only put just one value. Its seems I'm getting all the values in the access field in my database.
What's wrong with this?

I'm getting too many values in the $_POST even if I only put just one value. Its seems I'm getting all the values in the access field in my database.

yes, because you have written your combo box code in while loop, that's why you are getting all the values in the access files from your database.

Please clearly tell what is your required output from your code.

Hi there,
Thank you for your response!

Since, I cannot attached an image let me create a link. http://img25.imageshack.us/img25/2839/picrb.png
User's is allowed to change the access status via the combo box and it will be process by clicking on the Update Button. And if the status will be changed, it should be put to an array.
Example: Elson's Access is an admin, I want to change it as a Member, The member value will now be stored into an array. Next is I want to change Juan's Access into Admin, the Admin will now again be stored in the array.

That's what I want to accomplish.

Thank you for your patience! :)

try to print this $row , whether it is correctly printed or not.
and try this and tel me it is working or not:

$strSQL = mysql_query("SELECT * FROM member");
while($row = mysql_fetch_array($strSQL))
{
	$data .="<td>";
    $data .= "<select name='access[]'>";
    $data .="<option value='member'";
    if($row['access'] == 'member')
    $data.=" selected";
    $data.=">member</option>";
    $data .="<option value='admin'";
    if($row['access'] == 'admin')
    $data.=" selected";
    $data.=">admin</option>";

$data .="</select>";
$data .="</td>";
$data .= "</tr>";       
}

and also once check your update query is updating correctly or not in your data base table.

Hi! Thank you for your quick response.

It won't store the correct value. when I change the access of the member on the 3rd row, it won't print the correct value. It also gives me a wrong size of array.

post your entire code

This is my member.php

mysql_select_db("members", $con);



$strSQL = mysql_query("SELECT * FROM member");

$data = "<form method='POST' action='view.php'>";
$data .= "<input type='submit' name='method' value='update' />";

 $data .= "<table width='100%' class='datagrid'>";
  $data .= "<thead>";
  $data .= "<tr>";
  $data .= "<td width=5%>";
  $data .= "<input type='checkbox' name='mark' id='mark' />";
  $data .= "</td>";
  $data .= "<td>";
  $data .= "Name";
  $data .= "</td>";
  
  $data .= "<td>";
  $data .= "Access";
  $data .= "</td>";
  
  $data .= "<td>";
  $data .= "Membership Request";
  $data .= "</td>";
 
  $data .= "</tr>";
  $data .= "</thead>";
  $data .= "<tbody>";
  
  while($row = mysql_fetch_array($strSQL)){
		$data .= "<tr>";
		$data .= "<td>";
    $data .= "<input type='checkbox' class='memberchk' name='membersID[]' value='".$row['member_id']."' />";
    $data .= "</td>";
	$data .="<td>";
	$data .= "".$row['name'];
	$data .="</td>";
	
	$data .="<td>";
	$data .= "<select name='access[]'>";
	
	$data .="<option value='member'";
	if($row['access']=='member')
	$data .="selected";
	$data .=">member</option>";
	$data .="<option value='admin'";
	if($row['access']=='admin')
	$data .="selected";
	$data .=">admin</option>";
$data .="</select>";
	$data .="</td>";
$data .="</tr>";
}
$data .="</tbody>";
$data .="</table>";
$data .="</form>";

This is my update.php

$member_id = $_POST['membersID'];
$access = $_POST['access'];
$size_id = sizeof($member_id);
$size_access = sizeof($access);
echo "Size:".sizeof($size_access)."</br>";
for($i =0; $i<$size_id; $i++){
echo "Access: ".$access[$i]."</br>";
echo "ID: ".$member_id[$i]."</br>";
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.