Question
The given code is in php. Please edit in php: Executing: Validating data submitted to the server
The given code is in php. Please edit in php:
// For security, required PHP files should "die" if SAFE_TO_RUN is not defined if (!defined('SAFE_TO_RUN')) { // Prevent direct execution - show a warning instead die(basename(__FILE__) . ' cannot be executed directly!'); } ?>
// TODO: Change these checks according to the columns/formats you expect
// Reference for preg_match: https://www.w3schools.com/php/func_regex_preg_match.asp // Reference for filter_var: https://www.w3schools.com/php/func_filter_var.asp // Note that preg_match and filter_var take different parameters // Try out regular expressions at e.g. https://regex101.com/
// If you see a "Notice: Undefined index" message, check that each name you validate // in $data has an input with that name (not id) in the HTML data form
$value = $data['firstname']; // ^$ = anchors, [a-zA-Z ] = letters/spaces, {1,30} = 1-30 characters $format = "/^[a-zA-Z ]{1,30}$/"; // If value does NOT match the format then it is invalid if (!preg_match($format, $value)) { $feedback['firstname'] = 'Server feedback: Only 1-30 letters/spaces are permitted'; $valid = false; }
$value = $data['lastname']; // ^$ = anchors, [a-zA-Z ] = letters/spaces, {1,30} = 1-30 characters $format = "/^[a-zA-Z ]{1,30}$/"; // If value does NOT match the format then it is invalid if (!preg_match($format, $value)) { $feedback['lastname'] = 'Server feedback: Only 1-30 letters/spaces are permitted'; $valid = false; }
$value = $data['email']; // If value does NOT match the filter then it is invalid if (!filter_var($value, FILTER_VALIDATE_EMAIL)) { $feedback['email'] = 'Server feedback: Only valid email addresses are permitted'; $valid = false; } // Also check the maximum length for this field as filter_var doesn't do this if (strlen($value) > 50) { $feedback['email'] = 'Server feedback: Email must be 50 characters or less'; $valid = false; }
if (!$valid) { echo '
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started