We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13632
    • 22 Posts
    hi

    I’ve been playing around with a module and snippet. So far I’ve got the module to store info into a Db and the snippet to display the list of Db records.

    Now, in the list that’s displayed i have 2 links (1 image and 1 text). In the html img tags I want to use the ’onclick’ event to run a javascript function which will update the Db by 1 count (yes, a click counter). I would also like to update the count on the same record if the text link is clicked.

    I can get the ’onclick’ event to display an ’alert’ msg but can’t work out how to get the id value of the Db record that the image file name is stored.

    I had a go with a few diffrent attempts including this:

    
    //from snippet
    
    <a href="'.$am_link.'"><img src="'.$am_img.'" id="'.$am_rec_id.'" onclick="Amhit()" border="0" alt=""  ></a> 
    
    // javascript in external file
    
      function Amhit('.$am_rec_id.')
    {
    var recid=document.getElementById('.$am_rec_id.').innerHTML;
    alert("you clicked on rec id!" + recid.innerHTML );
    }
    


    but nothing seems to work except the basic alert msg.

    I should mention that I’m new to javascript, although, it does seem quite similar to php, I just can’t figure this out.

    any help or advice is much appreciated

      • 31471
      • 206 Posts
      I’m not sure I understand your problem, but when I get unsure I always look at w3cschools.com as a reference. It has x/html, css, javascript, php, dom, ajax (and more) sections with examples, which you can search around.
        • 13632
        • 22 Posts
        Thanks for that vhollo, but thats how I’ve got to where I am now smiley
          • 10449
          • 956 Posts
          This is just a regular hit-counter, right? If yes, this seems somewhat confusing and overly complicated.

          Try this:

          // snippet should contain something like this:
          $output =<<<MARQUIS
          <a href="$am_link" onclick="deSade('$am_rec_id');"><img src="$am_img" border="0" alt="Click to visit $am_link" /></a>
          MARQUIS
          
          return($output);
          
          
          
          // JS
          // load an Ajax library (e.g. http://js.fleegix.org/)
          <script type="text/javascript" src="js/fleegix.js"></script>
          
          function msg(resp) {
          	alert(resp);
          }
          
          function deSade(recid) {
          	fleegix.xhr.doGet(msg, 'hitcounter.php?recid='+recid, 'text');
          }
          
          
          // hitcounter.php is basically just:
          // a) some mySQL query that updates your DB-table, according to the $_GET['recid'] value
          // b) a simple message that will be output to the browser (msg JS function), e.g.
          echo "Record ID $recid has been updated!";
          
          
          



          I mean, if you already know the recid var at the time you write the snippet which outputs the link-lists, why not include this recid in the JS function? Getting the recid via document.getElementById() is a big unneccessary detour...
            • 13632
            • 22 Posts
            Thanks ganeshXL,

            I’ll go and have a play with that now. Just 1 question though, what does the ’<<<MARQUIS’ bit mean huh
              • 10449
              • 956 Posts
                • 13632
                • 22 Posts
                Ah.. I see. Thanks ganeshXL

                I’ve had a go with your suggestion and using a simple echo to return to the browser it works fine. But, when I add the code to update the Db I get this msg:

                "Fatal Error: Call to a member function getFullTableName() on a non object in /.../hitcounter.php on line 15."

                <?php
                
                //echo "Record ID $recid has been updated!"; //-- works great with just this
                
                //-- added this in an attempt to stop fatal error msg
                global $table;
                
                $basePath = $modx->config['base_path'];
                $siteURL = $modx->config['site_url'];
                $tb_prefix = $modx->db->config['table_prefix'];
                //-- didn't stop error though :(
                
                
                if ($recid !=""){
                
                $table = $modx->getFullTableName('clicklog');
                $rs = $modx->db->select('clicks', $table, 'id="'. $recid .'"');
                		$row = $modx->db->getRow($rs);
                		$hits = $row['clicks'];
                		
                		$hit = $hits++;
                		
                		$rows_affected = $modx->db->update("clicks = '.$hit.'", $table, "'id='.$recid.'"); 
                		
                		
                 echo "Record ID $recid has been updated!";
                
                 } else {
                echo "update failed - no id!!";
                }
                ?>
                


                what have I got wrong?
                  • 10449
                  • 956 Posts
                  Well, you should put all this inside a new snippet and publish a new modx-page with template = blank (+ "show in menu" = no).

                  The content of that page should only contain your "hitcounter" snippet, nothing else.

                  I just used a dummy filename "hitcounter.php" as an example, but this can also be a modx-page (with FURLs activated) + an alias of "hitcounter" smiley
                  Just make sure you point to the valid URL in your JS: fleegix.xhr.doGet(msg, ’hitcounter.php?recid=’+recid, ’text’);
                  that’s it smiley

                  If you use a separate php file, you have to include all the $modx vars first, before accessing $modx->config etc.
                  PHP by itself doesn’t know of any modx API, unless you include it.
                    • 13632
                    • 22 Posts
                    Hi ganeshXL,

                    Thanks again. I did as discribed above ( after prying myself away from our new Wii console smiley ) and it seems to be working in IE7 but I get nothing in FireFox.

                    Any ideas?

                      • 10449
                      • 956 Posts
                      That’s not possible, it works in all recent browsers.

                      What’s the URL of your test-page?