Calling separate functions via Ajax with Jquery in PHP

Asked

Viewed 273 times

1

I have the following example:

My file HTML has this code snippet:

<script> 
$( "form" ).submit(function( event ){
            event.preventDefault();
                    $.ajax({
                    url : 'funcoes.php',//url para acessar o arquivo
                    data: {id : 10},//parametros para a funcao
                    type : 'post',//PROTOCOLO DE ENVIO PODE SER GET/POST
                    dataType : 'json',//TIPO DO RETORNO JSON/TEXTO 
                    success : function(data){//DATA É O VALOR RETORNADO
                        alert(data.valor);//VALOR INDICE DO ARRAY/JSON
                    },
        });
});
</script>

and I also have a file called php functions. with the following content:

<?php
    function teste(){
      echo json_encode(array('valor' => $_POST['id']));
    }

    function teste2(){
      echo json_encode(array('valor' => $_POST['id']));
    }
?>

What I’d like to know is: How do I access function teste1(or teste2)? What I have to pass as "parameter" in my HTML file so that it knows which of these two functions I want to execute ?

  • I advise to read a little about REST

  • You can pass as a date, something like: data: {id : 10, acao: "algumaCoisaQueDefineQualFuncaoVaiSerAcessada"} E in the backend use a switch case to filter which function should be accessed when passed such action

  • I understood your idea, I will try to apply it and I return with the result, I thank also for the indication of reading on REST, giving a glance I saw that there are several things that fit my need.

1 answer

0


The ideal would be even you use REST API to be able to directly access the function you want, I recommend giving a read on the subject. If there is also the possibility, it would be interesting to use a framework PHP, if your project has this type of resource.

Now if I meet your need you could pass an additional parameter in your ajax call:

$.ajax({
    url : 'funcoes.php',
    data: {id : 10, acessar: 'teste'},
    type : 'post',
    dataType : 'json',
    success : function(data){
        alert(data['valor']);
    }
});

And in the archive php functions. would be:

<?php
    function teste(){
      echo json_encode(array('valor' => $_POST['id']));
    }

    function teste2(){
      echo json_encode(array('valor' => $_POST['id']));
    }

    if($_POST['acessar'] == 'teste'){
        teste();
    } else if($_POST['acessar'] == 'teste2') {
        teste2();
    }
?>

Looking at your environment would be such an approach.

  • Good afternoon Pedro Souza, thank you very much for the answer, it fits perfectly with the question I asked. About using REST, I’m already reading and I see that it really has everything to do with my need.

  • I’m glad you helped. Good luck there!

  • I’m starting now and sometimes I get apprehensive of asking some things, but if it is possible one last help I appreciate, what I would like to know is how to play the result of: "$. ajax({ url : 'functions.php', date: {id : 10, access: 'test'}, type : 'post', dataType : 'json', Success : Function(data){ Alert(date.value); } });" in a javascript variable.

  • No need to be apprehensive, we are all here to learn or/and transmit knowledge. I edited my reply by placing "Alert(date['value']);" in the Success function. Actually I had not attempted it, you in PHP responded with an array, so the way to access it is date['value'] and not date.value that would be an object.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.