简体   繁体   English

PHP通过json遗址数组到Jquery

[英]PHP to Jquery through json ruins array

I'm trying to perform some PHP code and then pass it's results to another PHP script through jquery. 我正在尝试执行一些PHP代码,然后通过jquery将其结果传递给另一个PHP脚本。 One of these results is an array, and I'm passing it to a GET so it gets to the other script. 其中一个结果是一个数组,我将它传递给GET,以便它到达另一个脚本。 (alot of work, but I can't have page reloads even tho I have to use PHP). (很多工作,但我不能有页面重新加载,即使我必须使用PHP)。

The problem occurs when I'm trying to put the PHP variable through JQuery. 当我试图通过JQuery放置PHP变量时,会出现问题。 What I have to do this for me is: 我必须为我做的是:

var _leafs = <?php echo json_encode($leafs); ?>;

When I run json_encode on $leafs and then print the result (all using PHP), it gives me a json array that has been successfully validated by JSONLint. 当我在$leafs json_encode上运行json_encode然后打印结果(全部使用PHP)时,它给了我一个json数组已成功验证的json数组。

When I use the above code and alert() the result it's missing the brackets and quotes. 当我使用上面的代码并alert()结果时,它缺少括号和引号。 Even weirder is when I pass it through like so: 甚至更奇怪的是当我通过它时:

$.get('set_fields.php?pt=' + _path + '&lf' + _leafs, function(data) {

The result is this: 结果是这样的:

" string(4) "
" 

Which shows up to be a <br> in my html reader. 在我的html阅读器中显示为一个<br>

Am I missing something when I'm converting it to json? 当我把它转换成json时,我错过了什么吗?

Additional code: 附加代码:

<?php
// Fetch the XML from the URL
if (!$xml = file_get_contents($_GET['url'])) {
    // The XML file could not be reached
    echo 'Error loading XML. Please check the URL.';
} else {
    // Get the XML file
    $dom = new DOMDocument();
    $dom->loadXml($xml);
    $xpath = new DOMXpath($dom);

    $paths = [];
    $leafs = [];
    foreach ($xpath->evaluate('//*|//@*') as $node) {
        $isLeaf = !($xpath->evaluate('count(@*|*) > 0', $node));
        $path = '';
        foreach ($xpath->evaluate('ancestor::*', $node) as $parent) {
            $path .= '/'.$parent->nodeName;
        }
        $path .= '/'.($node instanceOf DOMAttr ? '@' : '').$node->nodeName;
        if ($isLeaf) {
            $leafs[$path] = TRUE;
        } else {
            $paths[$path] = TRUE;
        }
    }

    $paths = array_keys($paths);
    $leafs = array_keys($leafs);

    echo "Choose a path<br><br>
            <form>
                <select id='field_dropdown'>";
                foreach($paths as $value) {
                    echo "<option value='".$value."'>".$value."</option>";
                }
    echo    "   </select>
                <button id='send_path'>Send path</button>
            </form>
            ";

}
?>

<script>
$(document).ready(function() {
$('#send_path').click(function() {
    var _path = $("#field_dropdown").val();
    // Get the leafs array and send it as a json string to set_fields.php
    var _leafs = <?php echo json_encode($leafs); ?>;
    $.get('set_fields.php?pt=' + _path + '&lf=' + _leafs, function(data) {
        $('#fields').append(data);
    }).error(function() {
        $('#fields').html('Error calling XML script. Please make sure there is no error in the XML file.');
    });
    return false;
});
});
</script>

And here the code where I want the json array to end up (and then get turned back into a PHP array). 这里是我希望json数组结束的代码(然后转回到PHP数组中)。

<?php
// Match all the fields to the values
$path = $_GET['pt'];
$leafs = json_decode($_GET['lf']);
$fieldLeafs = [];
$pathLength = strlen($path) + 1;
foreach ($leafs as $leaf) { if (0 === strpos($leaf, $path.'/')) { $fieldLeafs[] = substr($leaf, $pathLength); } }

var_dump($path."<br>");
var_dump($leafs."<br>");
?>

在构建get URI时,在lf查询参数之后添加=

$.get('set_fields.php?pt=' + _path + '&lf=' + _leafs, ...

What if you get the array through jquery instead of echoing it? 如果你通过jquery获取数组而不是回显它会怎么样?

<input id="hidden-value" value="<?php echo json_encode($leafs); ?>" />

and then 接着

var _leafs = $('#hidden-value').val();

Just write 'json' in the last parameter of get method: 只需在get方法的最后一个参数中写'json'

   $.get('set_fields.php?pt=' + _path + '&lf' + _leafs, function(data) {
        $('#fields').append(data);
    },'json')//<-- this
     .error(function() {
        $('#fields').html('Error calling XML script. Please make sure there is no error in the XML file.');
    });

你试过这个吗?

var _leafs = [<?php foreach($leafs as $leaf){ echo "'$leaf',"; } ?>]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM