douliangbian7323 2014-03-11 12:37
浏览 35
已采纳

PHP通过json遗址数组到Jquery

I'm trying to perform some PHP code and then pass it's results to another PHP script through jquery. One of these results is an array, and I'm passing it to a GET so it gets to the other script. (alot of work, but I can't have page reloads even tho I have to use PHP).

The problem occurs when I'm trying to put the PHP variable through JQuery. 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.

When I use the above code and alert() the result it's missing the brackets and quotes. 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.

Am I missing something when I'm converting it to 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).

<?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>");
?>
  • 写回答

4条回答 默认 最新

  • douyan1896 2014-03-11 13:03
    关注

    What if you get the array through jquery instead of echoing it?

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

    and then

    var _leafs = $('#hidden-value').val();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀