开发者

using and displaying object data, PHP and json/jquery

I am running into a brick wall when passing object data from jquery to PHP. I'm still trying to get my head around OOP.

Code follows:

        <script type ="text/javascript">
      $(function() { 


    $(".button").click(function() {
          //alert("click");


          var jsonvar1 = {"skillz": {
                            "web":[
                                    {"name": "html", 
                                     "years": "5"
                                    },
                                    {"name": "css", 
                                     "years": "3"
                                    }],
                            "database":[
                                    {"name": "sql", 
                                     "years": "7"
                                    }]
        }};



          $.ajax({
            url: "test2.php",
            type: 'POST',
            data: 'regemail=' + jsonvar1,



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

                },
                error:function (xhr, ajaxOptions, thrownError){
                alert(xhr.status);
                alert(thrownError);
            }
            });

      });
      });


        </script>
    </head>
    <body>

<input type="button" id="regSubmit" class="button" value="Register!" /></br></span>
<script>
$('input[type=button]').attr('disabled', false);
    //alert('test');
</script>  

The above code does 3 things. Catches a button click, and then: eclares a variable (jsonvar1), and performs an ajax request on that variable to a PHP backend.

Now the code in the PHP backend:

if (filter_has_var(INPUT_POST, "regemail")) {开发者_JAVA技巧

    $data = $_REQUEST["regemail"];

    //echo "<br>I got some data!</br>";

    //print_r($data);

    //echo $data->skillz;

    //echo $data;

    var_dump($data);

} else {

echo "No Data.";
}

(safely ignore all the echoes and dumps in the above PHP. That would be me flailing about trying to use the data in some fashion)

Question: How can I pull data from the object in PHP into variables or an array, or if you prefer, how can I work directly with the values in that object? (assuming it is an object, and that I'm not making some other unrelated mistake)

I'm wondering if I've neglected to tell my request that it is JSON... http://api.jquery.com/jQuery.getJSON/

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Edit:

I've attempted to add echo json_decode($data); to my PHP code, but it returns a blank dataset.

I also attempted to put dataType: 'json', in my ajax query.

Still not having any luck it seems.


You have run into a rather simple error which is easy to overlook: jsonvar1 contains an object, and when you concatenate it to "regemail=" with the + operator, it is turned into a string - but not in the way you intended. The resulting string is "[object Object]" because JSON conversion is not done automatically for you. Instead, the line should be

data: 'regemail=' + JSON.stringify(jsonvar1),

In your PHP file, the line

$data = json_decode( $data );

will give you an object to work with, as @sirlancelot has already said.


You'll want to use json_encode for that on the server side.

if (filter_has_var(INPUT_POST, "regemail")) {
    $data = $_REQUEST["regemail"];

    //echo "<br>I got some data!</br>";
    //print_r($data);
    //echo $data->skillz;

    echo json_encode($data);
} else {
    echo "No Data.";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜