0

I have searched all over the internet to run a function from URL and found out it is not possible.

I am asking if there is another way to do this. I will explain below what I am trying to achieve.

On my website I have several functions that when a hyperlink is clicked on that cracks.php page, the main content changes accordingly. Now I want to direct people from a single URL to that cracks.php page with the function already called. Example - www.example.com/cracks.php?AC ( which will call a function named AC and the content changes before the page loads)

Ive found this method below, but couldnt get it to work.

if(document.location.search == '?AC')  
{  
    AC();  
}  

Sorry for the messy code on the website. Thanks for reading, any help would be appreciated.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
Elemen7s
  • 3
  • 1

3 Answers3

0

You can call www.example.com/cracks.php?do=AC and then get do with $doMethod = $_GET['do'];. What you then do is, use a switch function or a few ifs to check and execute when e.g. $doMethod equals AC.

Like this:

$doMethod = $_GET['do'];
switch($doMethod)
{
    case "AC":
        //some random stuff to do
        break;
    case "BD":
        //some random stuff to do
        break;
    case "XY":
        //some random stuff to do
        break;
    default:
        break;
}
Pangolin
  • 7,284
  • 8
  • 53
  • 67
  • I also Tried your method but nothing happened. I tried both in internal(in the head) and in external. Any help, maybe im missing something – Elemen7s Jun 14 '11 at 11:17
  • Are you aware that my solution is coded in `PHP` and not `JavaScript` ...? Since you tagged it as `PHP` I gave the answer in that language. – Pangolin Jun 14 '11 at 11:25
  • Oh, OK sorry, my fault then :D . Now that we started on php I might keep using it. Can I call a javascript function from php ? If no, a javascript method would be really appreciated.I am writing the same messages to both of you as I am not sure which method I am going to use. – Elemen7s Jun 14 '11 at 17:42
  • Thats cool. No, you can't call JS from PHP or visa-versa. They are completely independent. PHP runs on the server and treats JS as characters (same as text) and send it all through to the client where JS lives. By the time JS is activated, the PHP code has been processed and gone. (Not visible to JS) But for your problem, this PHP solution is a good way to go. http://www.w3schools.com/php/default.asp – Pangolin Jun 14 '11 at 19:04
  • Thanks for helping but solved it by using a Javascript method. Thanks again ;) – Elemen7s Jun 15 '11 at 07:21
0

That depends if you need to do that dynamically or you can do it hard coded. Because that hard coded is too simple (with if's and switches), what you have to do is:

$functionsList = Array('func1', 'func2');


function func1(){
    echo '1';
}

function func2(){
    echo '2';
}

if (function_exists($_GET['f']) and in_array($_GET['f'], $functionsList)){
    call_user_func($_GET['f']);
}

Then call your_file_name.php?f=func1 and your_file_name.php?f=func2 and you'll see different outputs.

Pedro M. Silva
  • 1,298
  • 2
  • 12
  • 23
  • Tried your method but nothing happened. I tried both in internal(in the head) and in external. Any help, maybe im missing something – Elemen7s Jun 14 '11 at 11:17
  • That's strange because I tried it in my computer and it worked. Did you run this code and it didn't worked? Or have you changed it? If so, be sure the funcion exists and it's listed in the `Array` **$functionsList**. _PS:_ This is PHP, as @Nideo code, not JavaScript. You must be aware of that. :) – Pedro M. Silva Jun 14 '11 at 16:39
  • Oh, OK sorry, my fault then :D . Now that we started on php I might keep using it. Can I call a javascript function from php ? If no, a javascript method would be really appreciated. I am writing the same messages to both of you as I am not sure which method I am going to use. – Elemen7s Jun 14 '11 at 17:43
  • Directily no. However you can print with PHP some JavasSript that will call the JavaScript function. – Pedro M. Silva Jun 14 '11 at 17:45
  • Is your method possible with Javascript ? If no, what should I use to print Javascript that will call the functions ? – Elemen7s Jun 14 '11 at 17:50
  • It is possible, but not the way I'm doing Someone with more experience of JavaScript would know how to do it, I can't. ;) – Pedro M. Silva Jun 14 '11 at 17:56
  • OK, then I should re post the question with a javascript tag. Thanks for helping :) – Elemen7s Jun 14 '11 at 18:02
0

With the help of Mark Koopman I managed to use his Javascript method and it worked like I wanted.

So heres the method in Javascript:

<html>
    <body>
        <script>
            function handleOnload()
            {
                if(location.search == "?AC")
                   alert("the query string is " + location.search);
            }

            window.onload=handleOnload;
        </script>
    </body>
</html>
sjngm
  • 12,423
  • 14
  • 84
  • 114
Elemen7s
  • 3
  • 1