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.

PHP for simple HTML-PHP-CSS contact form --- aimed solely for RTL websites

+1
−0

As a non PHP programmer I have tried to program some basic PHP mail() function code to send Right To Left (RTL) contact form messages to my local email client (i.e. an email client which is affiliated with the same web domain of my website hosting machine).

The code does its task; contact form RTL messages indeed reach my local email box and the output is RTLized as expected but I guess I lack some backend validation there and perhaps some other things --- please opine about my PHP code.


contact_form.php

<?php
	$topic = $_POST['topic']; # Required;
	$name = $_POST['name'];   # Required;
	$email = $_POST['email']; # Required;
	$url = $_POST['url']; # Name attribute is "url" but input type is "text" because "url" obligates http:// or https:// which users normally don't input

	$phone = $_POST['phone'];
	$date = $_POST['date'];
	$time = $_POST['time'];
	$notes = $_POST['notes'];

	$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));

	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
 		header('Location: $contact_form_success_webpage');
		exit;
	}
?>

message.php

<?php
	$message = <<<LABEL
		<html>
			<head>
				<style>
				* {
					font-size: 20px;
				}
				a {
					unicode-bidi: plaintext !important;
				}
				.form_output_col_1 {
					font-weight: bold;
				}
				</style>
			</head>
			<body dir="rtl" style="text-align: right;">
				<div><span class="form_output_col_1">נושא: </span><span class="form_output_col_2">$topic</span></div>
				<div><span class="form_output_col_1">שם: </span><span class="form_output_col_2">$name</span></div>
				
				<div><span class="form_output_col_1">אימייל: </span><a dir="ltr" href="mailto:$email" class="form_output_col_2">$email</a></div>
				<div><span class="form_output_col_1">אתר: </span><a dir="ltr" href="$url" target="_blank" class="form_output_col_2">$url</a></div>
				<div><span class="form_output_col_1">טלפון: </span><a dir="ltr" href="tel:$phone" class="form_output_col_2">$phone</a></div>
				
				<div><span class="form_output_col_1">תאריך: </span><span class="form_output_col_2">$date</span></div>
				<div><span class="form_output_col_1">שעה: </span><span class="form_output_col_2">$time</span></div>
				<div><span class="form_output_col_1">הערות: </span><span class="form_output_col_2">$notes</span></div>
			</body>
		</html>
	LABEL;
?>
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (1 comment)

1 answer

+2
−0

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';
    $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.

$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:

if (count($errors) > 0) {
    echo "<p><b>Error validating your field:</b><br>";
    echo implode("<br>", $errors);
    echo "</p>";
}
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »