<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Click counter per più bottoni nella stessa pagine]]></title><description><![CDATA[<p dir="auto">Buongiorno a tutti. Ho questo semplice click counter che funziona benissimo, ma vorrei poter aggiungere più elementi cliccabili nella medesima pagina con archivio nello stesso file txt. Inoltre, il totale dei click scompare se si aggiorna la pagina, mentre vorrei che restasse sempre presente. È possibile? Potete aiutarmi? Grazie mille</p>
<p dir="auto">La fonte da cui ho preso il codice è questa: <a href="https://www.opentechguides.com/how-to/article/ajax/101/ajax-php-click-counter.html" rel="nofollow ugc">https://www.opentechguides.com/how-to/article/ajax/101/ajax-php-click-counter.html</a></p>
<pre><code class="language-html">&lt;a href = "https://www.xxxxxxxxxx.com" target="_blank" data-pageref-id="pageA1234" onClick="onClick(this)"&gt;LinkA&lt;/a&gt; - Clicks: &lt;div class="display: inline" id="clicks"&gt;&lt;/div&gt;

&lt;script&gt;
    function onClick(e) {
        var id = e.getAttribute('data-pageref-id');
    var post = 'id='+id;
    var req = new XMLHttpRequest();
    req.open('POST', 'click-counter.php', true);
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    req.onreadystatechange = function(){
            if (req.readyState !== 4 || req.status !== 200) return;
            document.getElementById("clicks").innerHTML = req.responseText;
     };
    req.send(post);
 };
&lt;/script&gt;
</code></pre>
<pre><code class="language-php">&lt;?php

//Set path to the text file that stores counts
$file = 'click-counter.txt';

//Open the file in read write mode
$fh = fopen($file, 'r+');

//Assign the page referene posted from page to a variable
$id = $_REQUEST['id'];

//Initialize lines variable
$lines = '';

//Repeat until end of file
while(!feof($fh)){

    //Split the line using comma delimiter
    $row = explode(',', fgets($fh));
    $pageref = trim($row[0]);
    $count = trim($row[1]);
    if(!empty($pageref)){
        if($pageref == $id){
         //Increment count
          $count++;
          echo $count;
        }
      
       //Append new line to text file
        $lines .= "$pageref,$count\r\n";
    }
 }
 
 file_put_contents($file, $lines);
 fclose($fh);
 
 ?&gt;
</code></pre>
<p dir="auto">Nel file click-counter.txt:<br />
pageA1234,0<br />
pageB5678,0</p>
]]></description><link>https://dev.connect.gt/topic/239085/click-counter-per-più-bottoni-nella-stessa-pagine</link><generator>RSS for Node</generator><lastBuildDate>Wed, 25 Nov 2026 10:06:33 GMT</lastBuildDate><atom:link href="https://dev.connect.gt/topic/239085.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 27 Oct 2019 10:59:18 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Click counter per più bottoni nella stessa pagine on Wed, 06 Nov 2019 13:20:53 GMT]]></title><description><![CDATA[<p dir="auto">Grazie e scusa se tardo a rispondere, ma non mi arrivano le notifiche dei nuovi messaggi, anche se le ho impostate. Non so perche<code> il totale scompare quando si aggiorna la pagina, ma cosi</code> e`. Intanto provo la soluzione che mi hai mandato. Buon pomeriggio e grazie ancora</p>
]]></description><link>https://dev.connect.gt/post/1260507</link><guid isPermaLink="true">https://dev.connect.gt/post/1260507</guid><dc:creator><![CDATA[cosina]]></dc:creator><pubDate>Wed, 06 Nov 2019 13:20:53 GMT</pubDate></item><item><title><![CDATA[Reply to Click counter per più bottoni nella stessa pagine on Tue, 29 Oct 2019 15:35:30 GMT]]></title><description><![CDATA[<p dir="auto">Scusa non avevo letto bene quello che chiedevi, ignora il mio ultimo messaggio.</p>
<p dir="auto">Il codice del link funziona.</p>
<p dir="auto">Va messo solamente un controllo</p>
<p dir="auto">Al posto di questa riga</p>
<pre><code class="language-php">$count = trim($row[1]);
</code></pre>
<p dir="auto">metti</p>
<pre><code class="language-php">$count = isset($row[1]) ? trim($row[1]) : '0';
</code></pre>
<p dir="auto">Come mai il totale dei clic scompare se si aggiorna la pagina?</p>
<p dir="auto">Se vuoi aggiungere ulteriori valori puoi farlo direttamente nel file dopo l'ultimo valore</p>
<p dir="auto">pageA1234,12<br />
pageB5678,5<br />
nuovovalore,0</p>
]]></description><link>https://dev.connect.gt/post/1260509</link><guid isPermaLink="true">https://dev.connect.gt/post/1260509</guid><dc:creator><![CDATA[flaviors200]]></dc:creator><pubDate>Tue, 29 Oct 2019 15:35:30 GMT</pubDate></item><item><title><![CDATA[Reply to Click counter per più bottoni nella stessa pagine on Tue, 29 Oct 2019 09:10:15 GMT]]></title><description><![CDATA[<p dir="auto">Grazie mille, provo, ma se fosse possibile una risposta più esaustiva direttamente nel codice postato, mi sarebbe di maggior aiuto.</p>
]]></description><link>https://dev.connect.gt/post/1260506</link><guid isPermaLink="true">https://dev.connect.gt/post/1260506</guid><dc:creator><![CDATA[cosina]]></dc:creator><pubDate>Tue, 29 Oct 2019 09:10:15 GMT</pubDate></item><item><title><![CDATA[Reply to Click counter per più bottoni nella stessa pagine on Mon, 28 Oct 2019 17:34:00 GMT]]></title><description><![CDATA[<p dir="auto">Ciao,</p>
<p dir="auto">quando usi fopen apri il file in modalità "a+"</p>
<p dir="auto">Quindi usa la funzione fwrite per aggiungere una stringa</p>
<p dir="auto">Es.</p>
<pre><code class="language-php">&lt;?php
$filename = 'file.txt';
$handler = fopen($filename, 'a+');
fwrite($handler, "Stringa di test");
fclose($handler);
</code></pre>
]]></description><link>https://dev.connect.gt/post/1260500</link><guid isPermaLink="true">https://dev.connect.gt/post/1260500</guid><dc:creator><![CDATA[flaviors200]]></dc:creator><pubDate>Mon, 28 Oct 2019 17:34:00 GMT</pubDate></item></channel></rss>