How to concatenate js into php function

Asked

Viewed 34 times

0

I have this class and function

class Controller {

  public function vazio($verfica) {
      if (empty($verfica)) {
          return 'Vazio';
      } else {
          return 'Ok';
      }
  }

}

I call that function within the append it is necessary to concatenate the variable within the function

divposts.append("<?php echo $ctlr->vazio("+val.valor+")?>");
  • There is no way to do this because PHP is processed on the server and JS in the browser after PHP has done its part.

1 answer

1


In your case, as your function lies within your controller responsible for its requests and you’re using the architecture MVC, the correct would be through the javascript you call that your function controller.

For example:

Your Javascript

$.ajax({
  url: window.location.origin + '/sua-rota-apontada-para-controller',
  type: 'GET' // método http da sua rota
  dataType: 'application/json',
  success: function(data){
    divposts.append('<span>' + data + '</span>');
  },
  erro: function(err){
    console.log(err);  
  }
});

Browser other questions tagged

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