0

I am working in PHP, and I call a Javascript function like this:

?><script type="text/javascript">send_information();</script><?php

After that, I change the value of a session variable and refresh the PHP page (after refreshing some stuff is displayed on my web app) I would like to wait until the execution of my javascript function is done before refreshing the page.

I looked for something like a timer but did not find what I was looking for, also I looked for a way to send a variable from JS to PHP at the end of execution and put a "while" in my PHP, waiting for this variable but I understood that this is not possible...

(I am not looking for the difference between client side and server side, this question is totally different! :) )

  • Not really sure what part of the scenario you're asking about? If you want some PHP to run after a JS function, you'd make AJAX request at the end of your JS function, or after you call the function. – Jonnix Aug 31 '17 at 09:52
  • I would like to wait until the execution of my javascript function is done before refreshing the page, does not matter with what scenario :) – Olivia Delpierre Aug 31 '17 at 09:58
  • Okay, so what problem are you having with that? Perhaps show your function? Is it refreshing too early, not at all? – Jonnix Aug 31 '17 at 10:05
  • Yes it is refreshing directly, but this causes my javascript function not to work properly (I have to connect and disconnect a MQTT client in it, and refreshing the page stops this process before I have time to do what I want) – Olivia Delpierre Aug 31 '17 at 10:10
  • 1
    "I am not looking for the difference between client side and server side" - YES YOU ARE. You are missing a key understanding of how HTTP works and hence how PHP and javascript interact. – symcbean Aug 31 '17 at 11:35
  • Haha thanks for your answer, I had a misunderstanding, but a clear answer like I had is much more helpful than just pointing the misunderstanding :D THANK YOU – Olivia Delpierre Aug 31 '17 at 13:41

1 Answers1

1

You can't do what you wish to do.

The php part of the code is generated on the webserver. This generates a bunch of text which send as a whole to the client. PHP dies.

The browser on the users computer then interprets this text and displays it. This computer doesn't know anything about what the servers php code wishes to do. It only sees a ready text.

It then executes the javascript code.

Martijn
  • 15,791
  • 4
  • 36
  • 68
Tschallacka
  • 27,901
  • 14
  • 88
  • 133