davidolson Posted June 19, 2014 Share Posted June 19, 2014 function get_checked($variable, $status){ if($variable == $status){ return 'checked="checked"'; }else{ return ''; } } <input type=\"checkbox\" name=\"emailNewsletter\" value=\"Yes\" ".get_checked($emailNewsletter, 'Yes')."> <input type=\"checkbox\" name=\"emailInbox\" value=\"Yes\" ".get_checked($emailInbox, 'Yes')."> $emailNewsletter = isset($_POST['emailNewsletter']) ? filter_input(INPUT_POST, 'emailNewsletter') : 'No'; $emailInbox = isset($_POST['emailInbox']) ? filter_input(INPUT_POST, 'emailInbox') : 'No'; If no submit button clicked display mysql information $userInfo['emailInbox'] instead of 'NO' . How to do that? Quote Link to comment https://forums.phpfreaks.com/topic/289209-php-checkbox-help/ Share on other sites More sharing options...
ginerjm Posted June 19, 2014 Share Posted June 19, 2014 You need to show more code. Your question mentions MySQL info which we have no idea about; your html snippet is rather incomplete so as to make it hard to fully interpret for correctness; and your mention of a submit button doesn't show us any code that defines it nor processes it. Besides that if there truly is no submit button clicked then hih do you even get to your php script??? Quote Link to comment https://forums.phpfreaks.com/topic/289209-php-checkbox-help/#findComment-1482901 Share on other sites More sharing options...
davidolson Posted June 20, 2014 Author Share Posted June 20, 2014 <?php global $db, $configs, $userInfo; function get_checked($variable, $status){ if($variable == $status){ return 'checked="checked"'; }else{ return ''; } } if(isset($_GET['action']) and $_GET['action'] == 'editEmailAlerts'){ $emailNewsletter = isset($_POST['emailNewsletter']) ? filter_input(INPUT_POST, 'emailNewsletter') : 'No'; $emailInbox = isset($_POST['emailInbox']) ? filter_input(INPUT_POST, 'emailInbox') : 'No'; if(!empty($_POST['submit'])){ $updateUserQuery = 'UPDATE users SET emailNewsletter = :emailNewsletter, emailInbox = :emailInbox WHERE username = :username'; $updateUser = $db->prepare($updateUserQuery); $updateUser->bindParam(':emailNewsletter', $emailNewsletter, PDO::PARAM_STR); $updateUser->bindParam(':emailInbox', $emailInbox, PDO::PARAM_STR); $updateUser->bindParam(':username', $userInfo['username'], PDO::PARAM_STR); $updateUserSuccess = $updateUser->execute(); if($updateUserSuccess){ $successMessage = $lang['success']['emailAlertsHasBeenEdited']; }else{ $errorMessage = $lang['error']['databaseError']; } } if($configs['showPageName'] == 'Yes'){ print" <div class=\"pageName\">{$lang['global']['page']} ⇒ {$lang['global']['changeEmailAlerts']}</div>"; } if(!empty($successMessage)){ print" <div class=\"successMsgBox\"> <div class=\"title\">{$lang['success']['successMessage']}</div> <div class=\"text\">{$successMessage}</div> </div>"; } if(!empty($errorMessage)){ print" <div class=\"errorMsgBox\"> <div class=\"title\">{$lang['error']['errorMessage']}</div> <div class=\"text\">{$errorMessage}</div> </div>"; } print" <form action=\"\" method=\"POST\"> <table style=\"width:100%\" class=\"defaultTable\"> <tr> <td style=\"width:30%;font-weight:bold\">{$lang['global']['emailNewsletter']}</td> <td style=\"width:70%\"><input type=\"checkbox\" name=\"emailNewsletter\" value=\"Yes\" ".get_checked($emailNewsletter, 'Yes')."></td> </tr> <tr> <td style=\"font-weight:bold\">{$lang['global']['emailInbox']}</td> <td><input type=\"checkbox\" name=\"emailInbox\" value=\"Yes\" ".get_checked($emailInbox, 'Yes')."</td> </tr> <tr> <td colspan=\"2\" style=\"text-align:center\"><input type=\"submit\" name=\"submit\" class=\"button\" value=\"{$lang['button']['editEmailAlerts']}\"></td> </tr> </table> </form>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/289209-php-checkbox-help/#findComment-1482930 Share on other sites More sharing options...
cyberRobot Posted June 20, 2014 Share Posted June 20, 2014 It looks like you're already testing for when the form is submitted, so you could use an else: if(!empty($_POST['submit'])){ //... //ELSE...FORM WASN'T SUBMITTED } else { $emailInbox = $userInfo['emailInbox']; } Also, it's recommended that you show all errors when developing PHP scripts. You can do that by adding the following to the top of your script: <?php //REPORT ALL PHP ERRORS error_reporting(E_ALL); ini_set('display_errors', 1); ?> Of course, you'll want to remove the code to show errors before going live with the page. Quote Link to comment https://forums.phpfreaks.com/topic/289209-php-checkbox-help/#findComment-1482933 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.