checkboxes with $_GET

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

checkboxes with $_GET

Post by pedrotuga »

ok... first of all... can i send and get checkboxes values to/from $_GET just like $_POST or is there some issues?

i am using this code on the form

Code: Select all

<form name="form1" method="get" action="index.php">

<$php
querydb="select * from uploadsites";
$result=mysql_query($querydb);
while ( $array=mysql_fetch_array($result) ){
	echo "<input type=\"checkbox\" name=\"".array[]." value=\"".$array["id"]."\">". $array["name"].
	"<br>";
}
?>
</form>

but i am having troubles figuring out where to get the values from.
shouldnt they be in

Code: Select all

$_GET["array"];
?


btw, what's the easyest way to print an associative array?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

#1. simply using array() as the field name will not work. You'll have to give it a name and add [] after it. For example,

Code: Select all

<input type="checkbox" name="box[]">
If you passed that using $_GET, your url would look something like this

page.php?box=x&box=y&box=3&box=4&box=43

Depending on the amount of checkbox values you had.

The best way is to use $_POST.

I'm not sure what will happen if you print_r($_GET['box']); but you could test it out and see if you get your values into an array.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

Post by pedrotuga »

I had some errors using the quotes...

it was echoing somthin like

Code: Select all

<input type="checkbox" name=">


thx for the printinig tip... btw it outputs something like this
Array ( [0] => 2 [1] => 4 [2] => 5 )


thanks scott
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Yeah, you'll have errors using the quotes if you don't escape them properly. You would want to do something like this:

Code: Select all

echo "<input type=\"checkbox\" name=\"box[]\" value=\"".$array['id']."\">";
And since it is working apparently by your output.. you could assign a variable to the array.

Code: Select all

$checkboxvalues = $_GET['box'];
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Re: checkboxes with $_GET

Post by someberry »

pedrotuga wrote:ok... first of all... can i send and get checkboxes values to/from $_GET just like $_POST or is there some issues?
Don't forget that unchecked checkboxes will not get sent by the browser, which is very annoying for dynamically created checkbox names.
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

Re: checkboxes with $_GET

Post by pedrotuga »

someberry wrote:
pedrotuga wrote:ok... first of all... can i send and get checkboxes values to/from $_GET just like $_POST or is there some issues?
Don't forget that unchecked checkboxes will not get sent by the browser, which is very annoying for dynamically created checkbox names.
thats actually ok for me... i notice that and its cool..

this way i can loop the array containing the ids of the elements checked at once.
Post Reply