nitation Posted August 28, 2008 Share Posted August 28, 2008 Good day, I have a checkbox option on my form whereby a user is allowed to choose. My question is , how do i retrieve the selected checkbox a user clicked in PHP. Link to comment https://forums.phpfreaks.com/topic/121674-capturing-checkbox-value-in-php/ Share on other sites More sharing options...
JasonLewis Posted August 28, 2008 Share Posted August 28, 2008 Using $_POST On the page that php posts the form data to, put this at the top: <?php print_r($_POST); ?> That will show you what variables have been set. Link to comment https://forums.phpfreaks.com/topic/121674-capturing-checkbox-value-in-php/#findComment-627694 Share on other sites More sharing options...
nitation Posted August 28, 2008 Author Share Posted August 28, 2008 @Projectfear I can do that and even insert into the database. I am trying to retrieve the selected checkbox for every user whereby the selected checkbox should be checked if it was selected. Thanks in advance Link to comment https://forums.phpfreaks.com/topic/121674-capturing-checkbox-value-in-php/#findComment-627695 Share on other sites More sharing options...
JasonLewis Posted August 28, 2008 Share Posted August 28, 2008 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? Link to comment https://forums.phpfreaks.com/topic/121674-capturing-checkbox-value-in-php/#findComment-627698 Share on other sites More sharing options...
nitation Posted August 28, 2008 Author Share Posted August 28, 2008 @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 /> Link to comment https://forums.phpfreaks.com/topic/121674-capturing-checkbox-value-in-php/#findComment-627714 Share on other sites More sharing options...
JasonLewis Posted August 28, 2008 Share Posted August 28, 2008 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. Link to comment https://forums.phpfreaks.com/topic/121674-capturing-checkbox-value-in-php/#findComment-627724 Share on other sites More sharing options...
nitation Posted August 28, 2008 Author Share Posted August 28, 2008 the $category is holding the value of the checkbox/es the user clicked during registration. All i wanna do is to retrieve all the checkbox that was clicked by a user during registration. Link to comment https://forums.phpfreaks.com/topic/121674-capturing-checkbox-value-in-php/#findComment-627739 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.