0

During the process of trying to develop an checkbox activated mySQL query based on the value inside a checkbox i've incurred an problem which i can't get past, I've tried the following at the moment

<input type="checkbox" name="status" onclick="updateStatus(<? echo $data['id']; ?>,this.checked)">

The Javascript/Ajax code for the function "updateStatus" is as followed

function updateStatus(id,value) {
if (window.XMLHttpRequest) {
    http = new XMLHttpRequest()
} else if (window.ActiveXObject) {
    http = new ActiveXObject("Microsoft.XMLHTTP")
} else {
    alert("Your browser does not support XMLHTTP!")
}
http.abort();
http.open("GET", "../functions/ajax.php?check=update_status&id=" + id + "&checked="+value, true);
http.onreadystatechange = function () {
     if (http.readyState == 4) {
        alert(http.responseText);
     }
 }
 http.send(null)

The PHP function inside functions/ajax.php

if(isset($check) and $check == 'update_status' and isset($_GET['id'])){
    $id = mysql_real_escape_string($_GET['id']);
    $checked= mysql_real_escape_string($_GET['checked']);
if($checked == true) {
    echo "Checked";
} elseif($checked == false) {
    echo "Not checked";
} else {
    echo "Invalid response";
}

when using this code it always returned "Checked" any idea why ?

1

1 Answer 1

1

try this (code tested),

<input type="checkbox" name="status" onclick="updateStatus(<? echo $data['id']; ?>,this.checked)">

You will get second parameter as true or false;

function updateStatus(id,checked) {
if (window.XMLHttpRequest) {
    http = new XMLHttpRequest()
} else if (window.ActiveXObject) {
    http = new ActiveXObject("Microsoft.XMLHTTP")
} else {
    alert("Your browser does not support XMLHTTP!")
}
http.abort();
http.open("GET", "../functions/ajax.php?check=update_status&id=" + id+"checked="+checked, true);
http.onreadystatechange = function () {
     if (http.readyState == 4) {
        alert(http.responseText);
     }
 }
 http.send(null)

You PHP will be:

if(isset($check) and $check == 'update_status' and isset($_GET['id']) AND isset($_GET['checked'])){
$id = mysql_real_escape_string($_GET['id']);
$checked= mysql_real_escape_string($_GET['checked']);
if($checked == true) {
$query = mysql_query("SELECT * FROM `check_status` WHERE `user_id` = '$_SESSION[userid]' and `id` = '$id'");
else
 //some other query
//Then i can insert the query based on if the checkbox checked or not.
Sign up to request clarification or add additional context in comments.

6 Comments

He will also need to update the Javascript function to accept, and then pass the second parameter.
So my new code would be function updateStatus(id,value) { http.abort(); http.open("GET", "../functions/ajax.php?check=update_status&id=" + id + "&checked=" + value, true); http.onreadystatechange = function () { if (http.readyState == 4) { alert(http.responseText); } } http.send(null) }
@suresh-kamrushi when i do that it's returning true everytime?
<input type="checkbox" name="status" onClick="alert(this.checked)"> just see this html in browser and click on it. It will populate with true or false only. Best of luck
I will go ahead and update my current code to the question so you can understand what i mean, sorry for the miscommunication.
|

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.