Jump to content

Creating and storing array of check boxs


snookian
Go to solution Solved by snookian,

Recommended Posts

Hi everyone, I have an issue posting an array from check boxs, simply put i have a database that contains some details, i want to take these details, split them into an array and therefore separate check boxs then a user will select a number of check box and then this will be saved as an array into a seperate table, hope i explained that ok.

So here is the code I have some far, I have some code elsewhere for the $result part, and for the submit part, but basically its grabs the information from a table and splits it into check boxs (in theory) the "label" part works, i have this here to check that its is getting the right information and it is, the problem happens when I submit the form, it will only save the last checked check box, so if i click say check box 1,3,5 (there could be an unlimited amount of options which is why im pulling the data from another table) and these values are One, Three, Five only Five will save in the table.
 

<?php foreach ( $results['detailsline'] as $detailsline ){
$invoice_details = $detailsline->details_line;
echo '<label>'.$invoice_details.'</label><input type="checkbox" name="invoice_details" value="'.$invoice_details.'"/>';   
 }                  
?>

 



If I have explained this well enough can someone guide me please.

Ian

Link to comment
Share on other sites

You need to put the checkboxes into an array, like this:

 

name="invoice_details[]"

 

Then when the form is posted, check the posted check boxes and save the data

 

 

I have tried this but it just saves as "Array" in my database.  :(

 

Ian

Link to comment
Share on other sites

The submitted values are an array. You have saved the array directly to the database, but databases (or at least MySQL, can't speak for others) can't store PHP arrays, they can only store strings. So you are going to have to parse that array, and turn it into a string.

 

You can use serialize() if you want. But then the indivudual values will not be searchable, which isn't the best database practice.

Edited by haku
Link to comment
Share on other sites

I really appreciate everyones help, but im still struggling, PaulRyan I have done what you said, and used Var_dump like this

 

var_dump($invoice_details[2]);	

To check that the value in that slot is what i want, and it is, but i having major problems storing to the database, i understand what haku is getting at about converting the array to a string before input but I have no idea where/how to do this.

 

Ian

Link to comment
Share on other sites

I have changed my code to the following

 

		<?php
			$i=0;
			foreach ( $results['detailsline'] as $detailsline ){
				$invoice_details[] = $detailsline->details_line;
						
				echo $invoice_details[$i];
			 	echo '<input type="checkbox" name="invoice_details[$i]" value="'.$invoice_details.'"/>';  
				$i++;
			  }		
				var_dump($invoice_details[4]);		
		?> 

This works well creating the layout (echo or each choice) and the vardump bit reads the values it should. I have tried using implode to create a string like below, but as i said and last post im at a loss if this is right ot where to put it. I think my naming conventions may be confussing me too, the field in the databse where this value is stored is called invoice_details, but as you can see i have used it for most things relating.

 

$idetails[] = implode(", ", $invoice_details);

 

Ian

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.