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?

Recommended Answers

All 4 Replies

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.

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!

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.

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.