dongtan9066 2010-04-07 23:30
浏览 45

PHP和MySQL检查了复选框问题

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" />
  • 写回答

4条回答 默认 最新

  • douwuying4709 2010-04-07 23:34
    关注

    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"'; ?>/>
    
    评论

报告相同问题?