So I've given RequireJS a try this weekend, and although it appears to work pretty smooth, I can't figure out one thing: How do I combine PHP and Javascript while using RequireJS?

See, RequireJS can only work with .js files, meaning that it cannot "require" .php files. In many files, however, I use PHP to retrieve database data which is then used by Javascript. It feels pretty inefficient to me to take apart these processes and let my Javascript execute an AJAX request to retrieve the settings I can normally set with PHP directly. Is there anyone with experience with this that maybe knows a solution or a way how to deal with this?

Re: Combining Javascript & PHP with RequireJS 80 80

It feels pretty inefficient to me to take apart these processes and let my Javascript execute an AJAX request to retrieve the settings

That's the normal flow of things. RequireJS is used to load Javascript modules. There's no way to include PHP on the client-side, or to let Javascript connect to your database.

Re: Combining Javascript & PHP with RequireJS 80 80

Yeah, I thought so.. Too bad, I think I'm just gonna stick with what I have for now, then. I had hoped for a way to let RequireJS "require" a .php file, which would actually be Javascript but with some PHP in it. Thanks for your answer!

Re: Combining Javascript & PHP with RequireJS 80 80

Depending on your situation, you could use AngularJS.

Okay it's not perfect, but it is backed by the Google development team and is an extremely efficient way of combining client+server side code. It doesn't 'require' PHP as such, but pretty much the same thing. It would be able to do the same thing when handling your 'settings'.

Personally I think it's pretty cool, and there's an example of a simple SQL query with PHP on this page.

Re: Combining Javascript & PHP with RequireJS 80 80

I have heard of AngularJS, but I've never really looked into it. Does seem interesting, thanks for the tip!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.