click() method does not work, why?

[php]

TEST <?php echo "" ?>

HOME

[/php]

Well, your code will work with a minor change depending on what you want it to do.

You have an anchor that tags a webpage to switch to. Then, you override it with Javascript to capture the click execution. An HREF is usually used to switch to a page. The ONCLICK() Javascript option is usually used to do some calculations or some other code before switching the page.

Tell us exactly what you want to happen when the user clicks on your HREF link.
(My guess is that you want to just add the “return” option into it.)

I was expecting the PHP echo at the beginning of the to execute the goHome() function whick should simulate a mouse click on the <a href=… thus loading the page “\index.php”.

This is a simplified example of what I am really thrying to accomplish.

Here is the code I want to use a click() method for.

[php]"; } ?>

Dams: Lilly Quick
etc... [/php]

The code above works fine when the user manually clicks on the choices in the table above.
The p7_ShowPic then switches what

layer is displayed in the browser window according to yhe parameters provided.

The code with the click() method I am trying ro use takes effect when the user comes to this page from anothe another page which passes the ‘dogname’ in the calling URL. PHP retrieves this parameter and calls the selectDog() function passing this dogname as the parameter. selectDog() is then supposed to display the correct

layer by simulating a click on one of the two choices in the table which have the onclick= set up to accomplish that.

Is this understandable?

Thanks so much for helping me with this.

Well, maybe that code works, but, PHP is not calling a Javascript function. It can not do that. At least not by any type of direct call. To explain this, PHP is SERVER-SIDE only and Javascript is CLIENT-SIDE. One way around this is to use Javascript’s “ONLOAD” function. You can use PHP to load a value into Javascript’s command to run once the page is loaded into the browser. This is the page’s ONLOAD function. It is used like this:

... rest of page... What the above does is load the entire page (after PHP has been completed) and immediately jump to the Javascript's "pagedloaded()" function. You can do whatever you need in that routine. You can use PHP in your original page to alter the commands inside the "pageloaded()" function.

In this manner, you are using PHP to alter a Javascript routine that would run as soon as the page is fully loaded into the browser. After that routine completes, the rest of the page is active.

Hope that helps. You should be able to alter your code to run in this manner. Good luck!

Thank you so much for staying with me.

I am using PHP to retrieve the value passed through the URL by the calling page.

Don’t quite understand why PHP cannot execute a java script locally.

[php]

<?php if ($_GET['dogname'] == "Quick") { echo ""; } ?>
Dams: Lilly Quick
[/php]

The PHP code above works and calls the selectDog() script.
The alert is displayed.
But the Click() does not work, although the code is loaded; I know because the body onload= which execute the same function as selectDog() works and is completed before the selectDog() script call by PHP (note that the selectDog() function is defined within a comment section and is not executed inline…

What am I missing?

Okay, once more… You do not understand PHP servers. It’s actually quite simple. I will explain.

First, PHP runs SERVER-SIDE only. It is code that can access databases, handle images, create HTML code AND create Javascript code based on your PHP programming. All of this is executed and finished BEFORE the new page is sent to the browser.

The browser is CLIENT-SIDE only. It has HTML code, Javascript code in it and other such as Jquery/Ajax, etc.
BUT, NO PHP code. PHP only exists SERVER-SIDE. Once in the browser (CLIENT-SIDE), HTML is combined with CSS and is “rendered” to the screen. At that point, FORM controls in HTML and also Javacript code is enabled and can be used.

So, PHP can NOT do anything in your browser. It does not exist! To prove that, just take a page that has PHP code in it and RIGHT-CLICK on it in a blank areas and select VIEW-SOURCE. You will see the live active code AFTER all PHP has been executed and you will NOT see any PHP code in it.

Think of it like this, PHP is SERVER-SIDE ONLY. Everything else is on your personal computer you are in front of which is CLIENT-SIDE.

So, back to Javascript commands, yes, they can be run on your computer CLIENT-SIDE. No, PHP can not touch them at that point. Except, when you create the Javascript, you can use PHP to alter it. I often use PHP to load an array of data from my database and place it inside of Javascript variables for use when the page is loaded. But, this only places data onto the page, not any code from PHP.

Hope all that makes sense. You can still use my routine to start a function when a page loads.
Something like this:

... rest of page...

You would have to change it to run your actual “dog” function instead of my sample. You could use PHP to alter the function called by echo’ing the onload=’<? echo $somevaribleforfunction; ?>()’; In this way you can pass a Javascript’s function name to the actual page and the onload would run it for you. This would work as you want it to.

Well, hope all that makes sense to you. Good luck.

ErnieAlex you got through to me: PHP executes on the server BEFORE the page is loaded :slight_smile:

Here is what I have now:

[php]

[/php]

The click() is executed BUT the value passed by PHP is always “undefined”.
Can you see why?

Yes. And, glad you understand the server-side/client-side. Seems I explain that a lot! I should do a tutorial on it maybe?

Oh, first you need to print the dogname not just pull the posted variable…
So, the HTML line:

should echo or print the dogname, like this:

$_GET is not actually a PHP command but a variable! Also, is this being sent from a form where the user types in a dog name? Is it POSTED to this PHP page? If so, you need to use $_POST not $_GET for this one. If it is sent to this page by URL, like: www.yourdomain.com/dogpage.php?dogname=Ernie then use GET.

Did that make sense? Normally if you POST or GET variables, they are sent from a posted form or from an HREF link. If that did not make sense, you will have to explain what you want this code to do.

Works, almost as you said it would; just had to

[php]<body class=“master” onload=“selectDog(<?php echo "'".$_GET['dogname']."'"?>)”>[/php]

and at the beginning of selectDog(dog) check for empty string and if so set a default target for click().

Thanks a lot for you help

Great! Glad you solved it. Yes, on the change, I mentioned you had to customize it to your dog page…

Nice feeling solving a programming puzzle, isn’t it! I will mark this solved, please come back again…

Sponsor our Newsletter | Privacy Policy | Terms of Service