Page 1 of 1

Getting Form Variables

Posted: Sun Nov 23, 2003 8:13 pm
by mikea
Ok, here's the problem. A script is pulling fields (help desk categories) from a database and displaying them on the screen in a checkbox fashion. You can then check the box to determine who can see these fields (similar to the departments in PerlDesk). The problem is that the number of categories can change as the admin adds and deletes them. So I'm trying to create a script that will display the choices and save information. What I had didn't work so someone suggested the following:

This is in a loop that draws checkboxes and assigns the checked value to $hd_cat_id based on the information it pulls from the category HD. There could be anywhere from 1 to 20 or more boxes. It' all depends on how many categories are entered into the HD.

Code: Select all

print "$default_right_line<input name="cat_check[]" type="checkbox" value="$hd_cat_id">Check to Allow</font></font></font></div></td>";
This is supposed to read each variable and print it to the screen, provided it was checked. In the final version I'll do something with the information, the print is just to make sure it's doing what it's supposed to be doing. $total_count is passed from the calling form as the total categories displayed.

Code: Select all

for ($counter = 1; $counter <= $total_count; $counter++) 
      {
       $cat_check[$counter]=(bool)(isset($_POST['cat_check'][$counter]));
      print "$counter - $cat_check[$counter] .. ";
      }
    die();

If I select all the boxes then the output is something like this

1 - 1 .. 2 - 1 .. 3 - 1

But the data that's stored in $cat_check[$counter] (the hd_cat_id) isn't 1. At least not for all the fields, maybe one of them is.

It should look something like:

1 - 1 .. 2 - 4 .. 3 - 10.. where the second number is the hd_cat_id.

Any suggestions would be helpful.

Posted: Sun Nov 23, 2003 9:05 pm
by infolock
for starters this line :

Code: Select all

<?php

print "$default_right_line<input name="cat_check[]" type="checkbox" value="$hd_cat_id">Check to Allow</font></font></font></div></td>"; 


?>
isn't set up correctly. you are setting the variable to be a word instead of a var..

also, try using echo instead of print like this :

Code: Select all

<?php

echo $default_right_line.'<input name="'.$cat_check[].'" type="checkbox" value="'.$hd_cat_id.'">Check to Allow</font></font></font></div></td>'; 

?>

edit : what is cat_check suposed to be ??? I put it in there as an array value , but you didn't even have a $ in front of it before, which makes me wonder if this is just how you wanted it to look? if it is, just remove the ' marks, the periods, and the $ mark so that it looks like name = "cat_check[]"

Posted: Sun Nov 23, 2003 9:13 pm
by mikea
What do you mean a word instead of a var?

Also, why use echo instead of print? Most of my code I use print so I've not quite understood the difference.

Posted: Sun Nov 23, 2003 9:37 pm
by infolock
echo and print are the same, i just use echo since it's a lost more simple.

but what i was meaning as far as word and var is this :

let's say $var = 'jon'

and we call

echo 'hello $var';

then it's gonna echo HELLO $var on your screen, not what $var actually stands for..

so, you would use

echo 'Hello '.$var
instead to get the browser to display $var's true value.

see what i mean?

so we had to escape the echo string, and assign each $var properly in order to correctly assign it's value..

again, i just suggested to use echo because it's easier to use when calling variables...