Forum Moderators: open

Message Too Old, No Replies

PHP and Javascript

Can php script be written in java script?

         

shallow

11:22 pm on Jan 19, 2006 (gmt 0)

10+ Year Member



I am planning to have a script written that will be used with Amazon Web Services. The script I want to use requires PHP 5 but it will be some months before my host upgrades from version 4.x to 5.

Question: can something be written in javascript and work just as effectively as if it had been written in php? Can both do the same thing, in this case, feed updated, current information to my site whenever the page is visited or refreshed?

Getting this up and running in a timely manner is quite important since January - March is the time in my niche where major announcements are made for new products coming to market.

JAB Creations

12:06 am on Jan 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can use PHP inside of JavaScript, but ultimately your JavaScript (on file not linked in assumption) will be inside of PHP ultimatly.

You'll have to escape single quotes....

JS without being in PHP...

onmouseover="this.className='rolloverh'"

JS now inside of PHP...

onmouseover="this.className=\'rolloverh\'"

So escape single quotes but putting the backslash before it.

Without asking a more specific question though I'm not sure what best example or what suggestions would be better for me to suggest.

ooba

2:08 am on Jan 21, 2006 (gmt 0)



If I understand correctly, Shallow is asking whether a script that is written in JS *instead of* PHP can work equally effectively with Amazon Web Services.

This question caught my eye because for the last few days I've been wondering the same thing and trying to figure out the answer. So far I'm concerned about two things:

(1) I'm wondering if there could be a problem with people turning off JS in their browsers, in which case they won't see the dynamic content from Amazon.

(2) I'm wondering if it's possible to write JS code for parsing XML that will run in all browsers. So far I'm getting the impression that it's not.

On the optimistic side of the ledger, I'm also wondering if it's possible to use Amazon's XSLT service to parse the XML, which might knock out concern #2.

Like Shallow I'm very curious to hear more responses about this.

Tapolyai

3:16 am on Jan 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First, Javascript is a client-side code, and PHP is a server side.

That is the Javascript code is sent as part of the HTML page to the client, then the browser interprets it (or ignores it). The server does not care or interpret the Javascript.

PHP is first interpreted at the server, and most often the PHP interpreter generates an HTML on the fly from the result of the PHP code. That code is sent to the client. The client does not care or see, the PHP code.

The Javascript code CAN do a lot of things PHP cannot, and vice versa. Javascript can validate an input field on a form or take an action on mouse click without talking to the server. This is because all those activites are at the client side, and done by the browser.

PHP can access databases, and manage external files, draw new documents in and create images on the fly for example. But, at the end - when all the stuff is sent to the client, it is only HTML.

Now... you CAN combine the two. You can make a PHP code which generates Javascript dynamically. And, with AJAX a "newer" version of Javascript, you can trigger a piece of PHP on the server, and make it generate a new piece of HTML code.

To answer some of the other questions, yes users can turn off Javascript. Yes, you can parse XML code in Javascript. For IE you would use "XMLHTTP ActiveXObject", for Mozilla/Firefox/Opera/Safari use "XMLHttpRequest".

DrDoc

6:32 pm on Jan 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



... not to mention, though, that for tasks that are similar in function between JavaScript and PHP (such as parsing XML), PHP is immensly faster (and more reliable). You don't want to handle feed updating/retrieving using JavaScript, since that would expose sensitive information to your users through the JavaScript code.

As a side note -- why does the code require PHP5? I am positive the same tasks can be written/achieved using PHP4. If nothing else, the few portions that would require PHP5 could be rewritten to accomodate PHP4. In all cases -- the code should probably be created to support both, which is not hard to do.