Jump to content

repopulate checkboxes help


glennn.php

Recommended Posts

i have a form with an array of checkboxes that i need to repopulate when returned to it to edit it. I've tried a few different means, but i'm still a novice at php.

 

can someone help me get the posted values back into the checkboxes that are originally checked? this doesn't work:

 

<input name="category[]" type="checkbox" value="1-Anatomic (AP)" <?php if($_POST['category'] == 1) echo "checked=\"checked\"; " ?> />
         Anatomic (AP)<br />
       <input name="category[]" type="checkbox" value="2-Anatomic & Clinical(AP/CP)" <?php if($_POST['category'] == 2) echo "checked=\"checked\"; " ?> />
         Anatomic & Clinical(AP/CP)<br />
        <input name="category[]" type="checkbox" value="3-Cardiovascular Pathology" <?php $category = $_POST['category']; if($category == '3') echo "checked=\"checked\"; " ?> />
         Cardiovascular Pathology<br />

 

thanks in advance,

GN

Link to comment
https://forums.phpfreaks.com/topic/103737-repopulate-checkboxes-help/
Share on other sites

You seem to be using the array equivalent of category instead of just "category".  Either try using just the name category, instead of category[], or in your <?php echo ?>, or use $_POST['category[0]'] == "1-Anatomic (AP)").

 

Note: the value you are checking must match exactly the value passed from the checkbox field.

If your browsing around the page for sometime I don't think $_POST will work. (someone correct me if I am wrong). I think that the variables won't be 'set' after you click submit and leave the area. I would suggest using $_SESSION variables to store current work and then if you go back to edit pull your check/unchecked values from the session variables.

 

Also, like phorman stated your getting information from wrong input names. Try to name your checkboxes something like name="1" value="1-Anatomic (AP)" and then test like this:

<?php
if($_POST['1'] == "1-Anatomic \(AP\)")$box1 = "CHECKED";

//Input will look like:

echo "<input name='1' type='checkbox' value='1-Anatomic (AP)' checked='$box1'/>";
?>

Let me know if you need some additional explanation.

i'm a terrible describer.

 

i'll go into more detail:

 

having this form as ready to submit for processing:

 

<form...>
<input type="hidden" name="c_name2" value="<?=$_POST["c_name2"]?>">
<input type="hidden" name="email2" value="<?=$_POST["email2"]?>">
<input type="hidden" name="telephone2" value="<?=$_POST["telephone2"]?>">
<?php 
//GET THE CHECKBOXES
$category=$_POST['category']; //this is passed as an array

   //CHECK WHETHER A CHECKBOX HAS BEEN SELECTED
   if(!empty($category))
   {
      //LOOP THE SELECTED CATEGORIES
         for($x=0; $x<count($category); $x++)
         {
            $exp=explode("-", $category[$x]);
            
            //$exp[0] is the int, and $exp[1] is the name

            echo '<input type="hidden" name="category[]" value="'.$exp[0].'">';

//////////////////
//
//  <input type="hidden" name="category[]" value="2">
//  <input type="hidden" name="category[]" value="1">  etc...
//
//////////////////

         }
   }
   
<input type="image" src="images/edit_posting.gif" border="0" name=button3 onClick="OnButton3();">

 

onClick calls an editform.php that needs to retain all the values:

 


<input name="telephone2" type="text" size="35" id="telephone2" value="<?=$_POST['telephone2'] ?>" /> 
//  no problem here, of course


<input name="category[]" type="checkbox" value="1-Anatomic (AP)" <?php if($_POST['category[0]'] == "1") echo "checked=\"checked\";" ?> /> Anatomic (AP)<br />
<input name="category[]" type="checkbox" value="2-Anatomic & Clinical(AP/CP)" <?php if($_POST['category[1]'] == "2-Anatomic & Clinical(AP/CP)") echo "checked=\"checked\";" ?> />

 

( when it's sent to the database, the categories are just in integers )

 

the attempts above didn't work, either.

 

hmmm... help?

Thanks

GN

 

can't rename the checkbox fields - they're built as an array from a database

 

checkBoxSearch('category[]', 'SELECT pk AS field1, name AS field2 FROM category ORDER BY name ASC', 1, 5, 0);

 

this isn't my script; i thought the [] was necessary in a form field array...?

 

 

 

 

If your browsing around the page for sometime I don't think $_POST will work. (someone correct me if I am wrong). I think that the variables won't be 'set' after you click submit and leave the area. I would suggest using $_SESSION variables to store current work and then if you go back to edit pull your check/unchecked values from the session variables.

 

Also, like phorman stated your getting information from wrong input names. Try to name your checkboxes something like name="1" value="1-Anatomic (AP)" and then test like this:

<?php
if($_POST['1'] == "1-Anatomic \(AP\)")$box1 = "CHECKED";

//Input will look like:

echo "<input name='1' type='checkbox' value='1-Anatomic (AP)' checked='$box1'/>";
?>

Let me know if you need some additional explanation.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.