Article - 03
"Contact me - PHP contact form"
Hello, everybody!
How about to create a contact form, using just PHP?
I created one for my old website,
Before I don't have used or learned php.
And this contact form is my first experience with php.
PHP (Personal Home Page tools) is a general-purpose programming language,
originally designed for web development.
Давайте начнем!
Before we start coding you need to create and run our own Apache server.
I have used XAMPP Control Panel
After downloading Xampp just start an Apache Server.
With Xampp downloading would be automatically created xampp folder on the disk C.
There you need folder htdocs:
And it's your main folder where you should work.
Now let's create here index.php file.
Echo: "hello world!"
<html>
<head>
<title>Test PHP - Hello world!</title>
</head>
<body>
<?php echo '<p>Hello world!</p>'; ?>
</body>
</html>
or just so:
<html>
<head>
<title>Test PHP - Hello world!</title>
</head>
<body>
<?php echo 'Hello world!'; ?> //echo is the same as just print.
</body>
</html>
Compared with html you can't open .php file just in the browser,
you need first to connect to the server
and for this, you need xampp.
If you are already connected
go to the browser on http://localhost/.
You should see something like this:
How we can see with php you need to use html to
and for php commands <php "command;" ?>.
Now to contact form.
It's my version and I'm not 100% sure that it's completely right construction.
But it works.
In index.php we need to create a contact form which sends data to the server
and we need to collect them.
Contact form (index.php):
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
$_POST
?>
<form action="welcome.php" method="post">
<input type="text" name="name" placeholder="Name" maxlength="25"><br> <!--<br> used to wrap text/object-->
<input type="email" name="email" placeholder="E-mail" maxlength="25"><br>
<input type="text" name="subject" placeholder="Subject" maxlength="25"><br>
<br>
<textarea type="text" name="text" placeholder="Message" maxlength="300" rows="10px" cols="35px"></textarea><br>
<br>
<input type="submit">
</form>
</body>
</html>
We have created a form with <form>)
With action to file welcome.php with method get or post.
It means when you click on the submit button (<input type="submit">)
result of this form would be saved/showed on this (welcome.php) file.
It's like we create a bridge between these two files with a server on the middle/end.
In this form, we have created an input window for name/email/subject, text window
and submit the bottom.
For each input we prescribe data type (type="type"),
like text for text and email for emails, genial.
In the same time, we can prescribe the maximum length of input with maxlength="25"
and you can write something in the input window with attribute placeholder="write something",
a user would see this before he started something to write.
We have contact form information which would be sent.
But we need to collect them.
And this we do in the second file (welcome.php).
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$text = $_POST["text"];
$date = date("h:i:sa [d.m.Y]");
$br = "\n";
$line = "-------------------------";
echo $date;
echo "<br>";
echo "name is: " . $name;
echo "<br>";
echo "email is: " . $email;
echo "<br>";
echo "subject is: " . $subject;
echo "<br>";
echo "message: " . $text;
echo "<br>";
echo $line;
echo "<br>";
?>
</body>
</html>
You see we create variables with symbol $
and move with method $_POST or $_GETfrom a contact form to these variables.
In the square brackets, we need to write wich input name from index.php would be saved in a variable.
<input type="text" name="name">
---------------------------------------------------
$name = $_POST["name"];
or
<input type="email" name="email">
---------------------------------------------------
$email = $_POST["email"];
It should look like this:
Now we can write -> send -> read inputs
But we need to save it too.
I have made so - If you clicked on submit you will be sent to welcome.php
and your inputs would be saved in log.txt
welcome.php:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$text = $_POST["text"];
$date = date("h:i:sa [d.m.Y]");
$br = "\n";
$line = "-------------------------";
$log = file_put_contents('logs.txt', $line.PHP_EOL , FILE_APPEND | LOCK_EX);
$log = file_put_contents('logs.txt', "date: " . $date.PHP_EOL , FILE_APPEND | LOCK_EX);
$log = file_put_contents('logs.txt', "name: " . $name.PHP_EOL , FILE_APPEND | LOCK_EX);
$log = file_put_contents('logs.txt', "email: " . $email.PHP_EOL , FILE_APPEND | LOCK_EX);
$log = file_put_contents('logs.txt', "subject: " . $subject.PHP_EOL , FILE_APPEND | LOCK_EX);
$log = file_put_contents('logs.txt', "message: " . $text.PHP_EOL , FILE_APPEND | LOCK_EX);
$log = file_put_contents('logs.txt', $line.PHP_EOL , FILE_APPEND | LOCK_EX);
$log = file_put_contents('logs.txt', $br.PHP_EOL , FILE_APPEND | LOCK_EX);
?>
</body>
</html>
See I don't use echo because the user doesn't need to see his letter.
Instead of this, we create logs.txt
(you don't need to create a file by yourself, php would automatically create a file if it doesn't yet)
and write their variables content.
$log = file_put_contents('logs.txt', $line.PHP_EOL , FILE_APPEND | LOCK_EX);
Sure it would be much more convenient if you will know the date when a letter was sent.
$date = date("h:i:sa [d.m.Y]");
The server can self check the time without scripts.
Just write how you want to see time (which format).
I have a little bit played with logs design too and the result must be something like this:
-------------------------
date: 02:01:13pm [25.03.2019]
name: Max
email: email@email.email
subject: test 1
message: Hi
-------------------------
-------------------------
date: 02:08:35pm [25.04.2019]
name: Max
email: email@email.email
subject: test 2
message: Hi again
-------------------------
Probably it's all,
we have created a contact form on php!