How to run a php function in ajax?

Asked

Viewed 1,607 times

0

I’m wondering how I can do to click a button and the onclick of this button I call a php function, said I have to use ajax, but I have no knowledge in ajax but I will study, could help me?

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

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

 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;
 }

1 answer

2

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

first You need to know the url of the page that runs your php function ex: (http://omeusite.com/minhafuncao.php)

2nd Since you can’t have php inside javascript or html, what you have to do is run a javascript function that will do an httpRequest to your function url (php)

HTML:

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

Javascript (I will use Jquery to facilitate the use of 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 must 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 have not tested but the logic is this and I think everything is working

  • Just one thing, before I ask, do some research first, because this is one of the most basic scenes you have, and as you say above, you were told you needed to use ajax, you just needed to waste five minutes researching and you had your answer

  • Thanks @Simão Lemos, Doubt.. In the url you have the same ** ones? and the code or php function I call in the request.done?

  • No, sry, the ** was just to bold the text, but it seems I can’t do that inside the code. I’ll fix it now

  • You also don’t need to put the whole url, if the path is the same. You can just put /my path.php

  • Aaah ta, @Simão Lemos but the problem you solved is another, I want to run a function like this I have the php page and run the function of this page

  • Logic is there but I’ll edit the answer...

  • 5

    Already edited... more than this only if it is there your house to make....

Show 2 more comments

Browser other questions tagged

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