Return from jQuery function

2

Hello, I'm picking up on something very simple. I have a function in jQuery that checks if there is any open modal on the page. For this it checks the class of <body> and returns 1 if it has modal open and 0 if it does not.

So far so good. If I give a console.log inside ifs it works normally. But, now comes the "problem."

I tried several ways to use the return in my script php but I can not. I just want to get the return, and check if it's equal to 1, if it's got modal open, if it's 0 it does not have it.

Could you help me?

function:

function verifyModals(){
if ($("body").hasClass("modal-open")) {
    console.log('modal aberto');
    return 1;
}else{
    console.log('modal FECHADO');
    return 0;
}

}

In my script php , how do I get the return? a echo "<script>verifyModals();</script>"; would no longer solve?

Thank you in advance

    
asked by anonymous 16.06.2016 / 16:20

1 answer

1

Lipearu,

If I understand correctly, you want to get the return of this jQuery function and use it in your PHP script.

But this is not possible, quite simply what happens is:

  • The user makes a request for your page
  • Your server receives the request interprets which page should be returned.
  • Your PHP code from that page will be interpreted and will return the HTML page.
  • The server takes that page and returns it to the user.
  • From that moment on, your jQuery will start running and your page will not run again.
  • The point is PHP is interpreted by the server, jQuery is interpreted by the browser. So if you want to make your return interpreted by some PHP, you should make a request and get your return. I would advise you to use Ajax for this.

        
    16.06.2016 / 16:54