Jump to content

[SOLVED] Send Multiple Values Using Checkboxes With Same Name?


limitphp

Recommended Posts

I have a bunch of checkboxes in a form.

They are all named genre.

 

When I submit the form, and try to catch the value of the checkboxes on the other side with:

$genres = $_POST['genre'];

 

It only gets the value of the very last checked checkbox.

In other words, if 2 checkboxes are checked, it will only get the value of the last checked one.

 

 

 

Link to comment
Share on other sites

change names of your checkboxes to 'genre[]'

 

Is there another way?

 

Thats the way I had it and it does work that way, but it messes up my javascript.

 

function checkRockGenres(){
var num_genres = document.frmGenres.genre.length;
var unchecked_flag=0;

if (document.frmGenres.allrock.checked){		
	document.frmGenres.genre[0].checked=true;
	document.frmGenres.genre[1].checked=true;					
	document.frmGenres.genre[2].checked=true;
	document.frmGenres.genre[3].checked=true;
	//CHECK TO SEE IF ALL GENRES ARE CHECKED
	for (var i=0; i <= num_genres-1; i++)
	{
		if (document.frmGenres.genre[i].checked==false)
			unchecked_flag = 1;		
	}
	if (unchecked_flag==1)
		document.frmGenres.allGenres.checked=false;
	else
		document.frmGenres.allGenres.checked=true;
}
else
{					
	document.frmGenres.allGenres.checked=false;
	document.frmGenres.genre[0].checked=false;
	document.frmGenres.genre[1].checked=false;					
	document.frmGenres.genre[2].checked=false;
	document.frmGenres.genre[3].checked=false;
}
}

 

Even when i change the line to:

var num_genres = document.frmGenres.genre[].length;

 

it still doesn't work.

Link to comment
Share on other sites

<TR>
					<TD ALIGN="center" COLSPAN="2" STYLE="padding-top:15px;">											
				<?php if($allRock==1){		
						echo "<input type='checkbox' name='allrock' value='1' onclick='javascript:checkRockGenres()' CHECKED />";}
					else{
						echo "<input type='checkbox' name='allrock' value='1' onclick='javascript:checkRockGenres()' />";}
				?>
						<FONT CLASS="genres">All Rock 'n' Roll</FONT>
					</TD>
				</TR>
				<TR>
					<TD COLSPAN="2">
						<FONT CLASS="small-genre">
				<?php if(strstr($genres, 'Alternatve')) { ?>		
						<input type="checkbox" name="genre" value="1" onclick='set_major_genres()' CHECKED />
				<?php }else{ ?>
						<input type="checkbox" name="genre" value="1" onclick='set_major_genres()' />
				<?php } ?>
						Alternatve  										
				<?php if(strstr($genres, 'Rock')) { ?>		
						<input type="checkbox" name="genre" value="2" onclick='set_major_genres()' CHECKED />
				<?php }else{ ?>
						<input type="checkbox" name="genre" value="2" onclick='set_major_genres()' />
				<?php } ?>
						Rock  										
				<?php if(strstr($genres, 'Punk')) { ?>		
						<input type="checkbox" name="genre" value="3" onclick='set_major_genres()' CHECKED />
				<?php }else{ ?>
						<input type="checkbox" name="genre" value="3" onclick='set_major_genres()' />
				<?php } ?>
						Punk  										
				<?php if(strstr($genres, 'Indie')) { ?>		
						<input type="checkbox" name="genre" value="4" onclick='set_major_genres()' CHECKED />
				<?php }else{ ?>
						<input type="checkbox" name="genre" value="4" onclick='set_major_genres()' />
				<?php } ?>
						Indie
						</FONT>
					</TD>

 

It continues all the way down to name="genre" value="17"

Ignore all the

if(strstr($genres, 'Alternatve')

I need to rework that part.

 

The problem with using id instead of name is this will not work:

var num_genres = document.frmGenres.genre.length;

Right?  That .length can only be used on form objects?

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.