Hi there,
I've made this page in html:
http://www.greenbeanbaby.com/retailers.php
with drop-down menus that work.

But when I try to make it dynamic for my client to update it, I'm stuck! The drop downs don't work.
Here is the code I'm using:

$r=mysql_query("SELECT * FROM retailer");
	$varloop=1;
	echo "<div id='wrap'>";
	while(list($idr, $name)=mysql_fetch_array($r)){
		//echo "$name";
		//echo "<br />";
			
		$r2=mysql_query("SELECT idp, provinces FROM province WHERE parentidr='$idr'") or die(mysql_error());
		while(list($idp, $provinces)=mysql_fetch_array($r2)){
			echo ("<span class='product-menu'>");
			echo "$provinces";
			echo ("</span>");
			echo ("<a href='#' id='example.$varloop.-show' class='showLink' onclick='showHide(example.$varloop);return false;'><img src='../images/retailer-down.gif' alt='retailer' width='10' height='8' class='img' /></a>");
			echo ("<div id='example.$varloop' class='more'><a href='#' id='example.$varloop.-hide' class='hideLink' onclick='showHide(example.$varloop);return false;'><img src='../images/retailer-up.gif' alt='retailer' width='10' height='8' class='img' /></a>");
			echo "<br />";
			$varloop++;
			//echo $varloop;
				
			$r3=mysql_query("SELECT idc, cities FROM city WHERE parentidp='$idp'") or die(mysql_error());
			while(list($idc, $cities)=mysql_fetch_array($r3)){
				echo ("<span class='bold'>");
				echo "$cities";
				echo ("</span>");
				echo "<br />";
					
				$r4=mysql_query("SELECT ids, stores, website, link, phone FROM store WHERE parentidc='$idc' AND parentidp='$idp'") or die(mysql_error());
				while(list($ids, $stores, $website, $link, $phone)=mysql_fetch_array($r4)){
					echo "$stores";
					echo " ";
					echo ("<a href='$link' target='_blank' class='second'>");
					echo "$website";
					echo ("</a>");
					echo " ";
					echo "$phone";
					echo "<br />";
				}			
	                }
		echo ("</div>");
		echo "<br /><br />";
		}
	}
		
	echo "</div>";

Can someone help me find my mistake please?

Thank you!

Dani AI

Generated

A compact, practical summary and fix for the symptom described by (static HTML works but the PHP-generated page’s drop-down toggles do not), and the observation from that no drop-downs appear to work.

Most common causes

  • The HTML that PHP prints is not the same as the working static HTML (IDs or onclick arguments are malformed or unquoted).
  • The JS event is bound to an element that does not exist or has an unexpected id.
  • Dots or other punctuation in id values complicate selection or cause errors.
  • Script runs before the DOM is ready or a JS error stops execution (check the browser console).

Fast, reliable approach (safe, modern, and easy to debug)

  • Generate simple, predictable ids (no dots): use example-1, example-2, etc.
  • Avoid inline onclick string-building mistakes; instead emit a data attribute and attach a single event listener. This removes quoting/escaping issues when echoing from PHP.

Example patterns (apply these instead of inline onclick generation):

<!-- HTML produced by PHP -->
<a href="#" class="toggle" data-target="example-1">▾</a>
<div id="example-1" class="more"> ...stores... </div>
/* JS: attach once, no per-item inline JS */
document.addEventListener('click', function(e){
  var t = e.target.closest && e.target.closest('.toggle');
  if(!t) return;
  var id = t.getAttribute('data-target');
  var el = document.getElementById(id);
  if(el) el.classList.toggle('open');
  e.preventDefault();
});
/* CSS */
.more { display: none; }
.more.open { display: block; }

Additional tips

  • While debugging, open DevTools Console: look for ReferenceError or syntax errors and inspect the generated HTML for exact id/onclick values.
  • Always escape database output (e.g., htmlspecialchars) to avoid XSS.
  • Replace deprecated mysql_* code with mysqli or PDO and prepared statements for security and future compatibility.

This pattern makes the generated markup easy to inspect, avoids string-escaping pitfalls when echoing from PHP, and simplifies debugging if a toggle is not working.

Recommended Answers

All 5 Replies

I looked at this site and I don't see any working drop-down menus.

I'm not prepared to spend the time doing the debugging that you need to do. Maybe someone else is but that may be why you don't have any responses yet. If you at least showed what was working and then specifically what you changed, there might be a chance that someone would spot something wrong in the changes.

Hi,
Thank you for the remark...

So I guess I did not explain properly: the drop-down are next to the provinces "Alberta"..., the little triangle that indicates 'down': this opens a list of stores. And you can find the html code by viewing the page source.
So you can compare the html code and the php code that is up here in my first post.

I hope this is enough new info.

Thank you for any help.

Help please!

I'm gonna close this discussion but I thought I would give you the answer, as I found it from a friend of mine:
Changes are in red

Line 13
echo ("<a href='#' id='example.$varloop.-show' class='showLink' onclick='showHide(example.$varloop);return false;'><img src='../images/retailer-down.gif' alt='retailer' width='10' height='8' class='img' /></a>");

Line 14
echo ("<div id='example.$varloop' class='more'><a href='#' id='example.$varloop.-hide' class='hideLink' onclick='showHide(example.$varloop);return false;'><img src='../images/retailer-up.gif' alt='retailer' width='10' height='8' class='img' /></a>");

Hope this can help some of you, unless my code is really bad anyway! At least it works for now!
Cheers

Member Avatar for Member #120589
Changes are in red

Well that certainly helps! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.