Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Required Fields and Basic Validation | Working with Forms
Quizzes & Challenges
Quizzes
Challenges
/
PHP Core Concepts

bookRequired Fields and Basic Validation

When users submit data through a web form, you must ensure the data is complete and valid before processing it. Validation is crucial because it protects your application from missing, malformed, or malicious input. Without validation, users might submit incomplete forms, or attackers could inject harmful data.

index.php

index.php

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
<?php $name = $email = ""; $nameErr = $emailErr = ""; $success = false; if ($_SERVER["REQUEST_METHOD"] == "POST") { // Check if name is empty if (empty($_POST["name"])) { $nameErr = "Name is required"; } else { $name = trim($_POST["name"]); } // Check if email is empty if (empty($_POST["email"])) { $emailErr = "Email is required"; } else { $email = trim($_POST["email"]); } // If no errors, process the form (for demonstration, just show success) if (empty($nameErr) && empty($emailErr)) { $success = true; } } ?> <!DOCTYPE html> <html> <head> <title>Simple PHP Form Validation</title> </head> <body> <?php if ($success): ?> <h2>Form submitted successfully!</h2> <p>Name: <?php echo htmlspecialchars($name); ?></p> <p>Email: <?php echo htmlspecialchars($email); ?></p> <?php else: ?> <form method="post" action=""> <label>Name: <input type="text" name="name" value="<?php echo htmlspecialchars($name); ?>"></label> <span style="color:red;"><?php echo $nameErr; ?></span><br><br> <label>Email: <input type="text" name="email" value="<?php echo htmlspecialchars($email); ?>"></label> <span style="color:red;"><?php echo $emailErr; ?></span><br><br> <input type="submit" value="Submit"> </form> <?php endif; ?> </body> </html>

Required fields are form inputs that must be filled out; if any required field is empty, you should prevent further processing and prompt the user to provide the missing information. PHP allows you to check form data on the server side, ensuring that even if a user disables client-side validation or sends data directly to your server, your application remains protected.

question mark

Why is it important to validate form input on the server side in PHP?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 4

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

bookRequired Fields and Basic Validation

Deslize para mostrar o menu

When users submit data through a web form, you must ensure the data is complete and valid before processing it. Validation is crucial because it protects your application from missing, malformed, or malicious input. Without validation, users might submit incomplete forms, or attackers could inject harmful data.

index.php

index.php

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
<?php $name = $email = ""; $nameErr = $emailErr = ""; $success = false; if ($_SERVER["REQUEST_METHOD"] == "POST") { // Check if name is empty if (empty($_POST["name"])) { $nameErr = "Name is required"; } else { $name = trim($_POST["name"]); } // Check if email is empty if (empty($_POST["email"])) { $emailErr = "Email is required"; } else { $email = trim($_POST["email"]); } // If no errors, process the form (for demonstration, just show success) if (empty($nameErr) && empty($emailErr)) { $success = true; } } ?> <!DOCTYPE html> <html> <head> <title>Simple PHP Form Validation</title> </head> <body> <?php if ($success): ?> <h2>Form submitted successfully!</h2> <p>Name: <?php echo htmlspecialchars($name); ?></p> <p>Email: <?php echo htmlspecialchars($email); ?></p> <?php else: ?> <form method="post" action=""> <label>Name: <input type="text" name="name" value="<?php echo htmlspecialchars($name); ?>"></label> <span style="color:red;"><?php echo $nameErr; ?></span><br><br> <label>Email: <input type="text" name="email" value="<?php echo htmlspecialchars($email); ?>"></label> <span style="color:red;"><?php echo $emailErr; ?></span><br><br> <input type="submit" value="Submit"> </form> <?php endif; ?> </body> </html>

Required fields are form inputs that must be filled out; if any required field is empty, you should prevent further processing and prompt the user to provide the missing information. PHP allows you to check form data on the server side, ensuring that even if a user disables client-side validation or sends data directly to your server, your application remains protected.

question mark

Why is it important to validate form input on the server side in PHP?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 4
some-alt