Jump to content

php checkbox help


davidolson

Recommended Posts

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?

 

 

 

Link to comment
Share on other sites

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???

Link to comment
Share on other sites


<?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>";

}

?>

 

Link to comment
Share on other sites

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.  :happy-04:
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.