Jump to content

Capturing Checkbox Value in PHP


nitation

Recommended Posts

Oh I think I understand.

 

$checkbox1_value = 1; //For example, you set a setting to 1, meaning it will need to be checked on the form.

if($checkbox1_value == 1){
$selected['checkbox1'] = "selected='selected'";
}else{
$selected['checkbox1'] = ""; //they didn't have it selected
}

echo <<<html
<input type="checkbox" name="checkbox1" value="1" {$selected['checkbox1']}>
html;

 

Is that kind of what you are after?

@Projectfear

 

This is my code. It doesn't work.

 


<?php
	 $Sqleditcat=mysql_query("select * from pdfsectionlink where id='$edit'") or die (mysql_error());
if(!empty($Sqleditcat)){
$row=mysql_fetch_array($Sqleditcat);
		  $category=$row["category"];

		  $category = 1;
		  if($category == 1){
              $selected['category'] = "selected='selected'";
              }else{
              $selected['category'] = ""; //they didn't have it selected
              }



		        
  }
?>
<input name="category" type='checkbox' value="1" <?php $selected['checkbox1']?>>  <?php print "Test"; ?><br />

Well what doesn't work?

 

Let's tidy it up a bit and sort some things out...

 

<?php

$Sqleditcat=mysql_query("SELECT * FROM pdfsectionlink WHERE id='$edit'") or die (mysql_error());

//Check if the error was successful
if(!$Sqleditcat){
$selected = array();
$row = mysql_fetch_array($Sqleditcat);
$category = $row['category'];

if($category == 1){
	$selected['category'] = "selected='selected'";
}else{
	$selected['category'] = ""; //they didn't have it selected
}
}
?>

 

What exactly will $category hold? What value?

In your code you were retrieving the value then setting it to 1 so it would always be a selected checkbox.

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.