How to return PHP code with Javascript?

Asked

Viewed 404 times

2

Topic edited completely

My file Follow_Button.php:

<?php

require_once '.././app/autoload.php';

$TwitterUsers = new TwitterUsers;

require_once '.././app/TwitterOAuthHelper.php';

$helper = new TwitterOAuthHelper;

$row = $TwitterUsers->selectUserAll();
foreach ($row as $fetch) {
    $helper->friend($fetch['id_user']);
}

My method:

public function friend($user_id) {
    $access_token = $_SESSION['twitter_access_token'];
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);

    $friend = $connection->post("friendships/create", [
        "user_id" => $user_id,
        'follow' => true

    ]);

    if ($user_id === $this->signedIn()->id) {
        return false;
    }

    return $friend;

}

I wanted to limit PHP code, receive 1 follower every 10 seconds, and count with javascript and when finished executing the code the reactivate button again:

<a href="" onclick="return getPhpAjax();" class="text-none">
  <button type="button" data-loading-text="Ganhando seguidores..." id="getFollow" class="btn btn-success btn-block">
    <i class="fa fa-refresh"></i> Ganhar seguidores agora
  </button>
</a>

<script type="text/javascript">
  function getPhpAjax() {
   $.ajax({
      url:'./app/Follow_Button.php',
      complete: function (response) {
         $("#getFollow").click(function() {
            var $btn = $(this);
            $btn.button('loading');
         });
      },
      error: function () {
          alert('Erro');
      }
  });  

  return false;
}
</script>
  • Use the jQueey ajax

  • I’m trying... I’ll update the topic soon...

  • Updated.....

1 answer

2


You should reset the button and only after the return of PHP should it be available again.

Try it like this:

<a href="" onclick="getPhpAjax();" class="text-none">
  <button type="button" data-loading-text="Ganhando seguidores..." id="getFollow" class="btn btn-success btn-block">
    <i class="fa fa-refresh"></i> Ganhar seguidores agora
  </button>
</a>

<script type="text/javascript">

  function enableBtn(state) {

   if(state) { $('#getFollow').text('Loading...').attr('disabled', true) }
   else { $('#getFollow').text('texto original'). attr('disabled', false) }
  }

  function getPhpAjax() {
   enableBtn(false)
   $.ajax({
      url:'./app/Follow_Button.php',
      complete: function (response) {
         var t = setTimeout(function() { enableBtn(true); clearTimeout(t); }, 10000)
      },
      error: function () {
          alert('Erro');
      }
  });  

  return false;
}
</script>

Obviously it’s a very simple definition because you could simply remove the code and it would stop getting blocked, I advise you to do a treatment on the server not to receive requests before 10s

  • It worked, I just had to change the line else { $('#getFollow').text('Ganhando seguidores...'). attr('disabled', true) }... I’ll mark as solved... but have another question. how to count incoming followers?

  • That would have to come in your back-end response

  • Like do a check if both followers are received but wanted to count type, you’re getting 1,2,3 ... followers count in real time...

  • But then it changes the structure of your code a little bit, you would need to open a socket to be able to do this in real time

  • with ajax there is no way? , I have a site here example I do not know if you can publish here.

  • I think you better open up another question to that...

  • Okay, I’m going on a trip now, when I get there...

Show 2 more comments

Browser other questions tagged

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