Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Code Reviews

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

66%
+2 −0
Code Reviews PHP for simple HTML-PHP-CSS contact form --- aimed solely for RTL websites

general improvements Move everything that is directly related for the submission inside the if ... 'POST' condition. if ($_SERVER['REQUEST_METHOD'] == 'POST') { $to = 'example@example.com'; ...

posted 3mo ago by GeraldS‭

Answer
#1: Initial revision by user avatar GeraldS‭ · 2024-08-04T07:43:53Z (3 months ago)
## general improvements

Move everything that is directly related for the submission inside the `if ... 'POST'` condition.

```php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $to = 'example@example.com';
    $subject = 'הודעה חדשה מאת: ' . $name;
    include 'message.php';
    $headers[] = 'MIME-Version: 1.0';
    $headers[] = 'Content-type: text/html; charset=utf-8';
    mail($to, $subject, $message, implode("\r\n", $headers));
    header('Location: $contact_form_success_webpage');
    exit;
}
```

there is no benefit of running all this code without submission of the form. Setting the other variables is useful if you use them in the form to keep the values on failed validation, otherwise you could move them also inside the condition.

## validation

You should make sure the required fields are actually enforced.

```php
$errors = [];
for ($required_field in ['topic', 'name', 'email']) {
    if (array_key_exists($required_field, $_POST)) {
        if (empty($_POST[$required_field])) {
            $errors[] = "$required_field cannot be empty";
        }
    }
}

if ($_SERVER['REQUEST_METHOD'] == 'POST' && count($errors) == 0) {
// ...
}
```

Then, beside your form you can output those error messages:

```php
if (count($errors) > 0) {
    echo "<p><b>Error validating your field:</b><br>";
    echo implode("<br>", $errors);
    echo "</p>";
}
```