chi
chi

Reputation: 357

ajax multiple data sent parameters php

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?

Upvotes: 1

Views: 60

Answers (2)

Benaya Paul
Benaya Paul

Reputation: 59

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

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

Upvotes: 0

rad11
rad11

Reputation: 1561

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"

Upvotes: 1

Related Questions