I am self taught in php only. now im starting to learn javascript as well. I have noticed so far that I can dynamically creat my pages, forms, validation etc... with js just like i can with php. (at least that seems true so far from what i am learning). My Q is: Is one better to use for this? if so, then why. It seems to me that page creation and other basics being dynamically created client side would be better than being created server side. Am I correct in this thinking?

Dani AI

Generated

As noticed, both PHP and JavaScript can produce HTML, but they serve different roles. PHP (server-side) is best for authoritative tasks: database access, access control, producing markup that must be available to bots or users with scripting disabled, and enforcing business rules. JavaScript (client-side) is best for interaction and responsiveness: inline validation for UX, partial page updates, animations and richer controls. Treat client-side work as an enhancement, not the security boundary.

Decision points that help choose where to generate content:

  • If content must be indexed, previewed (social/email), or work without JS -> render on the server.
  • If the UI needs frequent, fine-grained updates without full reloads -> render parts in the client and use server APIs.
  • For single-page apps, serve an initial HTML shell from the server and expose JSON APIs; add server-side rendering or pre-rendering if search/faster first paint is required.
    This ties back to and : a full site can be built using client tech, but it usually requires a server API and extra work for SEO/accessibility.

Security and correctness: never rely solely on client-side checks. Client validation improves UX; server validation enforces integrity. Use parameterized queries and escape output according to context to prevent SQL injection and XSS. Minimal PHP example showing server-side validation with PDO:

// server-side (PHP)
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
if (!$email) {
    http_response_code(400);
    echo 'Invalid email';
    exit;
}
$stmt = $pdo->prepare('INSERT INTO users (email) VALUES (:email)');
$stmt->execute([':email' => $email]);

Practical path and tips for : learn core DOM/events, JSON, Fetch (or XHR), and async patterns; practice unobtrusive progressive enhancement; use a lightweight library for cross-browser quirks if needed but prefer modern web APIs when possible. Debug with browser DevTools and server logs; test with scripts disabled and on mobile. Keep scripts out of critical render paths (defer/async), minimize DOM thrashing, and always revalidate on the server.

Recommended Answers

All 12 Replies

I'll ask a question back: how are you going to provide a full website (menu's and content) to your user, using only javascript.

Which is better for your needs, a van or a 4 door sedan? Without more info, the only real answer is "it all depends".

You don't need to choose because you can use both. Some things can only be done in Javascript but having that within a PHP context gives you the server side with a database and so forth. I have used JQuery within PHP programs and that works quite well and allows you to have the best of both worlds.

well, I am obviously a noob..lol Its just from what i have seen of js, it can be used to write html just like php. aside from database usage.
So, traditionally, i would use php as primary.. for outputting html, querying DB, etc... ???. I was just kinda thrown for a loop when i started learning js (today), and started seeing that it had alot of the capabilities as php. I have been using php for everything so far. Now its time to expand into js I think. I have no other background or education in programming other than what i've learned in php. I appreciate all your feedback. :)

just a suggestion:
javascript is very complicated... specially when your coding native javascript... i recommend you make use of the most popular javascript library today... which is jQuery; It will make your life more easier... ^_^

Member Avatar for Member #120589

php will create pages that everybody can use as it is 'served' via your web server. js can only work if it is enabled (to varying degrees) by the user's browser. Some browsers have compatibility issues, so native js can be problematic. Thankfully js libraries/frameworks such as jQuery take out the pain. Mind you, if an user turns off js - they won't be able to use your site properly, unless you have some sophisticated fallback (known as progressive enhancement).

Ajax is all the rage at the moment, and this *usually* uses php + js working together - js calls a php script which may then be further processed by js. Very useful for refreshing/loading parts of a page without a full page reload. It can also be used to update a db/ send email etc etc without page redirect/reload.

just a suggestion:
javascript is very complicated... specially when your coding native javascript... i recommend you make use of the most popular javascript library today... which is jQuery; It will make your life more easier... ^_^

Thank you, that has been another one of my issues... trying to get some direction as I am totally new to all of this. I appreciate your suggestion. :)

in answer to your question, i would hesitate to answer based on the fact that you haven't said what your creating. You cannot possible create an entire website from source code? Even if it is possible it take maybe many hundreds of lines of code to do.

If you are talking about small pages such as on the fly pages with dynamic content, then PHP definitely as it runs and executes much faster than javascript.

You cant choose JS over PHP they are designed to accomplish different tasks.
Combine them both to have a powerful and dynamic website.

Awesome advise guys.. thanks. I am trying to get into web development & design. So, i greatly appreciate the advise... all of you! :) so, I should probably learn some basics of javascript to learn the syntax (which i've noticed is very very similar to PHP), and then focus on something like ajax? So, far i have learned the basics of php, all the loops, arrays, cookie, sessions, form submission etc... I think i have a good foundation with php so far. pretty much all the common stuff that's on most web sites (as far as php goes) I can now do fairly comfortably.

Member Avatar for Member #120589

OK, I see that this is solved, but just another observation - as mentioned previously, php and js are used for different purposes. Although there can be some crossover in functionality, The 'site workhorse' should be server-side (php). The 'sparkle' comes from js. Sites can function just fine without any js. In fact, js is slooow, and should, in my opinion be used sparingly. If you've accessed a site via your mobile/Blackberry etc, you'll see 'running script' about 5 times on heavy js sites. By the end of all that, I've lost the will to live.

If you can reduce the js to a minimum, mobile users will thank you. You could always design an alternative site for mobiles though.

yes ajax is more advance it's the combination of PHP and javascript... that allows you to request data on the server without refreshing the page

fuston05,
As mentioned a couple of times, you will get the benefit of a lot of good Javascript work by using JQuery. I have used it very successfully without any great understanding of javascript. Personally, I find Javascript more complicated than PHP so I'd just as soon let someone else take care of the details. Most common things have already been developed more than once as JQuery Plugins so you have a wide choice of all sorts of effects. Ajax is useful if you want forms fields that react to what you enter without refreshing the whole page but it isn't intended to address the wide range of features that you get using a Javascript library like JQUERY.

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.