Pass Value By PHP Javascript parameter

Asked

Viewed 45 times

0

I have a little bit of code right below.

After the user clicks on a div whose ID is (click) this code will be executed. I have a PHP code that will be executed inside the javascript code block.I want to pass to the Javascript variable PHP (Value) in the method parameter $status->checkTipoUsuario(),getting $status->checkTipoUsuario(value). It is possible?

           $('#click').click(function () {

            var valor='exemplo';


           var recebePHP= "<?php
            $status= new Usuario_Control_Grupo();
            $status->verificarTipoUsuario();

            if($status==true) {
                echo 'existe';
            }else{echo 'inexistente';}

            ?>";
             });
  • 1

    What you need is to perform an AJAX request. A only how Javascript and PHP communicate is by some communication protocol - the most common is HTTP. Search about this.

1 answer

1


When you use web you are using client-server HTTP communication. This means that you perform an HTTP request to the server. The server in turn picks up your request and returns a response, usually an HTML file. Which is interpreted by your browser.

Once the server returns the answer it is impossible for PHP to change some value without another request. So your code will never work like this.

You have to create a separate PHP file that ONLY writes this result you want. Inside the page you do the checking should make a request with Javascript for this file.

Search for:

  • Utilizar Fetch Javascript
  • Simple Ajax with PHP
  • Client Communication Server.

Browser other questions tagged

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