views:

230

answers:

4

Is there a way to take multiple check boxes and have the related id/name automatically populate in an un-editable text box/text field?

As per one of the suggestions...I am expanding and clarifying.

I have several checkboxes that I want saved into the SAME db field...at the same time. So, since I'm a novice programmer where php/sql (and advanced languages) are concerned, I figured the easiest and quickest way is to have the check box ID's inserted into a single text box then take that dynamic info from the text box into the db field, since this information has to correspond w/ other textboxes on a single transaction. So:

Name: textbox
ID#: textbox
Other Info: textbox
Check1: X
Check2: X
Check3:

and the db would all have the lines there...help...I'm confused.

A: 

Sure. You could use jQuery.each to loop through the checkboxes, and add the id/nme to the textbox/field.

Steven
Not sure if the OP is after a js or php solution - question could be a bit more descriptive!
adam
A: 

If you have a form with multiple checkbox items:

<input type="checkbox" name="people[]" value="adam" />
<input type="checkbox" name="people[]" value="nick" />
<input type="checkbox" name="people[]" value="simon" />

Adding the [] after the name puts all the values in a php array. You can then access the data like so:

$people = $_POST['people'];
print_r($people); // prints array('adam','nick','simon');

The array values will only be present if the corresponding checkbox has been checked.

adam
I didn't know PHP could receive arrays like that! You would also obviously want to validate any data that you get from the $_POST array for XSS or SQL attacks before displaying or storing it.
Andrew
Tried this way, and it refuses to input the checkbox data into the text field...[code]<input name="Extrasfield" type="text" id="extras" value="'.print_r($Extras).'" disabled>[/code]It displays "1" and only inputs "Array" into the database field.
A: 

As the answer above suggests, use JQuery. Here is some usable code,

$("#id :checkbox").click(function() { 

    var values = [];

    $('#id :checked').each(function() {
        values.push($(this).val());
    });

    $("#id").val(values)
});

Good luck!

James
how would I add this into an existing php code? And where would the portion be to add it into the textbox? I like the code (short and sweet) but how do I get that INTO the textbox? Then, provided that I code it right, would those values in the textbox be able to be added into the database field as a single line? To prevent additional codeing in the textbox...I've added "disabled" at the end.
A: 

Can't comment here yet, but as a reply to your comment "Tried this way, and it...", aaron, you're misusing the print_r command. print_r is for dumping the data/structure of array variables in text format, you can NOT dump this text into the value="" section of an html input form. What you're really accomplishing by doing this will end up looking like this in the generated html:

<input name="Extrasfield" type="text" id="extras" value='Array
(
    0 => 'adam',
    1 => 'nick',
    2 => 'simon'
 )' disabled>

If you want the textbox to contain something like "adam, nick, simon", you'll have to replace the print_r with something like this:

 implode(', ', $Extras)

where the ', ' bit is the seperator you want to use.

Marc B
Okay...so I kept the Arrays in place, added 2 $ codes:`$Extras = $_POST['Extras'];$Extra = implode(', ', $Extras);`Then I added the $Extra into the db and it worked beautifully.Thank you marc b.