This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

why use php to create javascript?

Forum to discuss ExtJS and other JS plugins

why use php to create javascript?

by cwheat503 » Sat Jul 03, 2010 3:06 am

I am new to the frame work, not even finished with the jobeet tut. But I am wondering why there is a need for the javascript wrappers?

In my mind the reason for a framework is to separate out your code. View logic stays in the view (mostly html), you have your models (business logic/data) and your controllers.

So why mix javascript in php, why not just create the javascript in the view?

What are the advantages to using a helper like the jquery reloaded plugin? vrs just creating the JS in the view file?

I am not trying to flame so don't take it that way I am really trying to understand how/why all the pieces fit together.
cwheat503
Junior Member
 
Posts: 16
Joined: Mon Jun 28, 2010 10:23 am

Re: why use php to create javascript?

by halfer » Mon Aug 23, 2010 11:15 am

I think PHP used to create JS snippets is still in the view layer, so it's in the right place. It's just a shortcut, often used to set up commonly-used ajax operations etc. However I personally never got on with it, and do everything in raw jQuery... but perhaps if I learnt, some things would be quicker to code! It's a trade-off between convenience and understanding everything at the low level.
halfer
Faithful Member
 
Posts: 10148
Joined: Mon Jan 30, 2006 1:16 pm
Location: West Midlands, UK

Re: why use php to create javascript?

by garak » Sat Sep 18, 2010 4:09 pm

chrisadam wrote:There are times when you might want to fetch stuff from a database using PHP and then spit out a JavaScript file. For example, many image slideshows are controlled by javascript - but what if the image references are stored in MySQL? You can use PHP to fetch the info, and then include the javascript/php file in the <head> of your HTML page, something like this:

<script type="text/javascript" src="/includes/slideshow.js.php"></script>
The above file is being called by the browser as a JavaScript file - but because it has the “.php” extension, the server knows that it has some php work to do before it delivers the file to the browser.


If you do this, you are doing it in the wrong way.
You should never let javascript control anything directly. You should rather do things in HTML, then add your javascript enhancements.
User avatar
garak
Member
 
Posts: 34
Joined: Fri Dec 07, 2007 3:21 pm
Location: Rome, Italy

Re: why use php to create javascript?

by halfer » Sat Sep 25, 2010 10:35 am

@garark - I've deleted the user you responded to here. It was cleverly disguised link spam :|
halfer
Faithful Member
 
Posts: 10148
Joined: Mon Jan 30, 2006 1:16 pm
Location: West Midlands, UK