Why type="button" Never Sends Data in a POST Form
You are trying to build a calculator, which is an excellent first project for getting comfortable with the request/response cycle. The core issue preventing your code from displaying the value lies in how HTML forms and input types interact with the HTTP protocol. Specifically, using <input type="button"> inside a form that submits via method="POST" will never populate $_POST.
When you define an input as type="button", it is designed strictly for client-side interaction within the browser's document object model. Clicking such a button triggers the onclick event, which typically executes JavaScript code attached to that element. Crucially, a standard <input type="button"> has no default action to submit form data to the server. It does not change the page state or generate an HTTP request on its own.
To get a value into $_POST, you must use an input type that is designed to submit content when activated. The correct element for your scenario is <input type="submit"> or <button type="submit">. These elements are semantically bound to the form's submission mechanism. When clicked, they trigger the default form action, which involves constructing a new HTTP request containing the name-value pairs of all successful controls within that form.
Here is how the corrected HTML structure should look:
<form action="" method="POST">
<!-- This submits the data to the server -->
<input type="submit" value="0" name="zero">
</form>
With this change, your PHP logic remains largely the same. The isset($_POST["zero"]) check will now successfully detect the submission because the key exists in the superglobal array after the request is processed.
However, there is a subtle but important distinction between <input type="submit"> and <button type="submit"> that you should consider as your project grows. Both elements send data to $_POST, but they behave differently regarding custom attributes like onmouseover or complex JavaScript logic attached directly in the HTML attribute.
Using <button> is generally preferred in modern development because it acts as a container element, allowing you to place text, icons, and other content inside it without affecting form submission behavior. For example:
<button type="submit" name="zero" value="0">
Zero
</button>
While <input> is limited to simple text values or specific media types like file uploads, the <button> tag can hold HTML elements and still function correctly as a submit control. If you were to use type="button" on a <button> tag without changing the type, it would indeed behave exactly like your current code: it would allow JavaScript interaction but never send data to PHP unless you explicitly prevent default behavior and handle the form submission via AJAX or a separate script.
Another potential pitfall in beginner PHP projects involves how values are handled if they contain special characters. If you decide to change the button value later to something like "50%", ensure that your input is treated as safe data when displayed back on the page. Always escape output using htmlspecialchars() when echoing user input or form values to prevent cross-site scripting vulnerabilities, even for simple calculator buttons.
if (isset($_POST["zero"])) {
$value = htmlspecialchars($_POST["zero"], ENT_QUOTES, 'UTF-8');
echo "You pressed: {$value}";
}
If you are looking to scale this application into a full-stack web framework later, Laravel provides a robust way to handle form submissions and validation. You can generate these submit buttons automatically using Blade templates and route their data to specific controller methods. For instance, the Form::open helper in Laravel sets up the POST method and CSRF protection automatically, ensuring security out of the box while you focus on the calculator logic.
For more advanced interactions where you want to keep the user on the same page without a full reload, you might explore AJAX or Livewire. These tools allow you to capture the button click event via JavaScript, send the data asynchronously, and render the response in the view. This approach keeps the UI responsive but requires handling the asynchronous nature of the request differently than standard form submissions.