HTML ↔ JS ↔ PHP — communication possibilities

The demo at languages/forms-languages.php illustrates the classic channel: a full page reload via a GET form, with PHP re-rendering the result server-side (plus a cookie for persistence). This page lists the other ways HTML, JavaScript and PHP can talk to each other.

1 · HTTP method & how the data travels

POST vs GET

Same forms, but method="post" puts the value in the request body (read via $_POST), not in the URL. Use it for anything that changes state.

<form method="post" action="handle.php">
    <input type="text" name="name">
    <button>Send</button>
</form>
<?php
$name = $_POST["name"];   // from the body, not the URL

enctype & file uploads

multipart/form-data is required for <input type="file">; PHP exposes it via $_FILES.

<form method="post" enctype="multipart/form-data">
    <input type="file" name="upload">
</form>

Hidden inputs

Carry state the user doesn't see.

<input type="hidden" name="lang" value="en">

Post / Redirect / Get (PRG)

After handling a POST, redirect so a refresh doesn't re-submit.

<?php
// handle POST...
header("Location: thanks.php");
exit;

2 · Full reload vs no reload (the JavaScript axis)

fetch() / XHR (AJAX)

Send a request and update the page without reloading.

const res = await fetch("api.php", {
    method: "POST",
    body: new FormData(form)
});
const data = await res.json();
document.querySelector("#result").textContent = data.msg;

onsubmit + preventDefault()

Intercept the form, validate in JS, then decide whether to submit.

form.addEventListener("submit", (e) => {
    e.preventDefault();               // stop the normal reload
    // ...validate or send via fetch()...
});

localStorage / sessionStorage

Remember a choice entirely client-side — no server round-trip (a cookie alternative).

localStorage.setItem("lang", "es");
const lang = localStorage.getItem("lang");

3 · Persistent / push channels (beyond request→response)

Server-Sent Events (SSE)

PHP streams events; JS listens with EventSource.

// PHP — events.php
header("Content-Type: text/event-stream");
echo "data: language changed\n\n";
// JS
const es = new EventSource("events.php");
es.onmessage = (e) => console.log(e.data);

WebSockets

Full-duplex, persistent connection (PHP via Ratchet/AMP).

Long polling

setInterval + fetch — a poor-man's push.

Note: sibling projects already demonstrate SSE (php--server-sent-events--sse/) and WebSockets (web-rt-cards-game/web-sockets/php-amp/).

4 · Server-side state (PHP's memory)

$_SESSION

Server-side state tied to a session cookie; survives requests without exposing data in the URL.

<?php
session_start();
$_SESSION["lang"] = "pt";

$_COOKIE

Already used by the demo for persistence.

<?php
setcookie("lang", "es", time() + 60 * 60 * 24 * 30, "/");
$lang = $_COOKIE["lang"] ?? "unspecified";

Redirects

<?php
header("Location: /languages/");
exit;

5 · HTML5 form features

formaction / formmethod / formtarget

One form where each button goes to a different endpoint or method.

<form>
    <button formaction="save.php"  formmethod="post">Save</button>
    <button formaction="search.php" formmethod="get">Search</button>
</form>

New input types & validation

<input type="range" name="level" min="1" max="5">
<input type="date" name="when" required>
<input type="email" name="mail" pattern=".+@.+">
<datalist id="langs">
    <option value="en"><option value="es">
</datalist>

6 · Data formats

JSON (the de-facto AJAX format)

<?php
header("Content-Type: application/json");
echo json_encode(["lang" => "es", "ok" => true]);
// JS
const data = JSON.parse(await res.text());

XML, plain text, HTML fragments

All valid PHP responses; HTML fragments let PHP return a ready-made snippet for JS to inject.

Which fit the current demo (not yet implemented)

← back to the demo