Calling PHP function in jQuery

Asked

Viewed 5,450 times

0

I am starting my PHP studies and I have to make a registration form, however, it is very bad to validate data via PHP, so I am using jQuery for this.

However, I need to call the PHP function inside jQuery to only take data that has already passed validation. How can I do this?

  • Take a look here: http://answall.com/questions/6626/howto create a-site-sem-reload

  • 3

    Forget it it is very bad to validate via PHP. Front-end validation depends on the user. PHP validation is not susceptible to failure. If the user disables javascript or causes its validation function to always return true, its validation is over.

1 answer

4

Request via AJAX with jQuery.

We’re accessing a file http://exemplo.com/funcoes.php, assuming it is in the same directory, it is possible to access links. A very simple example, this is the HTML file:

   <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 the file funcoes.php

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

    //executando a funcao
    teste();
?>

Browser other questions tagged

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