1

I have check boxes that get generated dynamically, when the check box is checked the onchange method works fine and my function is called also when the box is unchecked by user the onchange method calls my intended function the problem is when the box is checked a part of the page becomes active which is good but when the user un-checks the checkbox the that part of the page becomes active again, and the value of the checkbox "checked " stays yes after the first time it was checked. Is there way to detect when the user choose to un-check so the active part of the page becomes inactive.

Could I send the check box status with out having submit in the form. Does it have to be submitted for post to work. Do I have to use Java script for this ?

I tried this i'm having issues

<script type="text/jscript">
//this funciton will be called when user checks a check box. 
function edit(){


     //get selected category from the form 
    //var formName = 'choose';
    //var Category = document[formName]['choose'].value;


    //check if browser suports ajax
    var xmlhttp = null;      
    if(typeof XMLHttpRequest != 'udefined'){
      xmlhttp = new XMLHttpRequest();
    }

    else if(typeof ActiveXObject != 'undefined'){
        xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
    }

    else 
        throw new Error('You browser doesn\'t support ajax');


    //open connection with activateImages.php to recieve the active images as an acho
    xmlhttp.open("GET", "activateImages.php",true);         

    //echeck if ready to recieve        
    xmlhttp.onreadystatechange = function (){

    if(xmlhttp.readyState == 4)
      window.activate(xmlhttp);
    };

    xmlhttp.send(null);
    }

    //recieve the active images then insert them in the specified location of the page. 
    function activate(xhr){

        if(xhr.status == 200){
            document.getElementById('images').innerHTML = xhr.responseText;

        }
        else 
            throw new Error('Server has encountered an error\n'+
            'Error code = '+xhr.status);

}

</script>
1

2 Answers 2

1

The change event handler isn't called until the checked state has been updated:

<label><input type='checkbox' onchange='handleChange(this);'>Checkbox</label>

function handleChange(cb) {
  console.log("Changed, new value = " + cb.checked);
}

Example

handleChange function will tell you when the value of checkbox will change in true or false. Use that to hide and show your div.

Sign up to request clarification or add additional context in comments.

1 Comment

I will be obliged if you can tick the answer. :)
0

My advice is, it is better to use a library like jQuery to do this kind of complex requests. The main reason is, it is cross browser compatible, and easier to implement. For the same logic of yours, you can do this way in jQuery.

First include the script in your header.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

The next thing is the AJAX GET Request and the function edit().

function edit(){
    $.get("activateImages.php", function(data) {
        $("#images").html(data);
    }).error(function(e){
        throw new Error('Server has encountered an error\n' + 'Error code = ' + e);
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.