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.
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
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>
Carry state the user doesn't see.
<input type="hidden" name="lang" value="en">
After handling a POST, redirect so a refresh doesn't re-submit.
<?php
// handle POST...
header("Location: thanks.php");
exit;
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;
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()...
});
Remember a choice entirely client-side — no server round-trip (a cookie alternative).
localStorage.setItem("lang", "es");
const lang = localStorage.getItem("lang");
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);
Full-duplex, persistent connection (PHP via Ratchet/AMP).
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/).
Server-side state tied to a session cookie; survives requests without exposing data in the URL.
<?php
session_start();
$_SESSION["lang"] = "pt";
Already used by the demo for persistence.
<?php
setcookie("lang", "es", time() + 60 * 60 * 24 * 30, "/");
$lang = $_COOKIE["lang"] ?? "unspecified";
<?php
header("Location: /languages/");
exit;
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>
<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>
<?php
header("Content-Type: application/json");
echo json_encode(["lang" => "es", "ok" => true]);
// JS
const data = JSON.parse(await res.text());
All valid PHP responses; HTML fragments let PHP return a ready-made snippet for JS to inject.