SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Parse Json data in php

    Hi,
    I have a json output like below, How can i parse this values in a php file ? Thanks in advance

    {"participants":[{"NoPrticiField":1,"empname":"gfh","designation":"fh","email":"fgh"},{"NoPrticiField":2,"empname":"fgh","designation":"fgh","email":"fgh"}]}:

  2. #2
    SitePoint Addict bronze trophy
    Join Date
    Sep 2005
    Posts
    318
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $json json_decode($output); 
    If I remember correctly...

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Patche,

    I tried this, But it's not working, Below is the jquery.ajax i am using

    $.ajax({
    type: 'POST',
    url: 'data11.php',
    data: { json: ko.toJSON(viewModel) },


    success: function (result) {
    alert(result);

    }

    });

  4. #4
    SitePoint Addict bronze trophy
    Join Date
    Sep 2005
    Posts
    318
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Can you paste any error messages or output from the page to here?

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In my below code, echo is giving This Error "Catchable fatal error: Object of class stdClass could not be converted to string"

    $value = json_decode($_POST['json']);
    echo($value);

  6. #6
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,412
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by salimali View Post
    In my below code, echo is giving This Error "Catchable fatal error: Object of class stdClass could not be converted to string"

    $value = json_decode($_POST['json']);
    echo($value);
    Using the JSON from your first post as an example, calling json_decode will give you back an object, which you can access like this:
    PHP Code:
    echo $value->participants[0]->empname;
    // Outputs: gfh 
    If you want to check the contents of $value, you can call var_dump($value);

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks fretburner, Patche... It works ..

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have 2 variables, var selectPartiNo = '2'; and var event_id = "4"; , I am currently passing it's value into my php file like url: 'data11.php?event_id=' + event_id + '&selectPartiNo='+selectPartiNo+'', ... Is there any other method to pass these variable values ???

    Code:
            processClick: function () {
                var selectPartiNo = '2';
                var event_id = "4";
                ko.applyBindings(viewModel, $("#registrationWrap")[0]);
    
                $.ajax({
                    type: 'POST',
                    url: 'data11.php?event_id=' + event_id + '&selectPartiNo='+selectPartiNo+'',
                    data: { json: ko.toJSON(viewModel) },
                    eventID:"+event_id+",
                         
                    success: function (result) {
                        alert(result);
                        
                    }
    
                });
    
            }
    In data11.php code is like below
    Code:
    echo "Event id   : ".$_GET['event_id']."<br>";
    echo "Selecte Number  : ".$_GET['selectPartiNo']."<br>";

  9. #9
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,412
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    I'm not sure if this is what you're asking, but you could pass the variables by POST, along with the json string:

    Code JavaScript:
    $.ajax({
        type: 'POST',
        url: 'data11.php',
        data: { 
        	json: ko.toJSON(viewModel), 
        	event_id: event_id,
        	selectPartiNo: selectPartiNo
    	}, 
        success: function (result) {
            alert(result);
        }
    });

    PHP Code:
    echo "Event id   : " $_POST['event_id'] . "<br>";
    echo 
    "Selected Number  : " $_POST['selectPartiNo'] . "<br>"
    Note that it would be a good idea to sanitize any POST/GET variables before using them in your script.


Tags for this Thread

Bookmarks

Posting Permissions

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