views:

555

answers:

2

Hello, my question is: How can my php script send json type data and received back into the success or complete function?

I was trying to get this chatfunction to work on my website Because it diddn't work, I created a minimized portion off the code to check if it had something to do with the json method.

I only tested if I could get a sessionname back after the phpscript was proccessed What I get back is "undefined" instead of "johndoe".

I have no idea what could be the problem. Obviously, the script has worked fine for others, if you see the comments on the creators page.

this is my testingcode

<?php
session_start(); 
$_SESSION['username'] = "johndoe" ;// Must be already set
?>

<script type="text/javascript" src="includes/jquery.js"></script>
<script language="JavaScript">
$(document).ready(function(){
 $("#testjson").click(function(e){
 startJsonSession();

    return false;
    });


function startJsonSession(){  
    $.ajax({
     url: "jsontest.php?action=startjson",
     cache: false,
     dataType: "json",
     complete: function(data) {
      username = data.username;
      alert(username);
     }

    });
}


}); 
</script>

<?php
//the php script

if ($_GET['action'] == "startjson") { startjsonSession(); } 



function startjsonSession() {
    $items = '';


    /*if (!empty($_SESSION['openChatBoxes'])) {
     foreach ($_SESSION['openChatBoxes'] as $chatbox => $void) {
      $items .= chatBoxSession($chatbox);
     }
    }


    if ($items != '') {
     $items = substr($items, 0, -1);
    }*/

header('Content-type: application/json');
?>
{
     "username": "<?php echo $_SESSION['username'];?>",
     "items": [
      <?php echo $items;?>
        ]
}

<?php


    exit(0);
}

?>

thanks, Richard

+1  A: 

Richard, you should look into the json_encode() function in PHP. It will convert your array to JSON quickly, and keep you from having to deal with the smaller nuances of JSON syntax with large amounts of data.


Update: Modified Code

<?php

    session_start(); 
    $_SESSION['username'] = "johndoe" ;// Must be already set

?>

<script type="text/javascript" src="includes/jquery.js"></script>
<script language="JavaScript">
$(document).ready(function(){

    $("#testjson").click(function(e){
     startJsonSession();
        return false;
    });

    function startJsonSession(){  
        $.ajax({
         url: "jsontest.php?action=startjson",
         cache: false,
         dataType: "json",
         complete: function(data) {
          username = data.username;
          alert(username);
         }

        });
    }

}); 
</script>

<?php

    if ($_GET['action'] == "startjson") { 
      startjsonSession(); 
    } 

    function startjsonSession() {
        $items = '';

        print json_encode(array(
      "username" => "bob",
      "items" => array(
       "item1" => "sandwich",
       "item2" => "applejuice"
      )
     ));
    }
?>
Jonathan Sampson
yes, I already looked into that a bitbut before I have to change the creators code, I was wondering if there could be something else going on that causes this.I really don't look forward to change his code because I do not completely understand it. With the heredoc syntax and more
Try a JSON format like this: {"a":1,"b":2,"c":3,"d":4,"e":5} - So your code would be {"username": "bob","items": "apples"} (Go ahead and leave the hard-coded values for testing)
Jonathan Sampson
Also, pull that header() method out for now - comment it out.
Jonathan Sampson
One second...your formatting has me a bit confused. Let me clean it up on my screen and take a second look...
Jonathan Sampson
Richard, I made some changes - it works locally for me. Check my post for updated code.
Jonathan Sampson
I saw you used the php function. I believe you when you say it works, but could you take a look at the authors code. I commented it out on my testpage sourcecode for you to look at. What I am trying to say is, am I going to get into a lot off trouble if I ajust this code with your method, because he uses heredoc, etc
A: 

this is my testpage, by the way

just looked at your test page and the response is empty, for some reason the php side is broken :-(
David Archer