how to call a php function through ajax?

0

How can I call my function PHP by ajax when a click occurs on my span ? I am not able to call the function correctly anyone could help me?

code HTML

<span  id="link" class="dropdown-toggle icone">
    <i class="icon-logout"></i>
</span>

code PHP

function logout($sessao){
        if($sessao == $_SESSION['logado']){
            unset($sessao);
            session_destroy();

        }else if($sessao == $_SESSION['logadoCliente']){
            unset($sessao);
            session_destroy();

        }
    }

code Ajax

$("#link").click(function(){
                $.ajax("functions/logout.php",{

                }).done(function(){

                }).fail(function(){

                });
            }); 
    
asked by anonymous 04.02.2017 / 22:55

3 answers

2
  

How to call a php function through ajax?

Response : ajax does not call the function itself, it only requests the script logout.php . The PHP script should instantiate the function itself and return a response:

logout.php :

if(isset($_GET['session'])){$sessao = $_GET['session'];} else{$sessao = '';}

function logout($sessao){
  if($sessao == $_SESSION['logado']){
   unset($sessao);
   session_destroy();
  }
  else if($sessao == $_SESSION['logadoCliente']){
   unset($sessao);
   session_destroy();
  }
}
//The cat's leap: Chame a função aqui mesmo:
logout($sessao);

Obs: I'm guessing that the value of this $session should be sent by the ajax request, then:

$(function () {
    $("#link").click(function(){
        $.ajax({
            url:"assets/teste.php",
            type:'get',
            data: {
                'session':'Valor da session'
            }
        }).done(function(resp){
            $( "#link" ).append( resp );
        }).fail(function(){
            $( "#link" ).append( 'Requisição falhou!' );
        });
    });
});
    
07.02.2017 / 23:09
0

If you are like me, that is, do not like jQuery and do not use it or pay you, here is another way:

<script type="text/javascript" charset="utf-8">
function ajaxObj(meth, url) {
var x;
try {x=new XMLHttpRequest();}
catch(e) {var XmlHttpVersions=new Array("MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP");for (var i=0; i<XmlHttpVersions.length && !x; i++){try { x=new ActiveXObject(XmlHttpVersions[i]);} catch (e){}}
};
if (!x){displayError("Erro ao criar o objeto XMLHttpRequest.");} else {x.open(meth, url, true);x.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); x.setRequestHeader("Cache-Control", "no-cache");x.setRequestHeader("Pragma", "no-cache");x.setRequestHeader("X-Requested-With", "XMLHttpRequest");return x;}};
function ajaxReturn(x){if(x.readyState==4 && x.status==200){return true;}
}
</script>
<script type="text/javascript" charset="utf-8">
function _(x){ return document.getElementById(x);}
</script>

<script type="text/javascript" charset="utf-8">
function pc(oquepost,pagesc){
if(typeof(oquepost) !== 'undefined'){
var name = oquepost;
}else{
var div1 = document.getElementById('artigo');
var name = div1.getAttribute("data-postid");
};
if(typeof(pagesc) !== 'undefined'){var pagec = pagesc;} else {var pagec = null;};

var vars = 'procuraralgo='+name+'&pagina='+pagec;
var url = "arquivo_procurador.php";
var ajax = ajaxObj("POST",url,true);
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
var return_data = ajax.responseText;
_("artigo").innerHTML = return_data;}
};
ajax.send(vars);
_("artigo").innerHTML = "A processar os dados...";
}
</script>

I hope it helps!

    
05.02.2017 / 23:11
0

You click on span and nothing happens? Is that it?

If so, try this:

$('body').on('click', '#link', function () {

   $.ajax("functions/logout.php",{

            }).done(function(){

            }).fail(function(){

            });

});

Your code PHP may be so (I did not understand the existence of the function ...)

if(isset($_SESSION['logado'])){

            unset($_SESSION['logado']);
            session_destroy();

        } else if (isset($_SESSION['logadoCliente'])){

            unset($_SESSION['logadoCliente']);
            session_destroy();

        }

This is if you want to log out with ajax .

    
07.02.2017 / 01:44