Jump to content

Recommended Posts

Hello....

 

I have a form with many check boxes.

I want to make a variable that includes all the info from just the selected ones.

 

so if my check boxes returns values 1 and 2 and 3

 

i want my variable to have the value 1, 2, 3.

 

The check boxes are for checking what fields in the database to include in a group. and my variable is supposed to be the one that chooses the fields with mysql command: SELECT $variable from $dbtable

 

Thank you for answers! :)

<input type="checkbox" name="chkbx[]" value="1" />
<input type="checkbox" name="chkbx[]" value="2" />
<input type="checkbox" name="chkbx[]" value="3" />
<input type="checkbox" name="chkbx[]" value="4" />
<input type="checkbox" name="chkbx[]" value="5" />

 

$checked = implode(', ',$_POST['chkbx']);

Thank you for the reply.

 

But i cant get it to work....

I get the error:

Warning: implode() [function.implode]: Invalid arguments passed in my folder and line

 

My code looks like this(cutted out the rest of the form and irrelevant PHP):

 

$list_include = implode(', ',$_POST['checkbox']);				
echo $list_include;

<form name="form1" method="post" action="<?php echo $PHP_SELF;?>">
<?php

		include("conn/conn_list.php");

		$query = "SHOW COLUMNS FROM $dbtable";
		$result = mysql_query($query) or die(mysql_error());

		while($row = mysql_fetch_array($result))
		{

		echo 	"<label>
          			<input type='checkbox' name='checkbox' value='". $row['0'] ."' id='". $row['0'] ."'>
          			". $row['0'] ."</label>
        			<br>";

		}
	?>
</form>

Yea.. i figured that out.. And have solved the problem with an if that checks if the form is submitted.

 

Thanks....:)

 

Have a new issue though.

After i have imploded the values to a one variable i want to explode them to separate variables again.

Got the explode to work and get the correct output.

 

But by the output of the explode i want to include different form fields.

like:

$list_include = explode(', ',$row['list_include']);
if($list_include[0] = 'product'){
include("source/form_product.php");
}

 

And so on... The number of arrays in $list_include can maximum be 28, but i expect it to extend...

How can i in a easy way use all of the different arrays and include a form by the output?

 

My intention with the whole function from the beginning is to make a group where the products are a member of. The group selects what fields in the database to use. And now I'm at the part where want to put out just the fields that is selected by the group. I get the fields out in the array $list_include.

 

Thanks again

 

 

first of all, in the case, if you are trying to store an array of data into a table, i would use serialize() and unserialize()....that way the values could have a comma in the them and it wouldn't break the code

 

on to your code. first, you want a double equals in your IF statement, not a single one. next, the includes would depend on what you are trying to do. is there an include for each 'keyword'? if so, just keep an array mapping the keywords to the includes, then loop over the $list_include and do the include. or, if it's always form_<keyword>.php you could just do:

$list_include = explode(', ',$row['list_include']);
foreach($list_inlclude as $include){
  include("source/form_{$include}.php");
}

 

if it's only for certain keywords, you can just test for those with an IF and in_array()

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.