2 assume like case with solutions based php of “Use Conditionals and Operators
Validate Form Data
Send Values to a Script Manually
Work with Forms and arrays of data
Use For and While Loops
Create a Simple Form using PHP
Receive Data from a Form in PHP”
Assistant
User
5 blank space question with solutions based php of “Use Conditionals and Operators
Validate Form Data
Send Values to a Script Manually
Work with Forms and arrays of data
Use For and While Loops
Create a Simple Form using PHP
Receive Data from a Form in PHP”
Assistant
Case 1: Use Conditionals and Operators
Problem: You have a form where users input their age, and you want to determine if they are eligible to vote (age 18 and above) or not.
Solution:
php
<?php
$age = $_POST['age'];
if ($age >= 18) {
echo "You are eligible to vote.";
} else {
echo "You are not eligible to vote.";
}
?>
Case 2: Validate Form Data
Problem: You have a registration form where users input their email address, and you want to validate if it is a valid email format.
Solution:
php
<?php
$email = $_POST['email'];
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Email is valid.";
} else {
echo "Email is not valid.";
}
?>
Case 3: Send Values to a Script Manually
Problem: You have a form with checkboxes where users can select multiple options, and you want to process the selected options in a PHP script.
Case 4: Work with Forms and arrays of data
Problem: You have a form where users can input multiple phone numbers, and you want to display all the entered phone numbers using PHP.
Solution:
html
<form action="process.php" method="post">
<input type="text" name="phone[]" placeholder="Phone number 1">
<input type="text" name="phone[]" placeholder="Phone number 2">
<input type="text" name="phone[]" placeholder="Phone number 3">
<input type="submit" value="Submit">
</form>
Case 7: Receive Data from a Form in PHP
Problem: You have a form where users can input their name, and you want to display a personalized message using PHP.
Here are some blank space questions along with their solutions based on the mentioned topics:
Use Conditionals and Operators:
Problem: You have a form where users input their age, and you want to determine if they are eligible to drive (age 16 and above) or not.
Solution:
php
<?php
$age = $_POST['age'];
if ($age >= 16) {
echo "You are eligible to drive.";
} else {
echo "You are not eligible to drive.";
}
?>
Validate Form Data:
Problem: You have a registration form where users input their password, and you want to validate if it meets certain criteria (e.g., at least 8 characters and contains at least one uppercase letter).
Solution:
php
<?php
$password = $_POST['password'];
if (strlen($password) >= 8 && preg_match("/[A-Z]/", $password)) {
echo "Password meets the criteria.";
} else {
echo "Password does not meet the criteria.";
}
?>
Send Values to a Script Manually:
Problem: You have a form where users select their favorite colors from a dropdown list, and you want to process the selected color in a PHP script.
<?php
$color = $_POST['color'];
echo "Your favorite color is $color.";
?>
Work with Forms and arrays of data:
Problem: You have a form where users can select multiple hobbies, and you want to display all the selected hobbies using PHP.
<?php
$rating = $_POST['rating'];
// Save the feedback and perform any necessary actions
?>
Receive Data from a Form in PHP:
Problem: You have a form where users can input their address, and you want to display the entered address in a formatted manner using PHP.