Ver Mensaje Individual
  #9 (permalink)  
Antiguo 25/06/2007, 21:32
Avatar de Panino5001
Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 20 años, 9 meses
Puntos: 834
Re: llamar funcion PHP desde JavaScript

caricatos ( ) está en lo cierto absolutamente. Lo que puede hacerse es simular que llamamos a una función php desde javascript y tomar los resultados desde el servidor para usarlos en javascript. Normalmente esa técnica de intercambio se llama Ajax, si es que se realiza sin recargar la página. Hay varias maneras de conseguir ese efecto. Dejo un ejemplo:
Código PHP:
<?php
session_start
();
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// siempre modificado
header("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0"false);
// HTTP/1.0
header("Pragma: no-cache");
if(isset(
$_GET['function']) && !empty($_GET['function'])){
    function 
contar(){ 
        
$_SESSION['contador']=isset($_SESSION['contador'])?$_SESSION['contador']+=1:1;
        return 
'document.getElementById("pepe").innerHTML="'.$_SESSION['contador'].'";';
    }
    function 
foto(){ 
        return 
'document.getElementById("pepe").innerHTML="<img src=\"http://www.disegnocentell.com.ar/new/ejemplos/po.jpg\" />";';
    }
    switch(
$_GET['function']){
        case 
'contar': echo contar();break;
        case 
'foto': echo foto();break;
    }
exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>ejemplo</title>
<script>
function adjs(url){
    oldsc=document.getElementById("old_sc");
        if(oldsc)
            document.getElementsByTagName('body')[0].removeChild(oldsc);
    sc=document.createElement('script');
    sc.id="old_sc";
    sc.src=url+'&<?php echo SID ?>'+'&'+Math.random();
    document.getElementsByTagName('body')[0].appendChild(sc);
}
</script>
</head>

<body>
<a href="javascript:adjs('?function=contar');">Llamar a función contar de php</a> | <a href="javascript:adjs('?function=foto');">Llamar a función foto de php</a>
<div id="pepe"></div>
</body>
</html>

Última edición por Panino5001; 26/06/2007 a las 11:25