I'm trying to check if the checkbox has been checked and display the check mark for the user to see when they check there account settings. I want to know how can I fix this problem using PHP so that the check mark is displayed every time the user views their account settings?
Here is the HTML.
<input type="checkbox" name="privacy_policy" id="privacy_policy" value="yes" />
Do you mean which attribute do you set so that the checkbox is checked?
<input type="checkbox" name="privacy_policy" id="privacy_policy" value="yes" checked="checked" />
EDIT: What I would do, is bind some code to the checkbox's click event (using a library like jQuery), like the following:
$(document).ready(function() {
$("#privacy_policy").click(function() {
// block the checkbox
$(this).attr("disabled", "disabled");
var checked = 0;
if($(this).is(":checked")) {
checked = 1;
}
// send the checked state to PHP script
// if the update has been successful, the script echos a '1'
$.post('account.php', { checked: checked, function(resp) {
// update has been successful
if(resp == 1) {
// unblock the checkbox, the current
// checked state reflects the stored value
$(this).removeAttr("disabled");
} else {
alert("settings could not be updated");
}
});
});
});
That will handle the real-time manipulating of that particular option's setting every time the checkbox is clicked. (I think that's what you meant, anyway). You just need to act on the $_POST['checked']
variable internally.
To set up the initial state of the checkbox based on a stored value (the first time the page loads), just use short tags, e.g.:
<?php
$isChecked = $_SESSION['someSetting']; // 1 or 0, for example
?>
<input type="checkbox" name="privacy_policy" id="privacy_policy" value="yes" <?php if($isChecked) echo 'checked="checked"'; ?>/>
I'm not entirely sure what your question is. If you know in PHP whether the checkbox should be checked, you can do something like
echo '<input type="checkbox" name="privacy_policy" id="privacy_policy" value="yes"';
if ($somevar) echo ' checked="checked" ';
echo ' />';
This will make the checkbox default to being checked if $somevar
is true.
<?php
$checked = $someBoolean ? ' checked="checked"' : '';
?>
<input type="checkbox" name="privacy_policy" id="privacy_policy" value="yes"<?php echo $checked; ?> />
Where $someBoolean
holds a boolean (true/false) value whether it was checked before or not (value retrieved from a database for instance).
If true
it will result in:
<input type="checkbox" name="privacy_policy" id="privacy_policy" value="yes" checked="checked" />
else:
<input type="checkbox" name="privacy_policy" id="privacy_policy" value="yes" />
I assume you have the user's settings stored in a database of some description. Assuming this is MYSQL do the following:
$query = "
SELECT privacy_policy FROM user_settings WHERE user_id = $user_id
";
$result = mysql_query($query);
$user_settings = mysql_fetch_assoc($result);
This assumes that the information is stored a column called "privacy_policy" in a table called user_settings and that you already know the $user_id. The privacy policy setting is now held in $user_settings['privacy_policy']
. I'm also assuming that it is a boolean where true means you want it checked and false means you don't want it checked.
You might also instead be getting the setting from a user input, such as after just changing the variable. In this case, just find the variable in your $_POST or $_GET e.g. $_POST['privacy_policy'] and continue with that variable instead.
So now do the following:
if($user_settings['privacy_policy']) $checked = "checked='checked'"; else $checked = ''; echo "';
If $user_sesstings['privacy_policy'] is true, $checked will be set and echoed along with the rest of the checkbox. Otherwise, the checkbox will be echoed without the attribute and so will be unchecked.