Re: PHP inside Javascript
| From: | David Robley | Date: | Tue, 05 Sep 2000 02:49:54 +0000 |
| Subject: | Re: PHP inside Javascript | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-15215@lists.php.net to get a copy of this message | ||
On 5 Sep, Samantha Savvakis wrote:
>
> 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:
>
> ------------------------
> <BODY>
> <SCRIPT language="javascript">
> <!--
> function NextPage()
> {
> <?php
> if ($username == "")
> {
> $error = "You must enter a username";
> }
> else if ($address == "")
> {
> $error = "You must enter an address";
> }
> else
> {
> ?>
> document.frmDetails.submit();
> <?php
> }
> ?>
> }
> //-->
> </SCRIPT>
>
> <FORM name=frmDetails>
> <INPUT type=submit name=btnNext id=btnNext value="Next>>"
> onclick="NextPage()">
> </FORM>
> </BODY>
>
> ------------------------------
>
> 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
>
You are forgetting that PHP is a server side application and javascript
is client side. ALL the PHP code is executed before any output is sent
to the browser, and only then is any javascript able to run. You might
have better luck doing your validatin with js, but it's better technique
not to rely on server side validation alone.
--
David Robley | WEBMASTER & Mail List Admin
RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/
AusEinet | http://auseinet.flinders.edu.au/
Flinders University, ADELAIDE, SOUTH AUSTRALIA