I have an "apply for a job" form that includes a bunch of checkboxes -- "Check the jobs that you would like to apply for".... The checkboxes get their name/value from a database.

I can get the checkboxes to display correctly. What I can't do is get them to stay checked/unchecked once someone has submitted the form and then gets an error message that takes them back to the form. I have looked around this forum and several others and found code that should work -- but it doesn't. Here's the code I'm using:


// Connect to the dbase and get the job data...

// Use a while loop to get all job data

while ($row = mysql_fetch_array($result)) {
   $job_id = $row["job_id"];
   $job_title = $row['job_title'];

// Display checkboxes using the job data

   if ($_HTTP_POST_VARS['job[$job_id]'] == '$job_title') {
      echo ("<input type=\"checkbox\" name=\"job[$job_id]\" value=\"$job_title\" checked> $job_title<br>\n");
   } else {
      echo ("<input type=\"checkbox\" name=\"job[$job_id]\" value=\"$job_title\"> $job_title<br>\n");
   }

} // end while loop

I've tried this using just $POST in place of $HTTP_POST_VARS. I've also tried this using isset($_POST) and other variations (eg., if (!isset...). The form comes back with all of the job titles and checkboxes showing - just not with any checked that may have been checked and then submitted.

I've got some non-dynamically generated checkboxes in this form and am able to get those to display as checked/unchecked with no problem. Here's example code for that:


if ($full_time_work_wanted == 1) {
   echo ("<input type=checkbox name=full_time_work_wanted value=1 checked> Full Time<br>");
} else {
   echo ("<input type=checkbox name=full_time_work_wanted value=1> Full Time<br>");
}

In the end, we simply are e-mailing the form input to someone.

So no need to save anything to the database or the like. Except for this dynamically generated checkbox part of the form, the application is pretty straightforward.

Any help - very appreciated!

    What's up man?

    Ok I might have found just a mental error here:

    $_HTTP_POST_VARS['job[$job_id]'] == '$job_title'

    Shouldn't that be:

    $_HTTP_POST_VARS['job[$job_id]'] == '$job_id'
    ?

    The reason I say this is that unless the title is the ID that wouldn't make sense in your table structure to be setting UNIQUE field (the ID field) and the title are the same thing. Am I right here or off in left field?

    Chad

      hey - what up?

      Ok - understood. But when I swapped out $job_title for $job_id I still don't get checks where I just checked off a box. I just get all of the job choices, none of them checked.

      Could it be that the way I've set this up I'm constantly overwriting the checkboxes with a fresh call to the database to build the checkboxes?

        Hey man, I am still in the office right now. Let me get home and I can pull up some code I have there. Give me a few hours (I know I hate waiting when I am working on a major problem). I have my stuff constantly resetting all the time with no problem, let me get home to see if I did anything different. Cya in a few hours,

        Chad

          Thanks - any help you can offer I truly appreciate!

            Ok... I am at home now.

            Try doing this for me:

            while ($row = mysql_fetch_array($result)) { 
               $job_id = $row['job_id']; 
               $job_title = $row['job_title']; 
            
            // Display checkboxes using the job data 
            
               if ($_HTTP_POST_VARS['job[$job_id]'] == '$job_title') { 
                  echo ("<input type=\"checkbox\" name=\"job[$job_id]\" value=\"$job_title\" checked> $job_title<br>\n"); 
               } else { 
                  echo ("<input type=\"checkbox\" name=\"job[$job_id]\" value=\"$job_title\"> $job_title<br>\n"); 
               } 
            
            } // end while loop
            

            if the doesn't work then try this:

            while ($row = mysql_fetch_array($result)) { 
               $job_id = $row['job_id']; 
               $job_title = $row['job_title']; 
            
            // Display checkboxes using the job data 
            
               if ($_HTTP_POST_VARS['job[$job_id]'] == '$job_id') { 
                  echo ("<input type=\"checkbox\" name=\"job[$job_id]\" value=\"$job_title\" checked> $job_title<br>\n"); 
               } else { 
                  echo ("<input type=\"checkbox\" name=\"job[$job_id]\" value=\"$job_title\"> $job_title<br>\n"); 
               } 
            
            } // end while loop
            

            See if that works out. If not then do this one:

            while ($row = mysql_fetch_array($result)) { 
               $job_id = $row['job_id']; 
               $job_title = $row['job_title']; 
            
            // Display checkboxes using the job data
               echo "$_HTTP_POST_VARS['job[$job_id]'] || COMPARED TO || $job_title || OR || $job_id<br>";
               $temp_posted_jobid = $_HTTP_POST_VARS['job[$job_id']'];
               echo "$temp_posted_jobid<br>";
               if($temp_posted_jobid == $job_id)
               {
                  echo "We have a match HOUSTON!<br>";
               }
            } // end while loop
            

            That way you can disect EXACTLY your results. If you get a match somewhere then heck you know how to take what your expected is and make it into what you want. I wish I could help you further but man it's hard to test something like this without being on the server. I will show you how I did it a while back:

            <?php
            
            // This page was part of the form.  Then it would be called
            // using $PHPSELF.  Then it would run the next script.            
            
            // This is an include of the connection strings.
                        include("connection.inc");
            
                    // First I get everything from categories.
                    $sql = "select * from categories";
                    $result = mysql_query($sql);
                    while ($row = mysql_fetch_array($result)) {
                    		// I then get the category_id.
                          $category_id = $row['category_id'];
                          $category_name = $row['category_name'];
            
            					// This just outputs the category name
                          echo "\r\r<p><strong>$category_name<br>";
                          $sqla = "select * from subcategories where ref_cat='$category_id'";
                          $resulta = mysql_query($sqla);
                          while ($row = mysql_fetch_array($resulta)) {
                                $sub_id = $row['sub_catid'];
                                $sub_title = $row['sub_title'];
                                echo "\r<input name='checkbox[]' type='checkbox' class='text_boxes_contractor' value='$sub_id'></strong>$sub_title<br>";
                          }
                    }
                    ?>
            
            <?php
            $sql = "select * from member_accounts where account_type='4'";
              $result = mysql_query($sql);
            	while ($row = mysql_fetch_array($result)) {
                     $owner_id = $row['member_id'];
                     $owner_email = $row['member_email'];
            
            	$sqla = "select * from profiles where owner_id='$owner_id'";
            	$resulta = mysql_query($sqla);
            	while ($rowa = mysql_fetch_array($resulta)){
            		$categories_served = $rowa['categories_served'];
            
            		$cat_served_array = explode(",",$categories_served);
            
            	foreach($cat_served_array as $cat_id)
            		{
            			foreach($checkbox as $checkbox_value)
            			{
            				if($cat_id == $checkbox_value)
            				{
            					//This will be where we mail the suppliers!
            					//  It will also disallow duplicate emails to the suppliers.
            					//  Good.
            					MailSupplierRFP ($url,$start_date,$ending_date,$project_title,$project_description,$owner_email);
            					$done = 1;
            				}
            				if($done == 1)
            				{
            					break;
            				}
            			}
            		}
            	}
            }
            ?>
            

            That is a really long winded thing but basically what I did is take the categories and checked them against a database to see if a user had this marked as a category they were interested in. If so then it would send an email to them. But it would not duplicate an email, hence if you had 3 categories you wanted to get an email on and all 3 were listed it would not send you 3 emails just one. Making it easier for you and the customer. :-D Let me know if this helps you out at all. I hope it did and I guess my signature really shines through with this coding. Haha.

            Lata,

            Chad

              depends on the browser - some require radio buttons and check boxes to be

              checked="checked" and selected="selected"

              instead of just 'checked' or 'selected'

              might just be the markup rather than the code at fault

                Hi Ihcra - Since the checkboxes that are not dynamically generated work just fine with "checked" only in the input code, I figure that "checked" should work in dynamically generated checkboxes. I am looking at this on a PC - IE6 and Mozilla (whatever's the latest). Also viewing in Mac/Safari. "checked" seems to be not a problem. Unless i'm missing something.

                chads2k2 - I'm checking out your code now - thanks!

                  Hey no problem buddy! Hopefully we can find out what your problem was, because man it sucks to be stuck in a problem.

                  Also "selected" is for "Select" boxes. Hince the word "selected". Check boxes only have checked.

                  Select Boxes == selected
                  Check Boxes == checked

                  In all my years of doing this I have never seen a checkbox use "selected" as a identifier. But hey, that doesn't mean your not right. What browser uses selected instead of checked?

                  Chad

                    Ok - I finally figured this out! Here's the solution to this problem! Works great!

                    // Connect to the dbase and get the data needed to display job info
                    
                    // Do a while loop to loop through all jobs available to apply for
                    
                    while ($row = mysql_fetch_array($result)) {
                       $job_title = $row['title'];
                    
                    // If the form has been submitted, and there's a need to post the form
                    // again due to missing input, etc., find out which jobs were checked off
                    // and echo them back to the screen as checked.  So...
                    
                       if ($_POST['job']) {
                       echo ("<input type=\"checkbox\" name=\"job[]\" value=\"$job_title\"");
                       foreach ($_POST['job'] as $checked_box) { 
                          if ($checked_box == $job_title) {
                             echo (" checked");
                          }
                       }
                       echo ("> $job_title");
                    
                       } else { // If the form hasn't yet been submitted, just echo unchecked job[] checkboxes
                    
                       echo ("<input type=\"checkbox\" name=\"job[]\" value=\"$job_title\"> $job_title");
                       }
                    } // End the while loop
                    
                    

                    Thanks for the help! And hopefully this will help someone else out too!

                      Originally posted by new7media
                      Hi Ihcra - Since the checkboxes that are not dynamically generated work just fine with "checked" only in the input code, I figure that "checked" should work in dynamically generated checkboxes. I am looking at this on a PC - IE6 and Mozilla (whatever's the latest). Also viewing in Mac/Safari. "checked" seems to be not a problem. Unless i'm missing something.

                      it caught me by surprise too i admit - and so far ive only found it to be the case in camino and firefox on mac

                      sorry i worded it badly when i included comment on selected. i was meaning that the same method was also required in the affected browsers for <select> elements as well - indeed check boxes do not accept selected as an attribute

                        Write a Reply...