dilmerchant Posted June 10, 2011 Share Posted June 10, 2011 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. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted June 10, 2011 Share Posted June 10, 2011 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)? Quote Link to comment Share on other sites More sharing options...
dilmerchant Posted June 11, 2011 Author Share Posted June 11, 2011 Thanks for the reply, The value is supposed to be $row Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 11, 2011 Share Posted June 11, 2011 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. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted June 11, 2011 Share Posted June 11, 2011 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. Quote Link to comment Share on other sites More sharing options...
dilmerchant Posted June 13, 2011 Author Share Posted June 13, 2011 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? Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 13, 2011 Share Posted June 13, 2011 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>"; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.