Here's how I'm creating my form:

foreach ($all_rows as $column)
	{
		print ("<tr>");
		print ("<td>". $column['name'] . "</td>");
		print ("<td><input type='text' name='form1[".$column['id']."][materialsPrice]' id='".$column['id']."' value='".$column['materialsPrice']."'/>
		<input type='hidden' name='form1[".$column['id']."' value='"."' /></td>");
		print ("<td><input type='text' name='form1[".$column['id']."][labourPrice]' id='".$column['id']."' value='".$column['labourPrice']."'/>
		<input type='hidden' name='form1[".$column['id']."' value='"."' /></td>");

		print("<td><input type = 'checkbox' name = 'form1[".$column['id']."][quantityCheckbox]' id = '".$column['id']."' checked = '".$column['quantityCheckbox']."' value = 'quantityCheckbox'/>
		<input type='hidden' name='form1[".$column['id']."' value='"."' /></td>");
		print("<td><input type = 'checkbox' name = 'form1[".$column['id']."][widthCheckbox]' id = '".$column['id']."' checked = '".$column['widthCheckbox']."' value = '".$column['widthCheckbox']."'/>");
		print("<td><input type = 'checkbox' name = 'form1[".$column['id']."][lengthCheckbox]' id = '".$column['id']."' checked = '".$column['lengthCheckbox']."' value = '".$column['lengthCheckbox']."'/>");
		print("<td><input type = 'checkbox' name = 'form1[".$column['id']."][heightCheckbox]' id = '".$column['id']."' checked = '".$column['heightCheckbox']."' value = '".$column['heightCheckbox']."' /> ");

		print ("<td><input type='text' name='form1[".$column['id']."][weight]' id='".$column['id']."' value='".$column['weight']."'/>
		<input type='hidden' name='form1[".$column['id']."' value='"."' /></td>");
		print ("<td><input type='text' name='form1[".$column['id']."][sku]' id='".$column['id']."' value='".$column['sku']."'/>
		<input type='hidden' name='form1[".$column['id']."' value='"."' /></td>");
		print ("<td><input type='checkbox' name='form1[".$column['id']."][delete]' id='".$column['id']."' value='delete'/>
		<input type='hidden' name='form1[".$column['id']."' value='"."' /></td>");
		print ("</tr>");

		$count2 += 1;
	}
?>

And here's how I'm checking all the text boxes currently and it works. I just added the checkboxes though and I have no idea how to work with those.

if (isset($_POST['saveChanges']))
	{	
		$form = $_POST['form1'];

		$query3 = "DELETE from options where id = ";
		$deleteCount = 0;
		$deleteRows = false;

		// Santize $form, make sure its an array etc..

		// Loop array
		foreach($form as $rowid => $columnarray)
		{
		   	// set query start
		   	$query = "UPDATE options SET ";
			$query2 = "INSERT into options (name, materialsPrice, labourPrice, weight, sku) VALUES ('";

			$count = 0;
		   
		   	// Loop each column for this row
		   	foreach($columnarray as $column => $value)
			{
				if ($value == 'delete')
				{
					$deleteRows = true;

					if ($deleteCount == 0)
					{
						$query3 .= $rowid;
						$deleteCount += 1;
					}
					else
					{
						$query3 .= " or id = " . $rowid;
					}
				}
				else
				{
					if ($count == 0)
					{
						if ($value == '')
						{
							$query2 .= "0'";
						}
						else
						{
							$query2 .= $value . "'";
						}
				      		// Add set phrase to query
				      		$query .= $column."='".$value."' ";
						$count = $count + 1;
					}
					else
					{
						if ($value == '')
						{
							$query2 .= ", 0";
						}
						else
						{
							$query2 .= ", " . $value;
						}
						// Add set phrase to query
				      		$query .= ", " . $column."='".$value."' ";
					}
				}
			}

		   	// End query:
		   	$query .= "WHERE id='".$rowid."'";
			$query2 .= ")";

			$checkForRow = @mysql_query("select * from options where id = '".$rowid."'");
			if(mysql_num_rows($checkForRow) > 0)
			{
				$updateTable = $db->query($query);
			}
			else
			{
				$insertValues = $db->query($query2);
			}
		}

		if ($deleteRows)
		{
			$deleteRowsResult = $db->query($query3);
		}
	}

The textboxes work, checkboxes don't. What I'd like is for it to enter the checked status of the checkboxes into the database and then load that status next time the page is loaded. So basically it saves the checkbox state in the database I already have set up. Right now it's just saving a 0 value in the BOOL type table whether it's checked or not. I know I have to check if the name value is set or not but because it's dynamic and is a 2 dimensional array, I'm lost here. I attached a screenshot of the page.

Recommended Answers

All 2 Replies

Can't help with the whole issue but I did notice an error on line 10 of your html syntax, for value= you are missing the .$column.

If you want single check box out of the multiple check boxes to be checked, you may make all check boxes name as same name. You will get the status.

If you want multiple check boxes to be selected you may name the check boxes as name=chk[1] name=chk[2] like this

Hope this will help.

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.