Forum Moderators: coopster

Message Too Old, No Replies

Help with keeping checkboxes checked when looping over array

         

TheBrandon

4:04 pm on Aug 3, 2010 (gmt 0)

10+ Year Member



I'm currently writing a script that lets people submit information about specials their business is offering. The submission form is pretty long (requiring info about the business and the specials) so I am trying to make the form as easy for the user I can.

Right now there are 2 options that I am submitting as arrays. These are the categories the discount belongs in, and the type of people eligible for said discount. This way I don't have to have 100 SQL fields for categories; I can just submit an array and manipulate it with implode/explode.

Where I am confused is once the user submits the form, my processing script makes sure it is an array. If it isn't an array, they didn't check a category so it dumps them back to the form.

However if they did check one and just didn't answer another required question, I'd like their category selection to still be checked.

Currently I am using this code to generate the category check boxes:
mysql_connect(SQL_HOST_NAME, SQL_USER_NAME, SQL_PASSWORD) or die(mysql_error());
mysql_select_db(SQL_DATABASE) or die(mysql_error());
$result = mysql_query("SELECT * FROM categories")or die(mysql_error());

echo '<ul class="cat_list_checkboxes">';

while($row=mysql_fetch_assoc($result)){
echo '<li><input type="checkbox" name="categories[]" value="'.$row['id'].'">'.$row['category_name'].'<li>';
}

echo '</ul>';


I'm confused as to how I need to add an if/else check in this and loop over it so the checkboxes that were previously checked are still checked when the user goes back to the form page.

If it matters, I have the above as a function so the above code is this:
<li>
<label for="store_location">Categories <span class="small">(select up to four)</span></label>
<?php fetch_categories_checkboxes(); ?>
<li>

/*Name: fetch_categories_checkboxes()
/*Desc: This function grabs the categories and outputs them in a checkbox format
/*
*/
function fetch_categories_checkboxes() {
mysql_connect(SQL_HOST_NAME, SQL_USER_NAME, SQL_PASSWORD) or die(mysql_error());
mysql_select_db(SQL_DATABASE) or die(mysql_error());
$result = mysql_query("SELECT * FROM categories")or die(mysql_error());

echo '<ul class="cat_list_checkboxes">';

while($row=mysql_fetch_assoc($result)){
echo '<li><input type="checkbox" name="categories[]" value="'.$row['id'].'">'.$row['category_name'].'<li>';
}

echo '</ul>';
}


Can someone please help me with this?

jatar_k

12:47 pm on Aug 4, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



one way to figure it out is to echo the $_POST array

echo '<pre>';
print_r($_POST);
echo '</pre>';

then find the value you are looking for and test it when it's on and off

then you could do a simple test in your checkbox echo

echo '<li><input type="checkbox" name="categories[]" value="'.$row['id'].'"';
if (isset($_POST['categories']) && $_POST['categories'] == 'somevalue') echo ' checked';
echo '>'.$row['category_name'].'<li>';

I think it just comes through as categories and then a numbered array of values but I suggest you check it as I am only having my first sip of coffee ;)

TheBrandon

9:57 pm on Aug 4, 2010 (gmt 0)

10+ Year Member



But if I did that, wouldn't I have to be making this per checkbox manually? So I could enter each 'somevalue' field to be accurate?

They're both generated through an array/loop. I posted the code above about how I'm generating them.

I think your method would work if I were printing the checkboxes manually, but I'm not.

Basically I have an SQL table listing the categories. I need to idiot proof it so someone can add a category through a PHP interface I'm making. The function then pulls that table and prints it in checkbox form. I don't want anyone to have to go in and edit the PHP just because they made a new category so I need everything to be as automated as possible using this system (or a different one, if there is a better idea?)

Matthew1980

10:23 pm on Aug 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there TheBrandon,

Sounds like you need to add a field in the DB, and as it's a checkbox - use an emun field and set it to either 'yes', 'no' or '0','1' so that depending on what is the value from the db you can pop and if there to echo 'checked' if the value is 1 and echo nothing if 0. And then just update the db with whatever the user selects, this way whatever the db pulls will always be relevant to what the user selected before.

Sorry if that sounds confusing, but it does make sense & I'm too tired to give a code example.

Hope I have understood you correctly.

Cheers,
MRb

TheBrandon

3:55 pm on Aug 5, 2010 (gmt 0)

10+ Year Member



Would there be any way to do that with a session array? There will be multiple people on the site at a time so what you're proposing is creating an entire database to hold "temporary" submissions?

Could this just be done with a cookie on their PC? I think that would be a much more efficient way since I don't see how adding in the checkbox thing to the database will help with my problem?

rocknbil

6:12 pm on Aug 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd use named elements, similar to what M mentioned above, but if you must use an array, step through it like an array.

while($row=mysql_fetch_assoc($result)){
echo '<li><input type="checkbox" name="categories[]" value="'.$row['id'].'"';
if (isset($_POST['categories'])) {
foreach ($_POST['categories'] as $c) {
if ($c==$row['id']) { echo ' checked'; break; } // checked="checked" for XHTML
}
}
echo '>'.$row['category_name'].'<li>';
}

TheBrandon

8:33 pm on Aug 5, 2010 (gmt 0)

10+ Year Member



I'll try that code shortly but can you elaborate on what you mean by named elements? It looks like I'm using the same name element you are?

Matthew1980

8:45 pm on Aug 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

I still think that the enum field in the DB would be the better option here, I see the logic to Rocknbils version, but as long as you have a column in the DB and then a 1 (Checked) 0 (not checked), then for each iteration of the loop reading from the DB, a ternary could be employed to make the check/uncheck happen:-

while($row=mysql_fetch_assoc($result)){
echo '<li><input type="checkbox" name="categories[]" value="'.$row['id'].'" '.($row['select'] == 1) ? 'Checked':'').' ';
}

Then depending on the result from the DB a column called 'select' (whether it's 1 or 0) the element will be either checked or unchecked. The all the user does is make their selection, and when you press update, just modify your receiver code to handle the extra column - it's just a suggestion, but that's the way I do it for one of my scripts.

Hope that's helpful

>>elaborate on what you mean by named elements

Yes Rocknbil, I think I know what your getting at but, it's been a long day on VB6 for me and nothing makes sense ;) so could you entertain us ;-p

Cheers,
MRb

rocknbil

6:22 pm on Aug 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Was confusing with another one of Matthew's threads. HTML elements.

$count=0;
foreach ($list as $checkbox_value) {
$count++;
$name = 'categories_' . $count;
echo "<input type=\"checkbox\" name=\"$name\" value=\"$checkbox_value\">
}

As opposed to an array.

categories[ ]