Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2011
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question AJAX: Send JSON to PHP code from Javascript

    I'm new to CodingForums.com, so hello everyone!

    I have some experience in javascript and php, but I am new to AJAX.
    Here is the issue:

    I have created a JSON object in javascript. I would like to send that object to a php file, have the php code process the JSON as an array, then send back a JSON object for the javascript to process.

    After many hours of frustrated attempts, I ended up just copying a code from tutorial I found at http://www.prodevtips.com/2008/08/15...d-json_decode/

    Still, nothing.

    Here is my code copied from the tutorial:
    Code:
    // Javascript file 
    <head>
    <SCRIPT src="../../includes/jquery.min.js"></script>
    <SCRIPT src="../../includes/jquery.json.min.js"></SCRIPT> 
    <script>
    $(document).ready(function(){
      var data = new Object();
      data.hello = "Hello";
      data.world = 'World';
      data.worked = " it worked ";
      data.somebool = true;
      data.array = new Array("he\"ll\"o", '"World"');
      var dataString = $.toJSON(data);
      $.post('phpfile.php', {data: dataString}, function(res){
        var obj = $.evalJSON(res);
        if(obj.somebool === true)
          $("#result").html(obj.hello + ' ' + obj.array[1] + obj.worked + ". Message from PHP: "+obj.php_message);
      });
    });
    </script>
    </head>
    <body>
    <div id="result">This is not the text I am hoping to see...</div>
    </body>
    // phpfile.php
    PHP Code:
    <?php
    $res 
    json_decode($_REQUEST['data'], true);
    $res["php_message"] = "I am PHP";
    echo 
    json_encode($res);
    ?>
    I am about to give up, and just convert my JSON to a string. But the ability to send JSON objects back and forth through AJAX seems like such a powerful tool, especially since I'm doing a lot of work with SQL databases.

    I am using php v 5.2.16. I read there might be some compatibility issues with json_encode(), but only before php 5.2.....

    I appreciate the help!

  • #2
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Quote Originally Posted by schw0722 View Post
    I am about to give up, and just convert my JSON to a string.
    I'm not sure what you mean by that. Your JSON string is a string.

    Anyway, can you show this on a live site? There are so many things that could potentially fail, so it's hard to tell you anything without seeing it in action.

  • #3
    New Coder
    Join Date
    Feb 2011
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Live Site

    Sorry, what I meant was that I could turn the data object into a string separated by ",". For example:

    Code:
    dataArray = new Array();
    for (var prop in data) {
    dataArray.push(data[prop]);
    }
    dataString = dataArray.join(",")
    Then in the php code, separate the dataString back into an array, and work with it like that. This doesn't allow me to send the same complexity of information to the php code, but it's the compromise I'm working with for now...

    BTW, I'm having no trouble receiving JSON objects back from the php.

    You can see a live version of the unsuccessful code here: here
    Last edited by schw0722; 02-22-2011 at 05:18 PM.

  • #4
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    The problem is definitely in the PHP. The Javascript component sends and receives ok, but PHP isn't sending back the extended object, but a new one only containing "php_message".

    Your PHP code looks ok though, so could you please do a var_dump on $_REQUEST (it would be better to use $_POST, btw), on $res (before and after adding the additional index), and on json_encode($res)? At one of those steps, something is failing.

  • Users who have thanked venegal for this post:

    schw0722 (02-22-2011)

  • #5
    New Coder
    Join Date
    Feb 2011
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Smile Fixed

    I took a look at the php code, did a little more research, and I think I figured it out.
    Apparently magic quotes are set on my server. The solution was just to add a stripslashes() tag.

    Here is my working php code:
    PHP Code:
    $res json_decode(stripslashes($_POST['data']), true); 
    $res[php_message] = "I am PHP"

    echo 
    json_encode($res); 
    Thanks for the help!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •