1

whats wrong with this?

EDIT:

 $check = $row['publish'] == 1 ? 'true' : 'false';

It works when I want to unpublished, but if the checkbox is empty I cannot publised.

 OnClick="doAction(<?php echo $check;?>, <?php echo $id;?>);"



function doAction(check,id){
 $.ajax({

      type: "GET",
      url: "test.php",
      data: "check=" + check + "&id=" + id,
      success: function(msg){
                 alert( "Data Saved: " + msg );
               }
 });
}

and the file test.php:

$id = $_GET['id'];
        $check = $_GET['check'];
        if ($check == "false"){
            $query = mysql_query("update article set publish = 1 where id =" . $id);
            echo "Published";
        }
        else {
            $query = mysql_query("update article set publish = 0 where id =" . $id);
            echo "Unpublished";
        }

I cannot display the id in the test.php file.it gives me nothing. But in the doAction parameters are(.., id) so it's been sent but I don t receive it in the ajax call and then in file. Why?

4
  • Assigning $check as checked or unchecked. And, In test.php, condition in if is $check == "false". Why? Commented Jul 27, 2016 at 8:35
  • Try out this: $.ajax({ type: "GET", url: "test.php?check="+ check + "&id = " + id, success: function(msg){ alert( "Data Saved: " + msg ); } }); Commented Jul 27, 2016 at 8:37
  • then how to assign the $check? in test.php if I write echo $check it gives me the( id, false) if I unpublished. but when i click the checkbox to published only it checks but doesn;t return anything, neither id or check status. Commented Jul 27, 2016 at 8:39
  • the problem is with the data you are passing to your function doAction(check, id). check that first! Commented Jul 27, 2016 at 9:00

2 Answers 2

1

Try change:

data: "check=" + check + "&id = " + id,

To:

data: "check=" + check + "&id=" + id,

And you should define what will be response HTML , JSON etc. use for this example:

dataType: "JSON"
Sign up to request clarification or add additional context in comments.

6 Comments

doesn't work. the problem I think is with $check = $row['publish'] == 1 ? 'checked' : 'unchecked';. It doesn't work for unchecked.
Edit your code to current revision. And try change $row['publish'] == 1 ? 'checked' : 'unchecked'; to $row['publish'] == 1 ? 'true' : 'false'; because you try check that value not checked or unchecked. Pass to html
Show what u got in var_dump($_GET["id"]), var_dump($_GET["check"]); before if
now it works, it really works but it doesn;t remain in the checkbox the values. I mean if it's 1 in db it's not checked.
So change $row['publish'] == 1 ? 'true' : 'false'; to $row['publish'] == 1 ? 'false' : 'true'; I do not know what about you in the database represents the value 1 is you need to set an appropriate value to Your code to run what I gave must act
|
0

If you are passing the value in arguments use below code.

 OnClick="doAction('<?php echo $check;?>', '<?php echo $id;?>');"

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.