1

Hi there I want to ask something very simple , unfortunately I run out of ideas , I want a function when is being $GET to do a JavaScript function straight away, but I just want to make sure that's how you do it...

<script>
function watch()
document.getElementById("home").style.display="none"; 
document.getElementById("watch").style.display="block";
</script>	


<a href="index.php?id=8">8</a>

<?PHP
if(isset($_GET['id'])) {
watch ()  <<<ISSUE
//run that function if ittselts GET
}
?>

1
  • This is a problem function function - cannot call functions using one of the keywords Commented Jan 25, 2015 at 14:47

4 Answers 4

3

Is easy, just do the following:

<a href="index.php?id=8">8</a>

<script>
var myFunction = function() {  
 ...
};

<?php
if(isset($_GET['id'])) {
   echo 'myFunction();';
}
?>
</script>

When you receive the id parameter, it will print the code to run the JS function, just put this <script> tags just before </body> so it can be executed after the DOM has been lodaded.

Sign up to request clarification or add additional context in comments.

2 Comments

If Its That Easy Thanks :)
Try it, if it works select this as an answer, if not, here i am to help you
1

Replace the line with the issue with:

    echo "<script>watch() </script>";

Because watch is a Javascript function, and you are calling it like a PHP function.

Comments

1

There are two problems in your code

  1. you cannot name your function "function" since it is a keyword

  2. you'll need to echo the JavaScript function call with php so that it is printed on the document sent to client.

Comments

1

Simply use like below:

<a href="index.php?id=8">8</a>
 <?PHP
   if(isset($_GET['id'])) {
     echo "<script> myFunction () {
                 ...........
          }
       </script>";
 ?>

1 Comment

Thankyou , I knew this one , I just needed it a bit separate :)

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.