0

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! :)

3
Contributors
10
Replies
11
Views
6 Years
Discussion Span
Last Post by elson.james
0

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.

Edited by diafol: n/a

0

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! :)

0

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.

0

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?

0

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.

0

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! :)

0

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.

Edited by Shanti C: n/a

0

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.

0

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>";
}

Edited by elson.james: I forgot to insert some codes

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.