0

I'm trying to call a jQuery / Javascript function in PHP but it doesn't work. If I replace myFunction() with a simple alert(); it works. myFunction() works if i call it with an onclick().

My code PHP :

<?php

if($_SESSION["..."] == true){
    echo "<script>myFunction()</script>";
} ?> 

My code JS :

<script>

function myFunction() {
    content...
} </script>

Anyone has a solution for me ?

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
TFFC
  • 1
  • 3
  • 1
    Where *exactly* in your html are the code lines for `function myFunction()` defined? It's quite likely that it comes after you try to call them. You should have an error in the console (do you?). In the first instance change your "my code js" so that it appears before you call it. – freedomn-m May 20 '21 at 07:48
  • @El_Vanja so i have to use Ajax ? – TFFC May 20 '21 at 07:52
  • @freedomn-m my is followed by – TFFC May 20 '21 at 07:53
  • 1
    If your goal is to echo the result of that JS function, then yes, you have to use AJAX. If you just want to run this function on page load, then I've misinterpreted the question. – El_Vanja May 20 '21 at 07:53
  • As long as your function is defined before you call it, it should execute when the user loads the page. View the page source in the browser, also look for console errors. – Scuzzy May 20 '21 at 07:53
  • @El_Vanja my goal is to show a new div (a notification alert) – TFFC May 20 '21 at 07:54
  • 2
    Then why not output that HTML with PHP? Why get JS involved at all? Does it depend on some client values? – El_Vanja May 20 '21 at 07:56
  • Which `"` if you're unable to fix/adjust the order of your code. – freedomn-m May 20 '21 at 07:58
  • Thank you all for helping me. It's working now ! – TFFC May 20 '21 at 08:03
  • Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Nico Haase May 20 '21 at 09:47

4 Answers4

0

Dear you have to use the same functionality in single script tag like below. Tbhis is the quickest solution, other solutions are also there but it will get complicated for you.

<?php
echo "<script>
function myFunction() {
    alert('asdf');
}";

if($_SESSION["..."] == true){
    echo "myFunction()</script>";
} ?> 
Muhammad Asif
  • 456
  • 4
  • 10
0

you can assign ur session value to the js variable and use js variable to trigger the function.

<script>
var checkcall = <?php echo $_SESSION["..."]; ?>
if(checkcall == true){
myFunction();
}
function myFunction() {
    content...
} 
</script>

if u are using jquery you can include calling block in document.ready

<script>
$(document).ready(function(){
    var checkcall = <?php echo $_SESSION["..."]; ?>
       if(checkcall == true){
          myFunction();
       }
});
    
function myFunction() {
    content...
} 
</script>
Rupesh Terase
  • 440
  • 3
  • 14
0

The issue is that myFunction does not exist YET when you try to call it. Since alert works, we know that your script actually executes as expected. So, in order to fix your issue you will need to define myFunction in your proper place.

Let me allow to help you test my statement. Let's try this code:

<script>
    if (typeof myFunction === "undefined") {
        function myFunction {alert("Define me!");}
    }
    myFunction();
</script>

If you see an alert with the text of Define me! then I precisely described your issue. In which case you will need to search for myFunction in your code, see how it is defined and replace the

        function myFunction {alert("Define me!");}

line with the proper definition of your function.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
0

Your PHP function is executing before JS file loads into the browser that's the reason it is not working.

"alert" & "onClick" will work because alert will only display a message you have written and for onClick event when the page is properly loaded then you click the button to perform its action.

If the javascript function is not big then what you should do is write a javascript function within PHP code.

Rahul Kathet
  • 135
  • 7