Error calling a php function from the onclick event

4

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, that is, 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 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;

}
    
asked by Natalia 18.01.2016 в 16:00
source

3 answers

5

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 update it), AJAX is used. Therefore, we must differentiate between PHP running on the server, and JavaScript that runs on the client side (computer, mobile, tablet ...).

It is a very broad topic, so I will not try to explain it in its entirety, but I will show a small example. To not complicate my life, I use jQuery.get() (link in English), but I 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 set to <textarea> .

    
answered by 18.01.2016 в 23:11
2

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

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

If what you want is that at that moment something happens on the server you can use ajax to call another pho script on the server that performs the action you want

    
answered by 23.05.2016 в 06:39
1

'onclick', is a JavaScript event that runs in the browser, so if you do not have any method that manages that event will always load what you have in the href. I would recommend that in the href you make a reference to the php file that has the method 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>
    
answered by 19.01.2016 в 03:19