How to execute a php function in ajax?

0

I'm wondering how can I do to click a button and onclick this button I call a php function, they said that I have to use ajax, however I do not have ajax knowledge but I'm going to study, could you help me?

  <button id="loadmore" onclick="<?php queryRetorno($limite*2);?>">Carregar 
   Mais</button>

I tried this way but it did not work, basically what I want is to increase the query sql limit every time the button is clicked, here is the query query function

 function queryRetorno($limite){

 $sql = mysql_query("SELECT DISTINCT wposts.*,(SELECT meta_value FROM      
 wp_7_postmeta WHERE meta_key='_wp_attached_file' and post_id = (SELECT      
 meta_value FROM wp_7_postmeta WHERE meta_key='_thumbnail_id' and  
 wp_7_postmeta.post_id= wposts.ID limit 1)) as imagem
 FROM wp_7_posts wposts 
 LEFT JOIN wp_7_postmeta wpostmeta ON wposts.ID = wpostmeta.post_id 
 LEFT JOIN wp_7_term_relationships ON (wposts.ID =      
 wp_7_term_relationships.object_id) 
 LEFT JOIN wp_7_term_taxonomy ON (wp_7_term_relationships.term_taxonomy_id =      
 wp_7_term_taxonomy.term_taxonomy_id)
 where 'post_type'='post' and post_status ='publish'
 LIMIT {$limite}");
 carregaImagem($sql);

 return $limite;
 }
    
asked by anonymous 17.03.2017 / 15:23

1 answer

1

Yes you will have to do an httpRequest using javascript (ajax) to a url that performs your desired function, since you can never use php in javascript because one works on the server side (php) and another on the user side (javascript)

1st You will need to know the url of the page that performs your php function ex: ( link )

2nd As you can not have php inside the javascript or html, what you have to do is to execute a javascript function that makes an httpRequest to the url of your function (php)

HTML:

<button onclick="chamaAminhaFuncao(ColocasAquiOTeuLimit);">Executar a funcao php</button>

Javascript (I'm going to use JQuery to make it easier to use ajax):

var chamaAminhaFuncao = function(limit){
    var request = $.ajax({
      url: "http://omeusite.com/minhafuncao.php",
      method: "POST",
      data: { limit: limit}
    });

    request.done(function( msg )
    {
        //Aqui colocas o codigo para quando tudo corre bem
    });

    request.fail(function( jqXHR, textStatus ) {
        //Aqui colocas o codigo para quando algo corre mal
    });
}

Attention, before your javascript you have to include jQuery in your HTML

<script
  src="https://code.jquery.com/jquery-2.2.4.min.js"integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
  crossorigin="anonymous"></script>
<script>
    Codigo javascript que fiz la em cima
</script>

Now in php it's easy

 function queryRetorno($limite){

      $sql = mysql_query("SELECT DISTINCT wposts.*,(SELECT meta_value FROM      
      wp_7_postmeta WHERE meta_key='_wp_attached_file' and post_id = (SELECT      
      meta_value FROM wp_7_postmeta WHERE meta_key='_thumbnail_id' and  
      wp_7_postmeta.post_id= wposts.ID limit 1)) as imagem
      FROM wp_7_posts wposts 
      LEFT JOIN wp_7_postmeta wpostmeta ON wposts.ID = wpostmeta.post_id 
      LEFT JOIN wp_7_term_relationships ON (wposts.ID =      
      wp_7_term_relationships.object_id) 
      LEFT JOIN wp_7_term_taxonomy ON (wp_7_term_relationships.term_taxonomy_id =      
      wp_7_term_taxonomy.term_taxonomy_id)
      where 'post_type'='post' and post_status ='publish'
      LIMIT {$limite}");
      carregaImagem($sql);

      return $limite;
 }

 //pegas os dados passados por post
 isset($_POST['limit'] ? $limit = ( (int) $_POST['limit'] * 2 ) : $limit = 0;

 //agora chamas a tua funcao php

 $oTeuRetorno = queryRetorno($limit);

I did not test but the logic is this and I think everything is working

    
17.03.2017 / 15:41