Jump to content

Checkbox issue wont show unchecked


Waddy

Recommended Posts

Hi,

 

I just cannot seem to get this to work, have been testing and searching all morning and not getting anywhere.

 

I have a form with a series of checkboxes which populates the MySqL database, this works fine.

 

Trying to print the results from the form if the row is empty show an unchecked checkbox, if there is a value show a checked checkbox.

 

I have tested my if statement and can echo correctly wether empty or not empty, just cannot get the checkbox to be unticked if there is no value in the row

while ($row = mysql_fetch_array($qry, MYSQL_BOTH)) {    

  // show total fields for debugging
//  $totalfields = mysql_num_fields($qry);
//  print ($totalfields)  ;


// if ( ! empty ( $row ['acid_wash'] ['neutralise'] ['clean'] ) ) { 
if ( ! empty ( $row ['clean']  ) ) {        
///if it's 1 (true) then we add 'checked' to a variable we will use in the input 
//to cause it to be checked 
   $checked = 'checked'; 
  
// test if row has data
echo "full";
    
}else{ 
//if the value is 0 (false) we set the variable to be empty, not checked 
   $checked = ''; 
// test for no value   
echo "empty" ;
}     

To Display Result:

<tr>
    <td>High Pressure Clean <input style='font-size:10px;' name='clean[]' type='checkbox' checked='$checked'></td>
      </tr>

 

The empty and full test echos do seem to work, showing me the if statement works.

 

The problem is even when the row is empty the checkbox shows checked....

 

Thanks.

Link to comment
Share on other sites

Hey,

 

I believe the word 'checked' being in the HTML <input> line is all you need to have the checkbox selected.  So since you always have 'checked=' it will always be checked.  Try changing that line to:

 

<td>High Pressure Clean <input style='font-size:10px;' name='clean[]' type='checkbox' $checked></td>

 

Hope this helps.

 

Cheers.

Link to comment
Share on other sites

:D

 

Thanks for the help 2-d, that did the trick, it works. I just need to now workout how to have that if statement run against several rows, not just clean. i.e clean, grind, neutralise rows in the DB and implement into the HTML some way.

 

 if ( ! empty ( $row ['clean']  ) ) {        
///if it's 1 (true) then we add 'checked' to a variable we will use in the input 
//to cause it to be checked 
   $checked = 'checked'; 
   echo "Clean<input style='font-size:10px;' name='clean' type='checkbox' $checked>";
    
}else{ 
//if the value is 0 (false) we set the variable to be empty, not checked 
   $checked = ''; 
   echo "Clean<input style='font-size:10px;' name='clean' type='checkbox' $checked>";
}     

 

 

 

 

Link to comment
Share on other sites

Ok nearly there, just one question if you can help, I have several rows I need the if statement to run against if ! empty or empty, i.e clean, acid_wash, neutralise etc. Basically the user chooses the options they require using checkboxes on the form, about 10 options, I would like to display the options chosen with a checkbox checked or unchecked depending if the row is empty or not.

 

How can i get the if statement it to use these several rows instead of writing multiple if statements?

 

So i can use

 

<tr>
    <td><?php echo "Acid Wash<input style='font-size:10px;' name='acid_wash' type='checkbox' $checked >";?> </td>
    
  </tr>
  <tr>
    <td><?php echo "Neutralise<input style='font-size:10px;' name='neutralise' type='checkbox' $checked>";?>  </td>
    
  </tr>

Link to comment
Share on other sites

Do you mean you want to have multiple comparisons in one if statement?  You can use || (or) to check if any of the rows are empty like this:

 

if (!empty($row['acid_wash']) || !empty($row['neutralise']) || !empty['clean']) || !empty(['grind']))

 

an or will check all of the variables, and if any one of them returns true, you will fall into that case.

Link to comment
Share on other sites

Hi 2-d,

 

Thanks for that, I did try that yesterday, I dont think it will work for this case as I think I may need to set a variable for each "option" to show a checked/unchecked checkbox for the options they choose when print to screen. I was trying to reduce the if statements, instead of doing one for each option as there are quite a few. Aicd Wash, neutralise, clean, grind and many more.

 

I have a form with the usual, name, address etc, and have about 10 checkboxes for where they can choose options, these are stored in the DB. I want to get the data from the form and print it to a page for them, showing whats been chosen, with the options they chose shown as a checkbox, checked or unchecked. This now works thanks to your help, but it looks like i need an if statement for each one to create a variable to use in the html..

 

If that makes sense.

 

Just not sure if its possible somehow due to my lack of knowledge, I have thought of creating a function but not sure if thats the solution either.

 

Thanks for the help.

 

 

 

 

 

Link to comment
Share on other sites

Yeah, that make sense.  What you can do in that scenario, is foreach loop through every element that is in the row and then display it accordingly.  I will entail just the same amount of if statements run, but you will only have 1 if statement coded.  It will just loop through it accordingly. 

 

So you can do something like this:

 

while ($row = mysql_fetch_array($qry, MYSQL_BOTH)) {    

foreach($row as $key => $val){
	if(!empty($val)){
		$checked = "checked";
	}
	else{
		$checked = "";
	}

	// This will make the key into a nice name to display in the echo
                // ucfirst makes the first letter captial, and str_replace swaps our the _ with spaces
	$nice_name = ucfirst(str_replace("_", " ", $key));

	echo $nice_name . "<input style='font-size:10px;' name='$key' type='checkbox' $checked>";
}

}

 

I added in the $nice_name part as well to make a nice name out of the Key so you can display it in the echo.  Let me know if this helps or if you need any explaining.

 

Cheers

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.