开发者

Converting JSON to Array and getting the type

I have a 2 part question.

  1. How do I get my script to alert the variable type in javascript instead of [object object]?
  2. How do I get my Json to successfully convert to an Array without returning NULL?

Javascript:

$(document).ready(function() {

// Arrays
var title = $('.table-item-1 > strong').map(function () {
    var value = $(this).text();
        return value;
    }).get();

var name = $('[width|="23%"]').next('.table-item-1').map(function () {
    var value = $(this).text();
        return 开发者_Go百科value;
    }).get();

var phone = $('[width|="23%"]').next('.table-item-1').next('.table-item-1').map(function () {
    var value = $(this).text();
        return value;
    }).get();

var fax = $('[width|="23%"]').next('.table-item-1').next('.table-item-1').next('.table-item-1').map(function () {
    var value = $(this).text();
        return value;
    }).get();

var email = $('[width|="23%"]').next('.table-item-1').next('.table-item-1').next('.table-item-1').next('.table-item-1 , a').map(function () {
    var value = $(this).text();
        return value;
    }).get();

// Individual    
var brand = $('.panel-1 tr strong').html();
var corp = $('.panel-1 tr td :gt(0)').html();
var web = $('.panel-1 tr td ~ .focus :not(strong)').html();
var lna_code = $('.panel-1 tr td :last').html();
var lna_class = $('.panel-1 tr td :gt(4)').html();


var items = {
    'brand': brand,
    'corp': corp,
    'web': web,
    'lna_code': lna_code,
    'lna_class': lna_class,
    'title': title,
    'names': 
        name,
    'phones': 
        phone,
    'faxes': 
        fax,
    'emails': 
        email,
};

var data = $.toJSON(items);

$.ajax({
    type: 'POST',
    url: 'http://xxxxxx.com/xxxxx/PHP/excel.php',
    data: {'data':data},
    success: function(data) {
        alert('success');
    },
    complete: function(data){
        alert(data); // alerts [object object] , want a string
   }
});

});

PHP:

<?php
$json = $_POST['data'];
$result = json_decode($json);
$resultsType = gettype($result);
echo $resultsType;
?>

note: I'm using json.js for the $.toJSON(items); call


if you want the result to be an array on the PHP side, it should probably be an array on the javascript side:

[value1]

instead of

{key1 = value1}

what is happening in your code is you are instead creating an object. the object is probably what you actually want however as it does not seem like you have to iterate over the values on the PHP side, which would be the only reason for an actual array as opposed to an object.

if you leave the code as is, you access the values on the PHP side like so:

echo $result->brand;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜