1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Using OnClick in php...?

Discussion in 'PHP' started by killaklown, Jun 21, 2006.

  1. #1
    Is there anyway to get OnClick to work in php? im trying to make it so that the category will be clicked and the subcategories are shown.

    Im trying to use:

    
    		echo'<div id="masterdiv">';
    		echo'<div class="menutitle" onclick="SwitchMenu('sub1')">';
    		echo '<tr class="menu'.$k.'cat"><td class="link" colspan="'.$colspan.'">'.$link1.'</td></tr>';
    		echo'</div>';
    
    PHP:
    but the onclick is giving me
    SEMrush


    The full code im trying to get these slide down menu to work is

    
    <?php
    
    $header = 'Home';
    $result = mysql_query("SELECT * FROM tdb_categories WHERE parent='' ORDER BY title");
    $num = mysql_numrows($result);
    if($num>"0") {
    	$k = "1";
    	$l = "0";
    	while($l<$num) {
    		$k++;
    		$cid = mysql_result($result,$l,"id");
    		$title = mysql_result($result,$l,"title");
    		$stati = mysql_result($result,$l,"status");
    		if($stati=="open") {
    					$colspan = "1";
    			$link1 = '<a href="list.php?cat='.$cid.'">'.$title.'</a></td><td class="info">'.mysql_numrows(mysql_query("SELECT * FROM tdb_tutorials WHERE cat='$cid' AND status='accepted'")).' &times;';
    		} else {
    			$link1 = ''.$title.'';
    					$colspan = "2";
    		}
    		
    		echo '<tr class="menu'.$k.'cat"><td class="link" colspan="'.$colspan.'">'.$link1.'</td></tr>';
    		$foetus = mysql_result($result,$l,"children");
    		$children = explode(",",$foetus);
    		if($k=="2") {
    			$k = "0";
    		}
    		if(count($children)>"0"&&$foetus!="") {
    			foreach($children as $child) {
    				$k++;
    				$title = mysql_result(mysql_query("SELECT * FROM tdb_categories WHERE id='$child' LIMIT 1"), "0", "title");
    				$stati = mysql_result(mysql_query("SELECT * FROM tdb_categories WHERE id='$child' LIMIT 1"), "0", "status");
    					if($stati=="open") {
    					$colspan = "1";
    					$link = '<a href="list.php?cat='.$child.'">'.$title.'</a></td><td class="info">('.mysql_numrows(mysql_query("SELECT * FROM tdb_tutorials WHERE cat='$child' AND status='accepted'")).')';
    				} else {
    					$link = ''.$title.'';
    					$colspan = "2";
    				}
    				echo '<tr class="menu'.$k.'subcat"><td class="link" colspan="'.$colspan.'">'.$link.'</td></tr>';
    				if($k=="2") {
    					$k = "0";
    				}
    			}
    		}
    		$l++;
    	}
    }
    ?>
    
    PHP:
     
    killaklown, Jun 21, 2006 IP
    SEMrush
  2. sarahk

    sarahk iTamer Staff

    Messages:
    25,266
    Likes Received:
    3,441
    Best Answers:
    100
    Trophy Points:
    665
    #2
    Some fundamentals - PHP produces a lump of text that your browser can read

    Your browser can do an onclick

    So, lets look at your code and the lump of text that you're working with
    echo '<div class="menutitle" onclick="SwitchMenu('sub1')">';
    Code (markup):
    Your echo string has single quotes around it so but within it there are more single quotes so this is what it reads
    echo'<div class="menutitle" onclick="SwitchMenu('
    sub1')">';
    Code (markup):
    Two commands and neither are right.

    All is not lost, you can escape the inner single quotes like this
    echo'<div class="menutitle" onclick="SwitchMenu(\'sub1\')">';
    Code (markup):
    and it'll work!
     
    sarahk, Jun 21, 2006 IP
  3. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #3
    well, it doesnt show an error, but it doesnt work..

    If anyone can get it to work using my code below, it would be appreciated!

    
    <?php
    
    $header = 'Home';
    $result = mysql_query("SELECT * FROM tdb_categories WHERE parent='' ORDER BY title");
    $num = mysql_numrows($result);
    if($num>"0") {
    	$k = "1";
    	$l = "0";
    	while($l<$num) {
    		$k++;
    		$cid = mysql_result($result,$l,"id");
    		$title = mysql_result($result,$l,"title");
    		$stati = mysql_result($result,$l,"status");
    		if($stati=="open") {
    					$colspan = "1";
    			$link1 = '<a href="list.php?cat='.$cid.'">'.$title.'</a></td><td class="info">'.mysql_numrows(mysql_query("SELECT * FROM tdb_tutorials WHERE cat='$cid' AND status='accepted'")).' &times;';
    		} else {
    			$link1 = ''.$title.'';
    					$colspan = "2";
    		}
    		// This is where the main category is, This is what i want to click and then show the subcategories for the category.
    		echo '<tr class="menu'.$k.'cat"><td class="link" colspan="'.$colspan.'">'.$link1.'</td></tr>';
    	
    		$foetus = mysql_result($result,$l,"children");
    		$children = explode(",",$foetus);
    		if($k=="2") {
    			$k = "0";
    		}
    		if(count($children)>"0"&&$foetus!="") {
    			foreach($children as $child) {
    				$k++;
    				$title = mysql_result(mysql_query("SELECT * FROM tdb_categories WHERE id='$child' LIMIT 1"), "0", "title");
    				$stati = mysql_result(mysql_query("SELECT * FROM tdb_categories WHERE id='$child' LIMIT 1"), "0", "status");
    					if($stati=="open") {
    					$colspan = "1";
    					// this is where the subecategories are displayed.
    					$link = '<a href="list.php?cat='.$child.'">'.$title.'</a></td><td class="info">('.mysql_numrows(mysql_query("SELECT * FROM tdb_tutorials WHERE cat='$child' AND status='accepted'")).')';
    
    				} else {
    					$link = ''.$title.'';
    					$colspan = "2";
    				}
    
    				echo '<tr class="menu'.$k.'subcat"><td class="link" colspan="'.$colspan.'">'.$link.'</td></tr>';
    
    				if($k=="2") {
    					$k = "0";
    				}
    			}
    		}
    		$l++;
    	}
    }
    ?>
    
    PHP:

    I commented where the important places are.
     
    killaklown, Jun 21, 2006 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    25,266
    Likes Received:
    3,441
    Best Answers:
    100
    Trophy Points:
    665
    #4
    When you say you get no errors you mean

    * the page looks as you would expect it to
    * the "view source" looks as you would expect it to
    * Firefox's javascript console doesn't throw any errors

    First tell exactly what "isn't working" and then can you point us to the page ?
     
    sarahk, Jun 21, 2006 IP
  5. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #5
    No errors as in, the page shows up without the php error of:
    Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/thfactor/public_html/header.php on line 170

    The thing that isnt working is that i want the category names to be able to be clicked, and a drop down of the subcategories appears. The script im trying to use is from: http://www.dynamicdrive.com/dynamicindex1/switchmenu.htm

    and i have a test of it here: http://www.tutorialhit.com/test/

    but when i add it into the php, it doesnt do anything. (yes the first part of the code that goes in the <head> is there)


    The site/page im trying to get it to work on is: http://www.tutorialhit.com It takes the categories/subcategories out of the mysql.
     
    killaklown, Jun 22, 2006 IP
  6. jnestor

    jnestor Peon

    Messages:
    133
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    It seems to work just fine for me. When I click on each category it expands to show the entried under it and the currently expanded category is collapsed.

    The current category names don't look "clickable" because you don't have them coded as links but rather as plain html text.
     
    jnestor, Jun 22, 2006 IP
  7. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #7
    are you talking about: http://www.tutorialhit.com/test/ ? I know that works because i was testing it there, but when i tried it on the page i want to, it doesnt work.
     
    killaklown, Jun 22, 2006 IP
  8. jnestor

    jnestor Peon

    Messages:
    133
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Ah. Now I see what you're asking. In line 22 you want to change it to:
    echo '<tr class="menu'.$k.'cat"><td class="link" colspan="'.$colspan.'"><div class="menutitle" onClick="switchMenu(\'sub'.$l.'\')">'.$link1.'</div></td></tr>';

    In other words you're wrapping the div around the Category title.

    Then between lines 29 and 30 you'll need to add:
    echo '<span class="submenu" id="sub'.$l.'">';

    and between lines 49 and 50:
    echo '</span>';

    This wraps the "sub menu" section with the span tags.
     
    jnestor, Jun 23, 2006 IP
  9. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #9
    Still doesnt work...

    Maybe i should provide the 2nd part of the script? (the actual drop down script)

    
    <script type="text/javascript">
    
    /***********************************************
    * Switch Menu script- by Martial B of http://getElementById.com/
    * Modified by Dynamic Drive for format & NS4/IE4 compatibility
    * Visit http://www.dynamicdrive.com/ for full source code
    ***********************************************/
    
    var persistmenu="yes" //"yes" or "no". Make sure each SPAN content contains an incrementing ID starting at 1 (id="sub1", id="sub2", etc)
    var persisttype="sitewide" //enter "sitewide" for menu to persist across site, "local" for this page only
    
    if (document.getElementById){ //DynamicDrive.com change
    document.write('<style type="text/css">\n')
    document.write('.submenu{display: none;}\n')
    document.write('</style>\n')
    }
    
    function SwitchMenu(obj){
    	if(document.getElementById){
    	var el = document.getElementById(obj);
    	var ar = document.getElementById("masterdiv").getElementsByTagName("span"); //DynamicDrive.com change
    		if(el.style.display != "block"){ //DynamicDrive.com change
    			for (var i=0; i<ar.length; i++){
    				if (ar[i].className=="submenu") //DynamicDrive.com change
    				ar[i].style.display = "none";
    			}
    			el.style.display = "block";
    		}else{
    			el.style.display = "none";
    		}
    	}
    }
    
    function get_cookie(Name) { 
    var search = Name + "="
    var returnvalue = "";
    if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    if (offset != -1) { 
    offset += search.length
    end = document.cookie.indexOf(";", offset);
    if (end == -1) end = document.cookie.length;
    returnvalue=unescape(document.cookie.substring(offset, end))
    }
    }
    return returnvalue;
    }
    
    function onloadfunction(){
    if (persistmenu=="yes"){
    var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
    var cookievalue=get_cookie(cookiename)
    if (cookievalue!="")
    document.getElementById(cookievalue).style.display="block"
    }
    }
    
    function savemenustate(){
    var inc=1, blockid=""
    while (document.getElementById("sub"+inc)){
    if (document.getElementById("sub"+inc).style.display=="block"){
    blockid="sub"+inc
    break
    }
    inc++
    }
    var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
    var cookievalue=(persisttype=="sitewide")? blockid+";path=/" : blockid
    document.cookie=cookiename+"="+cookievalue
    }
    
    if (window.addEventListener)
    window.addEventListener("load", onloadfunction, false)
    else if (window.attachEvent)
    window.attachEvent("onload", onloadfunction)
    else if (document.getElementById)
    window.onload=onloadfunction
    
    if (persistmenu=="yes" && document.getElementById)
    window.onunload=savemenustate
    
    </script>
    
    Code (markup):
     
    killaklown, Jun 24, 2006 IP
  10. Evoleto

    Evoleto Well-Known Member

    Messages:
    253
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #10
    When JS misteriously fail to work, you should inspect if there are other previous JS errors stopping the engine, if the function itself is correctly and if the DOCTYPE is permissive with the syntax you use. For this last reason you should get used to write JS code in a strict manner. You never know if in 2008 Firefox or IE will keep backwards compatibily for `not so well` written JS code.
     
    Evoleto, Jun 27, 2006 IP
  11. jnestor

    jnestor Peon

    Messages:
    133
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #11
    You have a few problems still.

    1) the switchMenu needs to be SwitchMenu
    2) you need <div id="masterdiv"> around the whole thing
    3) the table code is messing up the script. Not sure why. Not sure how to fix it. I played with it for a bit but couldn't get it to work.
     
    jnestor, Jun 27, 2006 IP
  12. Evoleto

    Evoleto Well-Known Member

    Messages:
    253
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #12
    Very good remarks. In reference to point (2) for maximum compatibility, use to duplicate the id into a name attribute too, there are certain cases when it's required. Something like <div id="myid" name="myid">Blah</div>
     
    Evoleto, Jun 27, 2006 IP
  13. Extension103

    Extension103 Guest

    Messages:
    29
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    ive just used a much simpler and better way to do the "onclick" you wanted.

    i had a similar problem where when a user submitted various data i wanted to fwrite to a file. i kept looking for a function to do an onclick.

    but i figured out you could just use a form that when submitted the action was to exicute a function.

    simple:cool:
     
    Extension103, Nov 30, 2010 IP
  14. Nahid

    Nahid Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    you need to use a backslash before single quote(') is the string is enclosed by '
    
            echo'<div id="masterdiv">';
            echo'<div class="menutitle" onclick="SwitchMenu(\'sub1\')">';
            echo '<tr class="menu'.$k.'cat"><td class="link" colspan="'.$colspan.'">'.$link1.'</td></tr>';
            echo'</div>';
    
    Code (php):
     
    Nahid, Dec 6, 2010 IP
  15. mrdebaaij

    mrdebaaij Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    you cant just call a function without enclosing it in the proper . and semicolons when it is written inside html in an echo.

    it could be something like this you need to change it to:

    echo'<div class="menutitle" onclick=".SwitchMenu(\'sub1\').">';
    or maybe evenb echo'<div class="menutitle" onclick="'.SwitchMenu(\'sub1\').'">';

    this also changes how you use or dont use the escapes around 'sub1'
    that might have to become "sub1" in stead.

    I dont know the exact right order here so you should probably just give it some trial and error.
     
    mrdebaaij, Mar 13, 2011 IP