RE: Beginner question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



-----Original Message-----
From: mike bode [mailto:mikebode1@xxxxxxxxxxx]
Sent: 25 August 2009 07:16 AM
To: php-general@xxxxxxxxxxxxx
Subject:  Beginner question

I am trying to use PHP on my web site I am developing now. I have installed
Apache 2.2 and PHP 5.2. My problem is that I can execute PHP code embedded
in my HTML code, but I can't execute the same cose when I put it into a
separate .php file that i then call from within the html code. for example:

I have a html file with

<body>
<?php
echo "Printing with php";
?>
<?body>

and it works fine.

When I take out the php code and put it inot a file pp.php, and I call that
file with my browser ("localhost/pp.php"), it works, too.

But when I change the html code to:

<body>
<script type="text/javascript" src="pp.php"></script> </body>

I get a blank page. this is probably something really stupid, but I have
been wrecking my head for days now, and I can't figure it out. anybody has
an idea?

--

I presume your html/php code is in a file with a .php extension? If not then
change the extension to "php" because most other webservers won't parse php
code in a html file. If you have a .php file you can mix html and php the
way you described, and you can put your php code between the <?php and ?>
tags.

The php script you're trying to include in your html code with javascript
<script type="text/javascript" src="pp.php"></script>
would presumably echo out the javascript code that ends up in your html
page, but if you don't know php then you're trying to run before you can
walk.

If possible try to see what javascript code is being generated (echoed) by
the php script and put it in a normal .js file (e.g. pp.js) and then use it
like any other javascript file
<script type="text/javascript" src="pp.js"></script>
This isn't always possible because often php is used to decide what
javascript to output (otherwise why bother). If you have to use php to
generate your javascript code then start with small steps - try echoing some
simple js code in your pp.php (like an alert) just so you can see how it
works. Once you get that working you can move on to more useful things.


To illustrate, what's actually happening with the code you have at the
moment is this:

The browser parses the initial html code and finds
<script type="text/javascript" src="pp.php"></script>
And sends a request back to the server for pp.php

The server runs pp.php and send the output generated by the php script back
to the browser,
so effectively the browser ends up with
<script type="text/javascript">Printing with php</script>

Obviously that isn't going to work. Hopefully you can now see what the
problem is and how to fix it.


Cheers
Arno





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux