Preparing the Interface (Frontend)

Preparing the Interface (The Counter)
Doña Pepa already understands that she needs a "Counter" so she never has to type SQL commands again. This counter is the User Interface (UI) or Frontend. Its main goal is to make registering a new product as easy as sending a text message.
To build it, we will use HTML (for the structure) and CSS (to make it look spectacular).
1. Best Practices: Semantic HTML
The most common mistake beginners make is using <div> tags for everything. To create a good counter, we must use Semantic HTML, meaning tags that describe exactly what they contain. This helps accessibility (people using screen readers) and the order of our code.
For Doña Pepa to enter a product, we use the <form> tag:
html<!-- Doña Pepa's Counter --> <form id="product-form" class="pepa-form"> <label for="name">Product Name:</label> <input type="text" id="name" name="name" required> <label for="price">Sale Price ($):</label> <input type="number" id="price" name="price" min="0" step="0.01" required> <button type="submit" id="btn-save">Save Product</button> </form>
Notice the for attribute in the <label> tags. By connecting them with the <input>'s id, if Doña Pepa touches the words "Sale Price", the cursor will automatically jump to the text box. That is good usability!
2. Security: The First Line of Defense
Did you notice the required and min="0" attributes in our HTML code? That is Frontend Validation.
required: Prevents Pepa from submitting the form if the field is empty.min="0": Prevents Pepa from typing a negative price (we can't pay customers to take milk!).
Golden Rule of Cybersecurity! 🛡️ Validation on the Frontend is NOT for real security; it's pure User Experience (UX). It serves to quickly warn Pepa that she made a mistake. A clever hacker can bypass the HTML and send a negative number anyway. Real security will be put in the Backend (in the next lesson).
3. UX/UI: CSS and User Experience
Doña Pepa doesn't have time to guess how the system works. We must apply UI (User Interface) and UX (User Experience) principles:
Styling with CSS
Based on Fitts's Law (important buttons should be large and easy to reach), we are going to use CSS so the form looks professional and the button calls to action:
css/* Counter Styles */ .pepa-form { background-color: #1a1a2e; padding: 2rem; border-radius: 12px; display: flex; flex-direction: column; gap: 15px; /* Space between elements */ color: #fff; font-family: sans-serif; max-width: 400px; } .pepa-form input { padding: 10px; border-radius: 6px; border: 1px solid #4a4e69; background-color: #22223b; color: white; } /* The Magic Button */ #btn-save { background-color: #00ff88; /* Neon green to stand out */ color: #000; padding: 12px; border: none; border-radius: 6px; font-weight: bold; cursor: pointer; transition: all 0.3s ease; } #btn-save:hover { background-color: #00cc6a; transform: translateY(-2px); /* Small animation on hover */ } /* When the button is disabled */ #btn-save:disabled { background-color: #555; cursor: not-allowed; transform: none; }
Visual Feedback with JavaScript
When Pepa clicks "Save", the button should change to the text "Saving..." and become disabled. If we don't do this, Pepa might click 5 times in a row because she thinks "it's slow", and she would register 5 identical milks in the database!
javascript// UX Feedback Example with JavaScript const form = document.getElementById('product-form'); const button = document.getElementById('btn-save'); form.addEventListener('submit', function(event) { event.preventDefault(); // Prevents the page from reloading immediately // UX: Disable the button to prevent double-click button.disabled = true; button.innerText = "Saving..."; // This is where we will send the data to the Waiter (Backend)... });
Summary: Our Counter is ready, pretty, and prevents silly mistakes! But currently, if Pepa hits "Save", the data doesn't go anywhere. In the next lesson (Building the Bridge), we will create the "Waiter" (Backend) to take this information and save it securely in the SQL database.
