Jump to content

Send an array as value of checkbox


dilmerchant

Recommended Posts

Hello All,

    I am a newbie and have been trying to send a complete array as a checkbox value, but cannot do so.

The problem :

I have a form which has multiple rows. These rows are displayed from gathering data from mysql database tables.

Below is the code I am trying.

while($row = mysql_fetch_array($result))
		{		
		    
		    $id = $row['id'];
		    $queryName = "SELECT name FROM users";
			$resultQueryName = mysql_query($queryName) or die('Error, query failed');	
			echo "<tr class='alt' align='center'>";
			echo "<td><input type='checkbox' name='chkbox[]' value='".$row."'/></td>";

 

After submitting the form, I tried checking for the values and retrieving them

foreach($_POST['chkbox'] as $value)
{
    echo $value;
}

 

$value shows an array, but if I try applying the foreach loop on $value, then it gives an error.

Link to comment
Share on other sites

One issue is that you're setting the checkbox values to $row which is an array.

 

<?php

echo "<td><input type='checkbox' name='chkbox[]' value='".$row."'/></td>";

?>

 

 

I'm not sure what the checkbox value should be, since the code is a little confusing. Is it supposed to be set to a field from the first query array ($row) or the second one ($resultQueryName)?

Link to comment
Share on other sites

What you are trying to do makes no sense. There is no reason to pass the entire array as the checkbox value. One of the reasons for using a database is that you can use references to the primary key for the database records. Set the value of the checkboxes to the ID of the records not the entirety of the records themselves.

 

Also, you should never be running queries in loops as you show above.

Link to comment
Share on other sites

what he means is that you are trying to set the html value attribute of an input tag to a PHP array. This is impossible. What happens is PHP tries to convert your array into a string (as you are using it like a string in that context). WHen you convert an array into a string, it always becomes the string 'Array' regardless of the size or contents of the array. Lets take a simpler example

$array = array("a", "bunch", "of", "values");
echo $array;
/****
Output is: Array
****/

 

 

What you want to do is provide the column you want as the key. So for example, if the column you want from $row is called 'column', you need to do

echo "<td><input type='checkbox' name='chkbox[]' value='".$row['column']."'/></td>";

 

also, each $value in each iteration of

foreach($_POST['chkbox'] as $value)
{
    echo $value;
}

 

is NOT an array. It is a single value.

 

Link to comment
Share on other sites

Thanks for the replies,

 

Maybe I will get a better solution if I elaborate the problem a little more.

 

So here goes....

 

The form has 4 columns :

An application ID, UploadedBy, Date and Time.

 

The application ID is simply printed as per value from the database.

The UploadedBy field is a selection box. The first selection is displayed as per the value in the Database.

Same is the case with Date and Time.

 

Since these values are retrieved from the database, I have a while loop

 

while($row = mysql_fetch_array($result))
{		
       $app_id = $row['app_id'];
       $queryName = "SELECT name FROM users";
       $resultQueryName = mysql_query($queryName) or die('Error, query failed');	
	echo "<tr class='alt' align='center'>";
	echo "<td><input type='checkbox' name='periods[]' value='".$row."'/></td>";
	echo "<td>";
	echo $row['app_id'];
                echo "</td>";
        }

 

There is a button which has to be clicked to submit all the values if they are changed, only if checkbox is checked.

 

The values have to be retrieved in the next page. So each row is stored in an array.

Is this possible in any way?

Link to comment
Share on other sites

1. Do not run queries in loops. You don't need to run the query to get the list of names multiple times - it will be the same data. just query it once before you run that loop and then use the data.

 

2. You don't show how you are creating the select field. I provided some code below that shows a simple method of creating the select options using the single query of name values.

 

3. In this type of scenario I would a) name the checkboxes as you have them - an unindexed array with the uniqu ID as the value. b) name the corresponding fields as arrays BUT give them a named index using the unique ID. Then when you process the POST results, just check the list of IDs passed in the checkbox array. Then for each ID passed in the checkbox array reference the corresponding fields.

 

Sample replacement code

    //Function to create select options
    function createOptions($values, $selected)
    {
        foreach($values as $value)
        {
            $selected = ($value==$selected) ? ' selected="selected"' : '';
            echo "<option value='{$value}'{$selected}>{$value}</option>\n";
        }
    }

    //Run query to get names ONE TIME
    $queryNames = "SELECT name FROM users";
    $resultNames = mysql_query($queryNames) or die(mysql_error());
    $names = array();
    while($row = mysql_fetch_assoc($resultName))
    {
        $names[] = $row['name'];
    }
    
while($row = mysql_fetch_array($result))
{		
         $app_id = $row['app_id'];
	  echo "<tr class='alt' align='center'>";
	  echo "<td><input type='checkbox' name='periods[]' value='{$app_id}'/></td>";
	  echo "<td>{$app_id}</td>";
	  echo "<select name=\"UploadedBy[{$app_id}]\">";
	  echo createOptions($names, $row['UploadedBy']);
	  echo "</select>";
    }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.