1

I have done many attempts with posts and examples - but I'm unable to understand how to do this. I have this function in the head of my php file:

    .....
    <script type = "text/javascript" language="javascript">
    function agicol() {
    scheduler.clearAll();
    var idname$ = $idencol;  
    scheduler.load("ret_l.php?connector=true&dhx_filter[IDCol]="+idname$,"json");
    }
 </script>  
 </head>

And in the body, I use a variable like so:

  <body bgcolor="#C0DFFD" onLoad="init();">
  <?php
   if(isset($_GET['id']))
   {
   $idencol = $_GET['id'];
   echo "<script>alert($idencol);</script>";
   // Alert works properly
   // $idencol contains id sended
   }
   ?>

So, I should call function agicol(), and pass getted id $idencol.

Can someone tell me how?

Thanks in advance

4
  • 2
    Surely just replace alert($idencol) with agicol($idencol) Commented Nov 24, 2015 at 15:56
  • you're not passing an id in the previous page. you're passing connector and dhx_filter[IDCol], neither of which would produce $_GET['id'] for you. Commented Nov 24, 2015 at 16:01
  • @MarcB Thanks, but isn't clear form me. I try to explain better: From home page, i click on link passing id to a new page in which i have the code above. On load, this page gets id sended correctly. Now, i should want to pass this id and call function in head of this page. But method suggested by Chris Evans doesn't work.I obtain this error "Call to undefined function agicol() in...." Commented Nov 24, 2015 at 16:09
  • then you need to put the function agincol() {...} stuff into that page too, which means you'd be better off putting it into an external .js file and link it to both pages. Commented Nov 24, 2015 at 16:36

3 Answers 3

1

You need to call the js function agicol() in the header which uses the js variable $idencol (very bad variable name here) set by php in the body.

The problem in your code is that the PHP part doesn't set the expected js variable properly. Change your PHP part like this (notice the escape \$ I have used):

<?php
   if(isset($_GET['id']))
   {
      $idencol = $_GET['id'];
      echo "<script>";           
      echo "\$idencol = $idencol;";   //Set JS global variable you expect in the function
      echo "agicol();";               //Call the function
      echo "</script>";

   }
?>

This will solve your current problem.


But there are many better ways to do it. You can set your JS function to accept an argument and send the id through that rather than using a global variable with a bad name.

function agicol(id) {
    scheduler.clearAll();
    scheduler.load("ret_l.php?connector=true&dhx_filter[IDCol]=" + id, "json");
    }

... in PHP in the body

$idencol = $_GET['id'];
echo "<script>agicol($idencol);</script>";
Sign up to request clarification or add additional context in comments.

4 Comments

Consider the alternative method I proposed. You should do it at least that way. Also understand the way the data is passed from php to js.
Yes I have just used the second method you have suggested and it works good.
Rather than calling it "passing data" I would like to say that "php is assembling the js script with hard coded values". That's how it happens here.
I'm sorry. Sometimes i am not able to translate what's on my mind, so i use bad terms or bad expression !
0

load will strip out the <head> (or rather, will extract the content of the <body> and discard everything else) from the document you are loading.

Either:

  • Define your function in a JS file you just load throughout the site (this is generally the best approach)
  • Move your script to the body
  • Replace load with ajax and extract the various bits of data you care about manually

3 Comments

I know there are many method to obtain this result, however is it just impossible to call this function from php code?
@Jayelef — Since the PHP executes on a different computer to the JavaScript, yes, it is impossible. You have to deliver the JavaScript to the client and get the client to execute it. Your current approach delivers the JavaScript to the client and then throws away the <head> containing half the code you wanted to execute.
Thanks for your explantion @Quentin
0

You can try something like: echo "<script>agicol();</script>"; Otherwise, you should update your javascript function to pass param.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.