Function return jQuery

Asked

Viewed 526 times

2

Hello, I’m picking up on something very simple. I have a jQuery function that checks if there is any modal open on the page. For that he checks the class of <body> and returns 1 if there is an open modal and 0 if there is no.

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

I tried several ways to use the return in my script php but I can’t. I just want to take the return, and check if it’s 1, if it’s got modal open, if it’s 0 there’s no.

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 will I catch the return? a echo "<script>verifyModals();</script>"; would no longer solve?

Thanks in advance

  • Do you want to recover the result of the function in PHP? would that be it? Otherwise the function already works right?

  • yes the function works. it does if correctly. the console.log returns the expected. Return should not be the return of the function

  • But you want to take this return to use in an if for example?

  • Well, if I’m not mistaken, there’s a time incompatibility. Technically you are calling the function verifyModals(); before that function exists on the page. So when the call happens it does not find.

  • yes eh para fazer u m if. the php page that has if has at the beginning a include('functions.php); inside the php functions this is this function

  • 1

    You won’t be able to do it that way, in the order of the archives. php is loaded into the server, so when you try to load using echo "<script>verifyModals();</script>";. This script does not exist yet, it only comes into existence after it is sent to the browser. To manipulate this way would have to make a direct call to some php file, through ajax, and would have to call this function on your page directly.

  • but as in the first execution it already gives the right result on the console?

  • something else. the function itself jQuery is who checks whether it has open modal or not. how do I call it in ajax? I’ll have to have another file check?

  • The function works because your echo calls it. You just can’t get back to it in PHP and use it in an if for example. echo only prints what in this case is the <script> tag. And you don’t call it in ajax, if you really want to pass this value to work in PHP, instead of giving the Return, you use an ajax post for example. Just one question, what is the purpose of checking if there is open modal? there may be another way to do..

  • the purpose is as follows. I have a php page that checks some things in the bank. if the information does not exist, it opens a modal to register. then reload the page and check again. this N times, until all information is registered. at the end when there is nothing left to register, there is no modal. then I would do an if asking if you have open modal, if you have not finished the process and can redirect to another page.

  • and if within the if/Else I do a Document.write(0); and Document.write(1); It will print a right return ? I could not use?

  • if no php if(verifyModals() == 1){ echo "open modal"; } should not work?

  • 1
  • 1

    @fleuquer-lima helped me chat. Thank you

  • @Fleuquerlima, do you think you could formalize an answer? Because if you leave it in the chat is less accessible to those who have the same problem and get here in the topic in search of solution.

  • Okay, I’ll formalize a response

Show 11 more comments

1 answer

1

Lipearu,

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

But this is not possible, in a very simple way what happens is:

  1. User makes a request for your page
  2. Your server receives the request interprets which page should be returned.
  3. Your PHP code from that page will be interpreted and return the HTML page.
  4. The server takes that page and returns to the user.
  5. From that moment your jQuery starts to work and yours on that 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 be interpreted by some PHP, you must make a request and get your return. I advise you to use Ajax for this.

  • So within the jQuery function I could not assign a value to some variable and in php call that variable ? if I give an echo "<script>verifyModals();</script>"; I’m calling the right function ?

Browser other questions tagged

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