Error calling php function from onclick event

The following happens to me:

I call a function php from the event onclick, my problem is that the php function is executed whenever the page is loaded, i.e. it ignores the onclick.

<a href="archivo.pdf" onclick="funcion()"/a>
  • Probe by calling the function that is on a php page.
  • Probe by calling the php page.
  • Probe by calling a javascript function and in this function is the called the php function.

In all cases the same thing happens to me, the php function is executed whenever the page is loaded ignores the onclick.

$visitas1 = array();
$visitas1 = contVisitas1();

function contVisitas1(){ 

    $archivo = "contador1.txt"; 
    $info = array(); 

    if (file_exists($archivo)){ 
        $fp = fopen($archivo,"r"); 
        $contador = fgets($fp, 26); 
        $info = explode(" ",$contador); 
        fclose($fp); 

        $mes_actual = date("m"); 
        $mes_ultimo = $info[0]; 
        $visitas_mes = $info[1]; 
        $visitas_totales = $info[2]; 
    }else{ 
        $mes_actual = date("m"); 
        $mes_ultimo = "0"; 
        $visitas_mes = 0; 
        $visitas_totales = 0; 
    } 

    if ($mes_actual==$mes_ultimo){ 
        $visitas_mes++; 
    }else{ 
        $visitas_mes=1; 
    } 
    $visitas_totales++; 

    $info[0] = $mes_actual; 
    $info[1] = $visitas_mes; 
    $info[2] = $visitas_totales; 

    $info_nueva = implode(" ",$info); 
    $fp = fopen($archivo,"w+"); 
    fwrite($fp, $info_nueva, 26); 
    fclose($fp); 

    return $info;

}
 5
Author: Carlos Muñoz, 2016-01-18

3 answers

If you want to run code on the server, you have to send a request one way or another. To do this without navigating to another page (or updating it), AJAX is used. Therefore, we must differentiate between PHP running on the server, and JavaScript running on the client side (computer, mobile, tablet...).

Is a very broad topic, so I'm not going to try to explain it in its entirety, but I'll show a small example. In order not to complicate my life, I use jQuery.get() (link in English), but you can do it without any library if you want.

HTML

<script>
function ajax() {
    jQuery.get("ajax.php").then(function (respuesta) {
        document.getElementById("resultado").value = respuesta;
    });
}
</script>
<button onclick="ajax()">pulsa</button>
<textarea id="resultado"></textarea>

Ajax.php

<?php    
echo '¡Hola, mundo!';

When you press the button, a "GET" request will be sent to the server asynchronously. When finished, the server response is put in the <textarea>.

 5
Author: Tom Fenech, 2016-01-19 10:50:59

You are mixing two languages running in different parts the php runs on the server when the document is generated javascript runs in browser

The onClick event will call a javascript function that you must program in that language

If you want something to happen on the server at that time you can use ajax to call another pho script on the server that performs the action you want

 2
Author: Juan Manuel Doren, 2016-05-23 04:39:16

'onclick', is a JavaScript event that runs in the browser, so if you don't have any method that handles that event it will always load whatever you have in the href. I would recommend that in the href you make a reference to the php file that the method has and then download the file.

contVisitas1();

function contVisitas1(){ 

    $archivo = "contador1.txt"; 
    $info = array(); 

    if (file_exists($archivo)){ 
        $fp = fopen($archivo,"r"); 
        $contador = fgets($fp, 26); 
        $info = explode(" ",$contador); 
        fclose($fp); 

        $mes_actual = date("m"); 
        $mes_ultimo = $info[0]; 
        $visitas_mes = $info[1]; 
        $visitas_totales = $info[2]; 
    }else{ 
        $mes_actual = date("m"); 
        $mes_ultimo = "0"; 
        $visitas_mes = 0; 
        $visitas_totales = 0; 
    } 

    if ($mes_actual==$mes_ultimo){ 
        $visitas_mes++; 
    }else{ 
        $visitas_mes=1; 
    } 
    $visitas_totales++; 

    $info[0] = $mes_actual; 
    $info[1] = $visitas_mes; 
    $info[2] = $visitas_totales; 

    $info_nueva = implode(" ",$info); 
    $fp = fopen($archivo,"w+"); 
    fwrite($fp, $info_nueva, 26); 
    fclose($fp); 

    return $info;

}

header("Content-Type: application/octet-stream");

$file = "archivo.pdf";
header("Content-Disposition: attachment; filename=" . urlencode($file));   
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");            
header("Content-Length: " . filesize($file));
flush(); 
$fp = fopen($file, "r");
while (!feof($fp))
{
    echo fread($fp, 65536);
    flush(); 
} 
fclose($fp); 

<a href="funcion.php">Descargar</a>
 1
Author: NAIJULU, 2016-01-19 02:19:52