0

I'm sending this array using ajax

code jquery:

$("#btnReact").on("click", function (e) {
    var post = {};
    post["comment"] = $("#bug_message").val();
    post["id"] = $(this).data("bid");


    var request = $.ajax({
        url: "ajax/save_comment.php",
        type: "POST",
        data: {
            post: post
        },
        dataType: "json"
    });

    request.done(function (msg) {
        if(msg.status == "success") {

        }
    });

    request.fail(function (jqXHR, textStatus) {
        alert("Request failed: " + textStatus);
    });

    e.preventDefault();
});

but I can't reach my data in php and I keep getting errors when i'm trying to send this data to my class.

code php:

if(isset($_POST["post"]))
    {
        try
        {
            $comment = $_POST['post']["comment"];
            $id = $_POST['post']["id"];


            $comment = new Comment();
            $comment->Comment = $comment;

            $comment->SaveComment($id);
            $feedback['status'] = "success";
        }
        catch(Exception $e)
        {
            $feedback['message'] = $e->getMessage();
            $feedback['status'] = "error";

        }
        header('Content-Type: application/json');
        echo json_encode($feedback);
    }

Is there something wrong with my syntax or is it something else?

9
  • JavaScript errors or PHP errors? Commented May 16, 2013 at 19:41
  • 5
    "I keep getting errors..." Is there any chance that we could get them, too? Commented May 16, 2013 at 19:42
  • I feel like you are missing a json_decode , but I don't have access to a PHP server to be sure Commented May 16, 2013 at 19:45
  • @JasonSperske: Why would he need json_decode? Commented May 16, 2013 at 19:45
  • 1
    @J.Bruni: jQuery will encode the object into a query string, which becomes an array in PHP; that is correct. Commented May 16, 2013 at 19:49

2 Answers 2

1

why don't you just post the object rather than object inside an object in data option

 var request = $.ajax({
   url: "ajax/save_comment.php",
   type: "POST",
   data: post,
   dataType: "json"
 });

and take it as

if(isset($_POST["comment"]) && isset($_POST["id"]))
{
    try
    {
       $comment=$_POST['comment'];
       $id = $_POST["id"];
       ......
Sign up to request clarification or add additional context in comments.

2 Comments

While a valid suggestion, how is this an answer?
i don't know... the data option looked weird though it might not be the issue but still i guess it is better to infrom the OP about the other simpler ways...so i posted..:).. and i think it would be hard to mention all this in comment....
0

try this :

    jQuery(document).ready(function(){
    $("#btnReact").on("click", function(e){
        var post = {};
        post["comment"] = $("#bug_message").val();
        post["id"] = $(this).data("bid");


        var request = $.ajax({
      url: "ajax/save_comment.php",
      type: "POST",
      data: {post : post},
      dataType: "json"
    });

    request.done(function(msg) {
        if(msg.status == "success"){

        }
    });

    request.fail(function(jqXHR, textStatus) {
      alert( "Request failed: " + textStatus );
    });

    e.preventDefault();
    });
    });

the only difference is that i put your code inside jQuery(document).ready(function(){});

2 Comments

It's an external script and is working just fine without adding document ready
@MarnixVerhulst: So, then what's the problem?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.