Help with AJAX, PHP code

Study Force

Participant
Joined
Oct 29, 2012
Messages
90
I have the following code that changes something in the database via AJAX. When the input checkbox is checked, the database variable updates, and the message "Your Profile Has Been Updated!" appears to the end-user. It currently is embedded in a PHP file, hence all the backslashes.

Code:
<script type="text/javascript">
function processForm() { 
$.ajax( {
    type: \'POST\',
    url: \'\php_file.php\',
    data: { hide_info : $(\'input:checkbox:checked\').val()},

    success: function(data) {
        $(\'#message\').html(\'<div>Your Profile Has Been Updated!</div>\');       
    }
} );
}
</script>

<input type="checkbox" name="hide_info" value="1" onclick="processForm()">
<span id="message"></span>

In php_file.php:

Code:
$checkbox = intval($_POST['hide_info']);

    if($checkbox == 1){
        $check_status = 1; 
    }else{
        $check_status = 0;  
    }

mysql_query("UPDATE users SET hide_info = $check_status WHERE userID = 1");

What I would like to happen is to get two different messages: one for when the checkbox is checked by the end-user, and a different one for when the checkbox is unchecked. For example, when it is checked, it says:

* Your Profile Has Been Updated!
* Your Profile Hasn't Been Updated!

In other words, the checkbox act as a toggle switch with a different message depending on its activation status.

Any suggestions?
 

kolakube

Participant
Joined
Apr 23, 2018
Messages
82
How far has this script gotten you? Any errors, or behaviors that happen when you click the checkbox? From this code it seems like you're on the right track.

But, be careful not to insert a div (block element) from JS into your HTML span (inline element) as that is invalid markup.
 
Top