Re: PHP inside Javascript
| From: | Lars Torben Wilson | Date: | Tue, 05 Sep 2000 02:44:31 +0000 |
| Subject: | Re: PHP inside Javascript | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-15211@lists.php.net to get a copy of this message | ||
Samantha Savvakis writes:
>
> Hi all,
>
> I would like to put some php inside javascript. I have done this before, but
> this time it's slightly different.
>
> The problem I'm having is that the PHP is being executed, despite the fact
> that it's inside a javascript function that hasn't been called yet. This is
> what I want to do:
> ------------------------------
[trim]
> No matter what I do, the php is being executed and $error always has a
> value. Is there a way I can do PHP validation inside the javascript function
> and it not be executed?
>
> Thanks,
> Sam
Not in the way you're thinking. PHP executes server-side; Javascript
executes client-side. The PHP has been executed and is all done before
the Javascript even makes it to the browser, where *it* gets
executed. And the PHP parser has no concept of Javascript functions;
it's all just text. So PHP can't know whether a given Javascript
function is going to run or not.
You'll need to take that into account and perhaps have PHP generate
the appropriate Javascript code to send to the browser. But remember
to keep in mind that they don't share execution times. For what you're
trying to do (client-side form validation?), why not use pure
Javascript for the validation and then submit it to a PHP script for
processing on the server? Better yet (unless it's for an Intranet or
otherwise captive audience where you *know* they have Javascript
turned on) do it all in PHP.
--
+----------------------------------------------------------------+
|Torben Wilson <torben@php.net> Netmill iTech|
|http://www.coastnet.com/~torben http://www.netmill.fi|
|Ph: 1 250 383-9735 torben@netmill.fi|
+----------------------------------------------------------------+