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?
– Fleuquer Lima
yes the function works. it does if correctly. the console.log returns the expected. Return should not be the return of the function
– Lipearu
But you want to take this return to use in an if for example?
– Fleuquer Lima
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.
– Fleuquer Lima
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
– Lipearu
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.
– Fleuquer Lima
but as in the first execution it already gives the right result on the console?
– Lipearu
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?
– Lipearu
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..
– Fleuquer Lima
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.
– Lipearu
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?
– Lipearu
if no php if(verifyModals() == 1){ echo "open modal"; } should not work?
– Lipearu
Let’s go continue this discussion in chat.
– Fleuquer Lima
@fleuquer-lima helped me chat. Thank you
– Lipearu
@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.
– Math
Okay, I’ll formalize a response
– Fleuquer Lima