3

I have found this: http://code.google.com/p/jquery-json, a plugin that emulates json_encode PHP function.

So I tried this:

var datasend = {};
    datasend['name'] = $(".chat_username").val();
    datasend['message'] = $(".chat_text").val();
    encoded_send = $.toJSON( datasend );
    $.ajax({
      type: "POST",
      url: "/apps/chat_write",
      data: {send_data: encoded_send },
      success: function(response){
        if(response!="OK"){
            alert("Παρουσιάστηκε πρόβλημα.Κάντε ανανέωση την σελίδα ή προσπαθήστε ξανά αργότερα.");
        }else{
            $(".chat_text").val("");
            $(".chat_count").val("0/100");
        }
      }
    });

The first problem?I have this

    $.ajax({
      type: "POST",
      url: "/apps/chat_write",
      data: "name="+escape($(".chat_username").val())+"&message="+escape($(".chat_text").val()),
      success: function(response){
        if(response!="OK"){
            alert("Παρουσιάστηκε πρόβλημα.Κάντε ανανέωση την σελίδα ή προσπαθήστε ξανά αργότερα.");
        }else{
            $(".chat_text").val("");
            $(".chat_count").val("0/100");
        }
      }
    });
    return false;
});

And i have problems with greek characters.. so i decidied to use json object If this code is correct how can I parse the JSON object from PHP? If it's not correct where are the errors?

Thanks

12
  • Use the json_decode PHP function. Your code makes no sense though, because you're declaring a variable encoded_send, and not using it. Then, you're using JSON.stringify(jsonObjects) without defining jsonObjects.
    – Rob W
    Dec 31, 2011 at 11:32
  • json_decode($_POST['send_data']); doesn't work
    – Chris P
    Dec 31, 2011 at 11:33
  • oupssss... JSON.stringify(encoded_send)
    – Chris P
    Dec 31, 2011 at 11:35
  • 1
    Fyi, Arrays do not have named keys. You want datasend = {}; (which is equal to new Object()) instead! And there is no reason to use a "jquery json plugin" since it's completely DOM or jQuery-unrelated. The preferred JSON parser is github.com/douglascrockford/JSON-js/blob/master/json2.js - it especially has a fallback to builtin functions if available. Dec 31, 2011 at 11:37
  • encoded_send is already JSON I assume. Why would you want to encode it again? Dec 31, 2011 at 11:38

1 Answer 1

1

(before I continue to your function..)
You seem to misunderstand the purpose of JSON. It's not necessary to replace the data with a JSON string when transmitting data to a server. A much easier approach is adding parameters through data, and retrieving them through $_POST.

Example:

$.ajax({
    type: "POST",
    url: "/apps/chat_write",
    data: {
        name: $(".chat_username").val(),
        message: $(".chat_text").val()
    },
    dataType: "json", /* This parameter is ONLY used at the response */
    success: function(response){
        //response is a deserialized JSON string
    }
});

Simple PHP example:

<?php
    header('Content-Type: application/json');
    $example = array("what" => "This");
    echo json_encode($example); //Echos: {"what":"This"}
?>

JSON should be used in a server respons. PHP objects can be serialized using json_encode. Make sure that you use header('Content-Type: application/json'); before sending any output.

(if you still want to JSON-encode data before sending)
Use the json_decode PHP function to deserialize a JSON string.

Your current code has to be fixed though.

var datasend = {};
datasend['name'] = $(".chat_username").val();
datasend['message'] = $(".chat_text").val();
var encoded_send = $.toJSON( datasend );
$.ajax({
  type: "POST",
  url: "/apps/chat_write",
  data: {send_data: encoded_send },
  dataType: "json", /* Can be removed if the RESPONSE is not JSON*/
  success: function(response){
    if(response!="OK"){
        alert("Παρουσιάστηκε πρόβλημα.Κάντε ανανέωση την σελίδα ή προσπαθήστε ξανά αργότερα.");
    }else{
        $(".chat_text").val("");
        $(".chat_count").val("0/100");
    }
  }
});
3
  • Btw, I don't think the MIME type has to be overridden, jQuery will still encode the data as x-www-form-urlencoded: send_data={json_here}. Dec 31, 2011 at 11:44
  • Now i take this error: <br /> <b>Fatal error</b>: Cannot use object of type stdClass as array in <b>C:\xampp\htdocs\apps\chat\chat_write.php</b> on line <b>4</b><br />
    – Chris P
    Dec 31, 2011 at 11:48
  • Try this PHP code in conjunction with my suggested JavaScript code: pastebin.com/NgdDxmgN
    – Rob W
    Dec 31, 2011 at 11:54

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.