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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>";
?>

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.