Mixing PHP and Javascript

T

Tim Streater

David Williams said:
Hello all,
I am having a really hard time mixing javascript and php together.
Does anyone know of a good webpage or FAQ that talks about what the rules
are?

Of special note:
Can we use multiple <script></script> enclosures in a document like we do
with <?php ?> or is that forbidden?

Can I make a databsae connection using php inside of <script></script>
tags if I separate using <? ?> tags.

Yes, if you need to. You might for example have some PHP code inside
your JavaScript that will load a JavaScript array depending on what it
finds in the database. Then you might use that array to load a <select>,
so the available choices for the user vary depending on the database
contents.
 
D

David Williams

Hello all,
I am having a really hard time mixing javascript and php together.
Does anyone know of a good webpage or FAQ that talks about what the rules are?

Of special note:
Can we use multiple <script></script> enclosures in a document like we do
with <?php ?> or is that forbidden?

Can I make a databsae connection using php inside of <script></script>
tags if I separate using <? ?> tags.

These are just a few of the problems I am having mixing the two not to
mention the huge problem of php not being able to read a javascript var without resubmitting the page since php is server side and javascript is client side.

Thanks,
David
 
E

Evertjan.

David Williams wrote on 29 okt 2007 in comp.lang.javascript:
Hello all,
I am having a really hard time mixing javascript and php together.
Does anyone know of a good webpage or FAQ that talks about what the
rules are?

If you mean by "javascript" clientside javascript running in the browser.
and by PHP a serverside language running on the server,
it can safely be said they do not mix.

The server, perhaps using PHP, or ASP-javascript renders a [usually html]
stream, that is sent to the client browser and that DOES NOT contain any
PHP or other serverside codes.

Do a view-source on your browser and you will notice tha absense.
Of special note:
Can we use multiple <script></script> enclosures in a document like we
do with <?php ?> or is that forbidden?

Does it "work"? Did you try?
I would guess testing is better than theory for you on this Q.

With "document" one can mean many things, but I like it this way:

A php-file on the server can be called to run on the servers php engine.
The resulting html stream is sent to the client.
That steam running on the browser as a DOM document.
Can I make a databsae connection using php inside of <script></script>
tags if I separate using <? ?> tags.

Since serverside code has stopped running when
<script type='text/javascript'></script> is executed on the client:
NO, no serverside action is possible AT CLIENTSIDE RUNTIME.
[AJAX cloaking of later server/client interaction goes to far for the
moment]
These are just a few of the problems I am having mixing the two not to
mention the huge problem of php not being able to read a javascript
var without resubmitting the page since php is server side and
javascript is client side.

Yes, that is the nature of things, but has nothing to do with the two
languages as such, but with the fact the they run on different machines.

Running two kinds of Javascript,
one on an [ASP] server and the other on the browser is very possible,
like this:

<script type='text/javascript'>
<script language='javascript' runat='server'>
var serversideVariable = 17
response.write('var clientsideVar = "' + serversideVariable + ';);');
</script>
alert(clientsideVar );
</script>

I would not call that mixing myself,
as they are separated by their running time.

I do not speak PHP, but the possibility should be the same.
 
H

Hal Rosser

David Williams said:
Hello all,
I am having a really hard time mixing javascript and php together.
Does anyone know of a good webpage or FAQ that talks about what the rules
are?

Of special note:
Can we use multiple <script></script> enclosures in a document like we do
with <?php ?> or is that forbidden?

Can I make a databsae connection using php inside of <script></script>
tags if I separate using <? ?> tags.

These are just a few of the problems I am having mixing the two not to
mention the huge problem of php not being able to read a javascript var
without resubmitting the page since php is server side and javascript is
client side.

Thanks,
David

You just have to take into account the fact that PHP runs on the server -
and its output can include javascript as well as HTML.
The Javascript is along with the html page to the browser - then the Browser
executes the javascript.

You just have to have server-side (PHP) and client-side (Javascript) clear
in your mind as to when each is executed.
PHP can write javascript to the page, for instance, but Javascript can't
write PHP (and expect it to work).
HTH
 
D

David Williams

Thanks everyone for your help.

Yes, I can write multiple <script> </script> enclosures using echo in php.
It was not working before when I tried prior to asking for help.
Like this:

echo 'javascript_array2.push(<?php echo $php_var; ?>);';

What was tripping me up was the semicolon ending the javascript statement.
Of the three javascript books I have referenced, I recall them saying
the ; was not necessary after a javascript statement.

I believe it is necessary when mixing with php because it somehow separates
the two.

I will continue to try this and see how far I get.

David Williams
 
T

Tim Slattery

David Williams said:
Hello all,
I am having a really hard time mixing javascript and php together.
Does anyone know of a good webpage or FAQ that talks about what the rules are?

The main rule is that PHP runs on the server. The result of that -
including Javascript code - is sent to the browser. Your javascript
code will then run on the browser.

(This assumes client-side Javascript code. Since you're running PHP on
the server, the chances that your Javascript is server-side is just
about nil.)
Of special note:
Can we use multiple <script></script> enclosures in a document like we do
with <?php ?> or is that forbidden?

Yes said:
Can I make a database connection using php inside of <script></script>
tags if I separate using <? ?> tags.

Yes. Remember that your PHP code is building an HTML file to be sent
to the client. Part of that file is the <script> blocks, and it's
perfectly kosher to build those blocks using server-side code.

If you're thinking of making a database connection on the server side,
and expecting your client-side code to use that connection: no, that's
not going to work. The connection is from the server to the DB, not
from the client.
 
V

vdfvdfv

Thanks everyone for your help.

Yes, I can write multiple <script> </script> enclosures using echo in php.
It was not working before when I tried prior to asking for help.
Like this:

echo 'javascript_array2.push(<?php echo $php_var; ?>);';

What was tripping me up was the semicolon ending thejavascriptstatement.
Of the threejavascriptbooks I have referenced, I recall them saying
the ; was not necessary after ajavascriptstatement.

I believe it is necessary when mixing with php because it somehow separates
the two.

I will continue to try this and see how far I get.

David Williams








--
David Williams
Georgia Institute of Technology, Atlanta Georgia, 30332
Email: (e-mail address removed)- Hide quoted text -

- Show quoted text -

The ; might cause many problems even when not using php
Better use it, just in case...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top