Error parse Json php

Asked

Viewed 90 times

1

I have an error when mounting a dynamic menu with data coming from mysql.

Code that mounts the menu:

<div class="easyui-accordion" data-options="multiple:true"  style="width:100%;">

  <?php
  $pdo = TConnection::open('teste');
  $sql = "select distinct a.IdMenu,a.descricao as Nome
        from Menu a inner join SubMenu b on b.IdMenu=a.IdMenu
        inner join Camada c on c.idCamada=b.IdCamada
        inner join CamadaOrganizacao d on d.IdCamada = c.IdCamada
        inner join CamadaUsuario e on e.IdCamada = c.IdCamada 
        where e.IdUsuario = ? order by a.descricao asc";
  $cat = $pdo->prepare($sql);
  $cat->execute(array($user));
  if ($cat->rowCount() >= 1):
    echo '<div class="easyui-accordion" style="width:100%;">';

    foreach ($cat->fetchAll(PDO::FETCH_ASSOC) as $res) :
      $id = $res['IdMenu'];
      echo '<div title="' . $res['Nome'] . '" data-options="iconCls:\'icon-ok\'" style="overflow:auto;padding:0;">';

      $sql2 = "select IdSubMenu , '#principal' as abas,IdMenu, Descricao as Nome ,url from SubMenu  where IdMenu = ? order by Nome asc";
      $subMenu = $pdo->prepare($sql2);
      $subMenu->execute(array($id));

      if ($subMenu->rowCount() >= 1):
        echo '<ul class="easyui-datalist" title="" lines="true" style="width:100%;">';
        foreach ($subMenu->fetchAll(PDO::FETCH_ASSOC) as $linha) :
          echo '<li>';
          echo '<a href ="#"  onclick="addTab('.$linha['abas'].','.$linha['Nome'] .','. $linha['url'] .');" >'. $linha['Nome'] .'</a>';
          //echo '<a href="#" onclick="addTab('.$linha['abas'].'","'.$linha['Nome'] . '","' . $linha['url'] . ')">' . $linha['Nome'] . '</a>';

          //echo '<a href="'.$linha['url'] . '">' . $linha['Nome'] . '</a>';
          echo '</li>';
        endforeach;
        echo '</ul>';
      endif;

      echo '</div>';
    endforeach;
    echo '</div>';
  endif;
  ?>
</div>

javascript function that opens the page in a div.

function addTab(obj,title, url) {

  if ($(obj).tabs('exists', title)) {
    $(obj).tabs('select', title);
  } else {
    var hDiv = 0;
    hDiv = $("#MenuLateral").height();
    $(obj).height(hDiv);
    var content = '<iframe name="conteudo" id="conteudo" scrolling="auto" frameborder="0"  src=' + url + ' style=""></iframe>';
    $(obj).tabs('add', {
      title: title,
      content: content,
      closable: true
    });
  };
};

But when clicking on the link of the following error, and I could not identify:

Uncaught Syntaxerror: Invalid or Unexpected token. (Function(Event){addTab(#main,Organization,. /Organization.php); })

Someone can give me a light ? inserir a descrição da imagem aqui

  • 3

    If you open the page directly in the browser "../cliente/getMenu.php" what comes to you?

  • 1

    What is the need to create this menu via JS?

  • echo '<a href="#" onclick="addTab('.$line['tabs'] .','. $line['Name'] .', '.$line['Url'] .')">'. $line['Name'] . '</a>';

  • What’s wrong? Add to question.

  • Missing quotes in call parameters addTab, the correct is something like this: echo "<a href =\"#\" onclick=\"addTab('".$linha['abas']."','".$linha['Nome']."','".$linha['url']."');\" >". $linha['Nome'] ."</a>";

  • Bacco, first of all thank you, I made the changes as mentioned but still gives error.

  • Well, now all that’s missing is a little adjustment, because the error has already changed more towards the end of the line. If you could paste a chunk of the generated code (using the browser view-source) it is easier to see. And always paste as text, never as image.

  • Ah, and it has some special character there, you might have to put a urlencode( ) around the $line[ ] to avoid this: addTab('".urlencode($linha['abas'])." or addTab('".htmlentities($linha['abas'])." in all fields (you need to see how this data is coming, and what is the intention in the call to see which is the most suitable)

  • Bacco, thank you very much, thanks very much..., I managed to rewrite the line again and it worked.

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.