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); })
If you open the page directly in the browser
"../cliente/getMenu.php"
what comes to you?– Sergio
What is the need to create this menu via JS?
– Kenny Rafael
echo '<a href="#" onclick="addTab('.$line['tabs'] .','. $line['Name'] .', '.$line['Url'] .')">'. $line['Name'] . '</a>';
– Rubens Bispo
What’s wrong? Add to question.
– user28595
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
Bacco, first of all thank you, I made the changes as mentioned but still gives error.
– Rubens Bispo
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.
– Bacco
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'])."
oraddTab('".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
Bacco, thank you very much, thanks very much..., I managed to rewrite the line again and it worked.
– Rubens Bispo