$_POST and checkbox

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

frANZi
Forum Newbie
Posts: 12
Joined: Mon Mar 21, 2005 1:52 pm
Location: Croatia

$_POST and checkbox

Post by frANZi »

People...

On my site I have about 20 checkboxes. On every that checkbox is a name and the checked value is 1. Now, when I submit, I want PHP to print strings of checkboxes (not names) that I have activated.

I don't want that to solve by arrays.

I had that problem also with listboxes, and I solved this by databases. I want to solve the checkboxes also by databases but I don't know how...

Please, help...

Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

¿qué?
Cronikeys
Forum Commoner
Posts: 35
Joined: Sun Jan 16, 2005 9:14 am

Post by Cronikeys »

I don't want that to solve by arrays.
Why? it will make your code 100% smaller with an array ;)

Also, give us some more info... where is this information going to go? is it like an online quiz, a database, an emailer... what?
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post by AGISB »

If you don't want to use arrays for whatever not understandable reason you have to hardcode the stuff.

Code: Select all

if (isset($_POST['checkbox1'] )) {
        //do something here
}
if (isset($_POST['checkbox2'] )) {
        //do something here
}
.
.
.
frANZi
Forum Newbie
Posts: 12
Joined: Mon Mar 21, 2005 1:52 pm
Location: Croatia

Post by frANZi »

Yeah... I know how to do this with if...then.

I think you don't understand me, I don't want the name of my checkbox to be "id[]" or something like this.

I have an idea to put all checkboxes in an array (array: 0=>checkbox1, 1=>checkbox2 ...), and then for each element in an array to do some code.
Also, give us some more info... where is this information going to go? is it like an online quiz, a database, an emailer... what?
I am making a database, and the first site is the site where the data is filled in, and the second step is for checking the data that is filled in. And then the data is put in a database.

Thanks for fast answers.
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post by AGISB »

I am confused now. You contradict yourself. Use arrays not use arrays. You mix this around.

if you use name="id[]" your checkboxes are put in the array $_POST['id'] that you can process on the next page.

I think your logic error is to set the value of the checkbox all to 1. This does not make sense. You put a name like "checkbox1" into the value field.

Every checked checkbox is found in the array $_POST['id'] and with a foreach you can get all the names of the checked checkboxes. As simple as that.

There is a good tutorial in the tutorial section
frANZi
Forum Newbie
Posts: 12
Joined: Mon Mar 21, 2005 1:52 pm
Location: Croatia

Post by frANZi »

Now, I managed somehow to make the code:

Code: Select all

<?php
$array = array("abc","abs","ads","alufelge","asd","asr","esp","ets","4wd","sportovjes","tuned");
foreach ($array as $value) {
    if (isset($_POST[$value])) {
        $post = $_POST[$value];
    }

mysql_select_db($database_db_aoglasnik, $db_aoglasnik);
$query_rs_karakteristike = "SELECT string FROM karakteristike WHERE name = $value";
$rs_karakteristike = mysql_query($query_rs_karakteristike, $db_aoglasnik) or die(mysql_error());
$row_rs_karakteristike = mysql_fetch_assoc($rs_karakteristike);
$totalRows_rs_karakteristike = mysql_num_rows($rs_karakteristike);

if ($post == 1) {
    echo "$row_rs_karakteristike".", ";
}
}
?>
Some variables in the code are in Croatian, but it is no matter.

Now when I execute the code it writes me: Unknown column 'abc' in 'where clause'.

I don't see the problem.

In the "karakteristike" table i have id, name and string, id's are auto increment, and names are like the array above. The strings are what I have to write down.

Yes, maybe I am a complicated person, but I am somehow "experimenting", I ve seen that code on an another site, and now I want to make that also.

However, thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you need to surround the string in quotes or mysql will think it's a field/column reference.
frANZi
Forum Newbie
Posts: 12
Joined: Mon Mar 21, 2005 1:52 pm
Location: Croatia

Post by frANZi »

O.K., thanks.

But a new problem:

Now it prints me: Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array,



What is this now????????????????
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

each record/row returned from mysql_fetch_assoc() is an array. $row_rs_karakteristike['string'] will give you the string found..
frANZi
Forum Newbie
Posts: 12
Joined: Mon Mar 21, 2005 1:52 pm
Location: Croatia

Post by frANZi »

Where do i have to place ['string'] and what means that in it "string", do I have to replace the string with something.

Thanks a lot feyd, you are really fast.
frANZi
Forum Newbie
Posts: 12
Joined: Mon Mar 21, 2005 1:52 pm
Location: Croatia

Post by frANZi »

ok, ok, ok, I got it, I must place ['string'] in the echo.

But now it writes me all the strings from the table.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that's what you are asking it to do it appears...
frANZi
Forum Newbie
Posts: 12
Joined: Mon Mar 21, 2005 1:52 pm
Location: Croatia

Post by frANZi »

OK, it is all finished.

I am the new prouded owner of the stupid big code.

I AM PROUD.

Thanks to all people on this forum, thanks to people that loved me, thanks to my mum and dad, thanks to all people on the world.

I love you all.

I FEEL AS I AM THE WINNER OF OSCAR.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Achieving that which frustrates you has that effect...:)

Now maybe whoever is adding "If you are going to post code of any kind, please review Posting Code in the Forums first" atop my textbox will have the same feeling when they get the post page to display properly...lol
Post Reply