#php vs JS

1 messages · Page 1 of 1 (latest)

hallow dust
#

what do you guys think about doing the front-end with JS and php?
is that how they do it?
like, php for connecting with the DBMS, and JS for the just in time changes

reef nacelleBOT
#

<@&987246964494204979> please have a look, thanks.

reef nacelleBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

frosty panther
# hallow dust what do you guys think about doing the front-end with JS and php? is that how th...

not sure who "they" is but this is a simple and fairly useful setup. Thats not 'front end' tho. Thats full stack. PHP is a backend language, but its also responsible for outputting the HTML buffer. JS is similar to PHP in that it can contain both HTML/css/frontend stuff, as well as 'backend' logic.
These things (JS and PHP) go together tho, unless you're using nodeJS as the backend server, which is possible but I dont prefer it. Most of the time a website language is referred to by the backend (e.g. PHP, Node (JS), Java (Spring), C# (ASP.NET), etc). Everything else (HTML, CSS/SASS, JavaScript/Typescript, etc) is going to basically be the same. The generation of the HTML will differ though.
Then each backend server can communicate with any database, some are easier/harder than others. like in PHP it is pretty easy to connect to mysql (unless ur php.ini disables it).

So in your proposed setup yeah you'd have PHP on the backend connecting with an SQL database, then that PHP backend also generates the HTML (preferably with separation of duties [frontend and backend code loosely coupled / isolated]). Then CSS frontend, with Javascript for extra frontend functionality, as well as intercepting HTML submissions

hallow dust
#

with they I meant like

#

the professionals

frosty panther
#

Some professional businesses work in PHP, but not many

ember valley
#

well, thats a strangely incoherent question

frosty panther
#

Most major companies use more robust solutions

ember valley
#

tons work in php, but not the ones that pay well ^

hallow dust
#

was frontend

#

even database querys

frosty panther
#

those arent and should never be done by the client

hallow dust
#

sorry

#

not database querys

#

but like sending the data to the DBMS

frosty panther
#

still the client doesnt do that

hallow dust
ember valley
frosty panther
#

the browser/javascript sends data to the backend server which processes it, communicates with the database (preferably after preventing sql injection), figures out what it wants to respond with, then sends it back to JS or the browser

ember valley
#

It's just a sign you are coming at this from a strange base

hallow dust
#

you don't use php on the front end?

#

on facebook, they use it

frosty panther
#

facebook and php are funny. They rewrote an entire php interpreter to speed it up with C++

#

thats just the url

#

it is in a variant of php, but you dont necessarily have to respond to /login.php

#

i could make my java server respond to login.php

#

a php server will look for a visible /login.php file for the above request, and if it doesn't it should go to index.php

#

assuming login.php exists, then it starts executing the script such as

<?php
require_once "dbcom.inc.php";
require_once "anythingelse.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Login - Facebook</title>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="/css/main.css">
</head>
<body>
  <h1>Login Senpai</h1>
</body>
</html>```
#

in this case PHP isnt being used for anything to alter the html stream

hallow dust
#

so

#

this is an exception

#

normally, you wouldn't use php on the front?

#

just JS

#

and then node.js or php on the side of the dbms

#

so you can like send data from the front with JS to the back

frosty panther
#

that example is still not php on the frontend explicitly

#

PHP is a server side language

#

The PHP server listens for requests, see's /login.php or /login, and looks for a script to execute, under which it collects HTML output strings', and returns the result to the user after the script executes

#

you can also have classes and datastructures and database connections, fancy algorithms, whatever, in the php server. None of that is given to the user. Only what is echoed or directly passed to the output stream

torn prism
#

Node.js & PHP, when on a Java discord server? 🤨

frosty panther
#

this is why originally i said PHP is sort of full stack. Both backend and frontend code are written in it (preferably in separate places). Its not explicitly one of the other. Whereas HTML and CSS are literally only frontend

frosty panther
#

and technically javascript in the browser is front end but also can act like backend for advanced logic

#

let me make a short example with both

hallow dust
#

okay

torn prism
#

PHP is solely back-end, it does not run client-side. It serves content which gets rendered client-side

frosty panther
#

true, but frontend developers could still write HTML inside of PHP files, so while it technically is back-end from the client-server architecture, it can be front-end to the developer

hallow dust
#

thats what I mean with php on the front

frosty panther
#

its executing on the backend to provide content to the user's client after the script executes

hallow dust
#

ohh

#

but

#

if I want to make the system more dinamic

#

so you don't have to reload all the content of the page, every time the DBMS does php stuff and gives you the new html

#

can't you like

torn prism
#

You could use something like AJAX

frosty panther
#
<?php

function factorial(int $n): int {
    $result = 1;
    for ($i = 2; $i <= $n; $i++) {
        $result *= $i;
    }
    return $result;
}


?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h1>Factorial Numbers</h1>
    <div>
        <table>
            <thead>
                <tr>
                    <td>n</td>
                    <td>n!</td>
                </tr>
            </thead>
            <tbody>
            <?php
                for ($n = 0; $n < 15; $n++) {
                    $nFact = factorial($n);
                    echo "<tr><td>$n</td><td>$nFact</td></tr>";
                }
            ?>
            </tbody>
        </table>
    </div>
</body>
</html>
#

and yes you can use ajax via javascript

#

which is part of the jquery library

torn prism
#

This seems like a strange question to ask on a Java server

#

Keep in mind, Javascript is not Java. They are completely dfferent

frosty panther
#

#Other EZ

torn prism
#

Yeah, guess you can ask about any language. Just thought this would be something to ask in a server which revolves around JS more, or even web dev in general

frosty panther
hallow dust
# hallow dust can't you like

for example, let's say you want to change your profile picture,
you, the user, click on the button, upload the pic, send it to the php DBMS
it manipulates the SQL, you reload the page, and the DBMS returns you the changes,
now you can see your new profile pic.

It could take a while if the system is slow, so can't you like "fake the change" on the front, so it is faster
seemingly?, in that case, you wouldn't use php?, like
<? php
<img src = $file_new_pic>

hallow dust
frosty panther
hallow dust
frosty panther
#

you could instead do ```js

$("#update-profile-picture-button").on("click", function() {
$.post({
url: '/profile/update',
data: {
image: image
},
success: function(response) {
$("#profile-image").html(<img src=${response.source} class="profile-main-image">)
},
error: function(xhr, status, error {
console.log("oops!");
}
})

});```
on the frontend client code, then on /profile/update.php

// intercept POST and get name

// update in database

return json_encode(["source": $newImageSrc]);```
reef nacelleBOT
frosty panther
#

echo*

hallow dust
#

well, thank you