Jump to content

[SOLVED] php throwing off my javascript function


justAnoob

Recommended Posts

<SCRIPT TYPE="text/javascript">
function closeall()
{
var divs=document.getElementsByTagName('div')
for(var i=0; i<divs.length; i++)
divs[i].style.display='none';
}
function clicked(element)
{
var div=document.getElementById(element)
if(div.style.display=='none')
div.style.display='block';
else
div.style.display='none';
return;
}
</script>

<?php
echo 'My';
echo '<div id="My">';
while ($row = mysql_fetch_array($sql))
{
?>
  <div id="trade">
   <li>
    <?php echo '<a href="#" onclick="clicked(' . $row['item_name'] . ')">' . $row['item_name'] . '</a>'; ?>
</li>  
<li>
  <?php echo '<img src="' . $row['thumb_1'] . '" width="100" alt="" />'; ?>
  </li>
  </div>
<?php
}
  echo "</div>";
?>

 

found some functions online that work great for a tree menu, but when i have a little php in with the menu (so i can pull info from db) it seems to throw off the javascript functions. Can you see what is wrong? I'm guessing that the js cannot be inside the php like that.

ok, maybe it is not working, sorry guys. here is the problem, again.

this works, meaning my function works

<a href="#" onclick="clicked('name')">name</a>

but for the name i want to pull it from the db, which is not correctly executed below.

<?php echo '<a href="#" onclick="clicked(' . $row['item_name'] . ')">' . $row['item_name'] . '</a>'; ?>

Anyone?

Here is all the code again.

function clicked(element)
{
var div=document.getElementById(element)
if(div.style.display=='none')
div.style.display='block';
else
div.style.display='none';
return;
}

<?php
echo 'My';
echo '<div id="My">';
while ($row = mysql_fetch_array($sql))
{
?>
<div id="trade">
<li>
  <?php echo '<a href="#" onclick="clicked(' . $row['item_name'] . ')">' . $row['item_name'] . '</a>'; ?>
</li>
<div id="item name">
  <?php echo '<img src="' . $row['thumb_1'] . '" width="100" alt="" />'; ?>
</div>
</div>
<?php
}
  echo "</div>";
?>

In your "working" example, there are quotes around the name.  But when you changed it to the echo, they got left out.  Don't you need them so JS will see the string rather than a variable name?

echo '<a href="#" onclick="clicked(\'' . $row['item_name'] . '\')">' . $row['item_name'] . '</a>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.