-2

If I want to send an array from PHP to JavaScript, I do something like this with PHP:

<?php
    $json=array();
    $json['datetime']="Something";
    $json['timestamp']="Something else"
    $encoded=json_encode($json);
    die($encoded);
?>

And this on jQuery/JavaScript (using Ajax):

...
success: function(response){
var chat = jQuery.parseJSON(response);
datetime=chat['datetime'];
timestamp=chat['timestamp'];
...

I was about wondering about doing the opposite.

In jQuery i have this array:

data_send['username']=$(".chat_username").val();
data_send['message']=$(".chat_message").val();

I want to encode this array as a JSON object, send this object via Ajax, and then take this object from $_POST/$_GET and decode it to an array.

How can I do this?

Abbas
  • 6,720
  • 4
  • 35
  • 49
Chris P
  • 2,059
  • 4
  • 34
  • 68
  • you want to send data from the client to the server like [.post()](http://api.jquery.com/jQuery.post/)? – T I Dec 31 '11 at 14:02
  • @ChrisPappas why? There's a standard format for sending key-value pairs to CGI scripts, and it's _not_ JSON! – Alnitak Dec 31 '11 at 14:04
  • @Alnitak now i use something like `data: "name="+escape($(".chat_username").val())+"&message="+escape($(".chat_text").val()),` this doesn't work for special characters, and greek characters (utf-8 encoding) – Chris P Dec 31 '11 at 14:06
  • @Chris because you should be using a map, not a string - see my answer. – Alnitak Dec 31 '11 at 14:06
  • @ChrisPappas also check http://stackoverflow.com/a/2473382/1114171 if you still having problems basically suggests using [encodeURIComponent](http://www.w3schools.com/jsref/jsref_encodeuricomponent.asp) – T I Dec 31 '11 at 14:14
  • @Alnitak thanks, can't see how I missed that in such a small paragraph lol, I have removed the comment as it is misleading. – T I Dec 31 '11 at 14:20

1 Answers1

1

If you want to encode an array into JSON from Javascript you can use JSON.stringify(myarray).

However, you shouldn't do that to send it to a PHP script.

jQuery has built in support for passing a map of key-value pairs in a POST method - just pass it as the data parameter in $.ajax() or as the second parameter to $.post().

jQuery will then correctly URI encode any unsafe characters that appear (whether in keys, or values), so in your case you can use:

var data_send = {
     username: $(".chat_username").val(),
     message:  $(".chat_message").val()
};
$.post(url, data_send, success_handler);

// or $.ajax({url: url, data: data_send, ... });

PHP then has built in support for reading that map - it's $_POST:

<?php
    $username = $_POST['username'];
    $message  = $_POST['message'];
?>
Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • Then in php `$data_send=json_decode($_POST['data_send']);$username=$data_send['username'];` Am i right? – Chris P Dec 31 '11 at 14:16
  • @ChrisPappas no - you don't need to JSON encode it or decode it at all - that's the whole point of my answer. I've updated it with an example. – Alnitak Dec 31 '11 at 14:18
  • `
    Notice: Undefined index: username in C:\xampp\htdocs\apps\chat\chat_write.php on line 2

    Notice: Undefined index: message in C:\xampp\htdocs\apps\chat\chat_write.php on line 4
    `
    – Chris P Dec 31 '11 at 14:27
  • @ChrisPappas NB I've updated the sample code to make it cleaner - the original just copied a snippet from your own code which on its own was incomplete. – Alnitak Dec 31 '11 at 14:31
  • @ChrisPappas right - as expected you've made `data_send` an array, it should really be an object, per my updated code above. – Alnitak Dec 31 '11 at 14:32
  • your codes works..the insertion into my database is correct.. `$_POST['username']` doesn't have problem but `$_POST['message']` has.. The chat updates correctly (i see a new row with the correct username), but no message displayed – Chris P Dec 31 '11 at 14:46
  • $(".chat_text").. THx u so much!! – Chris P Dec 31 '11 at 14:51
  • @Chris it does help if your HTML and JS are self-consistent ;-) – Alnitak Dec 31 '11 at 15:01