0

I'm wondering if this is something I can do. I have a PHP script that needs to call a function that I define in my main index.php.

Inside my other PHP file I have:

echo "<script>ShowModal('PopUpNameItem','closeNameItem');</script>";

and inside my main index.php I have that function. how do I actually call this from a seperate script? Normally you would use something like this but since my files are .php can I do this?

<script type="text/javascript" src="ajax.js"></script>

and this is the function I'm trying to call

function ShowModal(popup, closeBtn) {
        var modal = document.getElementById(popup);
        var span = document.getElementsByClassName(closeBtn)[0];
        modal.style.display = "block";
        span.onclick = function() {
            modal.style.display = "none";
        }
        if (modal != "PopUpNameItem") {
            window.onclick = function(event) {
            if (event.target == modal) {
                modal.style.display = "none";
            }
        }
    }
}
RamenChef
  • 5,557
  • 11
  • 31
  • 43
GregHBushnell
  • 143
  • 12
  • What's the context for calling it? AJAX call, page load, or something else? – RamenChef Oct 04 '16 at 18:20
  • Check out this post: http://stackoverflow.com/questions/1045845/how-to-call-a-javascript-function-from-php – c-bro Oct 04 '16 at 18:20
  • i just copied one i use for my code it could need to be index.php in my case but how do i do that? @RamenChef – GregHBushnell Oct 04 '16 at 18:21
  • I think, the sound approach is this: 1) create a pure html/javascript solution that does what you need (verify that it works). 2) make your PHP side produce exactly the same index.html ont he output. – Vladimir M Oct 04 '16 at 18:25
  • @VladimirM i have had it working before posting on here. its only when i try to call it from my php script that it does not show. – GregHBushnell Oct 04 '16 at 18:28
  • so, how do you include both of these scripts? Can you post your index.html, that is being generated? – Vladimir M Oct 04 '16 at 18:32
  • i cant post it all as its rather large but ill summerise.. index.php has some php at the top the main html which contans a hidden div that is popIpNameItem it is defualty hidden. when the user presses a button an ajax script is called which then calls the php script that has the echo script. in it aswell as other things. i can call the showModal function normally with an onclikc within the main index.php its only when its in the php one that nothing happens. there is also no errors – GregHBushnell Oct 04 '16 at 18:33
  • Ok, you may skip most of the parts. What is needed is: 1) where your ShowModal is defined (and included). 2) where do you call this method and how. Also not from you php script but from the final result that is being displayed by the browser. – Vladimir M Oct 04 '16 at 18:39
  • @VladimirM i cant skip most of it unfortunately the echo ""; is only called when the user wins on my website. so they press a button to call the ajax which calls the php which determines if they win which then also calls the showModal function i tjust isnt calling it for whatever reason – GregHBushnell Oct 04 '16 at 19:32
  • ive also tried putting this into the ajax function if (this.responseText == "YOU WON!"){ alert("test"); ShowModal('PopUpNameItem','closeNameItem'); } not even the alert appears but it does display YOU WON! on the page – GregHBushnell Oct 04 '16 at 19:44

2 Answers2

1

Ok. I think can see the clear picture now.

No. it is not possible. You can not can not call client-side javascript method from server-side PHP script.

In web application there are to sides:

  • client (in your case its web browser, that works with html/css and javascript + data received from the server)
  • server side that works with whatever (in your case its PHP) and generates the html/css, javascript and data.

Now, your index.php on the server side is processed by the web server and produces index.html that is being sent by the server to the client. Client opens this file and loads/executes all the scripts,styles that are defined in it. And it creates document structure. All of it is now existing on the client side.

However, server side does not have a direct link to your client context. So the are two ways to run this method as a result of the server processing.

1) to send a ajax request to the server, and, based on the result, execute the method.

2) If you don't want to deal with ajax cal logic, you may do a script insertion approach. How it works, is that when the button is pressed you do something like this:

var scriptRef = document.createElement('script');
scriptRef.setAttribute('src','url_to_script.php');
document.head.appendChild(scriptRef);

What it will do, it will dynamically append a script tag to your document and load script content as a result of your php script output. There you can decide to launch the method or do something else.

However, script insertion is a bit of a headache and should not be used on a long-running pages (in my opinion), as it actually adds a new script every time you press a button. And its your responsibility to keep track of it and remove it.

Vladimir M
  • 4,403
  • 1
  • 19
  • 24
  • Thanks for the answer. i wont be using your way but it did trigger an epiphany so i do have an idea of what to do now. thankyou – GregHBushnell Oct 05 '16 at 06:56
  • Vladimir, what would you suggest is the best option when you need to run a js-function daily and a php script daily? Calling the js-function from PHP isn't possible you say. How can I best achieve this? – nclsvh Apr 25 '18 at 12:30
  • @nclsvh Hello. First of all, it is a different question and needs separate thread and more context. W/o any more contexts it is hard to even imagine the use-case. Calling JS that is running on your browser is not possible from server-side script. But you could use websockets, or client-side polling to trigger the client, as an example. – Vladimir M Apr 26 '18 at 13:47
0

As long as you display your functions correctly in JavaScript format You can just call:

<script type="text/javascript" src="ajax.php"></script>
CyberHelp
  • 63
  • 1
  • 2
  • 9