Page 1 of 2

$_POST and checkbox

Posted: Mon Mar 21, 2005 2:03 pm
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.

Posted: Mon Mar 21, 2005 2:06 pm
by feyd
¿qué?

Posted: Mon Mar 21, 2005 2:48 pm
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?

Posted: Tue Mar 22, 2005 12:52 am
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
}
.
.
.

Posted: Tue Mar 22, 2005 7:28 am
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.

Posted: Tue Mar 22, 2005 8:58 am
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

Posted: Tue Mar 22, 2005 12:20 pm
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.

Posted: Tue Mar 22, 2005 12:24 pm
by feyd
you need to surround the string in quotes or mysql will think it's a field/column reference.

Posted: Tue Mar 22, 2005 12:32 pm
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????????????????

Posted: Tue Mar 22, 2005 12:34 pm
by feyd
each record/row returned from mysql_fetch_assoc() is an array. $row_rs_karakteristike['string'] will give you the string found..

Posted: Tue Mar 22, 2005 12:41 pm
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.

Posted: Tue Mar 22, 2005 12:43 pm
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.

Posted: Tue Mar 22, 2005 12:47 pm
by feyd
that's what you are asking it to do it appears...

Posted: Tue Mar 22, 2005 12:55 pm
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.

Posted: Tue Mar 22, 2005 1:40 pm
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