Blog.jirivanek.eu      Digitální zápisník

Simple contact form in PHP

This is a PHP code for a simple contact form in PHP that sends an email. Here’s a breakdown of the code and its functions:

Note: this simple script is used, for example, to test on or off mail() functions on the webserver. It contains no protection against to send out spam!

Simple contact form in PHP

Simple contact form in PHP

				
					<?php
// Kontrola, zda byly data odeslána formulářem
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Získání dat z formuláře
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    // Validace dat
    if (!empty($name) && !empty($email) && !empty($message)) {
        // Odeslání e-mailu
        mail("your_email@example.com", "Zpráva od " . $name, $message, "From: " . $email);

        // Potvrzení odeslání
        $status = "Děkujeme, Vaše zpráva byla úspěšně odeslána.";
    } else {
        // Chyba při odesílání
        $status = "Všechna pole musí být vyplněna.";
    }
}
?>

<!-- Kontaktní formulář -->
<form action="" method="post">
    <label for="name">Jméno:</label>
    <input type="text" id="name" name="name">
    <br>
    <label for="email">E-mail:</label>
    <input type="email" id="email" name="email">
    <br>
    <label for="message">Zpráva:</label>
    <textarea id="message" name="message"></textarea>
    <br>
    <input type="submit" value="Odeslat">
</form>

<!-- Status odeslání -->
<?php if (isset($status)): ?>
    <p><?php echo $status; ?></p>
<?php endif; ?>

				
			
  1. if ($_SERVER['REQUEST_METHOD'] === 'POST') { ... }: This checks if the HTTP request method is POST, which means the form has been submitted. If it is not POST, the code below the if statement will not be executed.

  2. $name = $_POST['name'];, $email = $_POST['email'];, and $message = $_POST['message'];: These lines retrieve the values entered by the user in the input fields of the form and assign them to variables.

  3. if (!empty($name) && !empty($email) && !empty($message)) { ... }: This checks if all the input fields have been filled out. If they have, the code below the if statement will be executed.

  4. mail("[email protected]", "Zpráva od " . $name, $message, "From: " . $email);: This sends an email with the values entered in the form to the email address specified (in this case, “[email protected]“).

  5. $status = "Děkujeme, Vaše zpráva byla úspěšně odeslána."; and $status = "Všechna pole musí být vyplněna.";: These lines set the value of the $status variable, which is used to display a message to the user indicating the status of the form submission.

  6. <form action="" method="post"> ... </form>: This is the HTML code for the contact form. It has three input fields for name, email, and message, and a submit button.

  7. <?php if (isset($status)): ?> ... <?php endif; ?>: This checks if the $status variable has been set, which indicates that the form has been submitted. If it has, the message specified in $status will be displayed to the user.

Byl pro Vás tento článek užitečný?

Klikni na počet hvězd pro hlasování.

Průměrné hodnocení. 0 / 5. Počet hlasování: 0

Zatím nehodnoceno! Buďte první

Jak užitečný vidíte tento článek.

Sledujte mě na sociálních médiích.

Je mi líto, že pro Vás nebyl článek užitečný.

Jak mohu vylepšit článek?

Řekněte mi, jak jej mohu zlepšit.

Odebírat
Upozornit na
guest
0 Komentáře/ů
Vložené zpětné vazby.
Zobrazit všechny komentáře.

Pokud mi chcete napsat rychlou zprávu, využije, prosím, níže uvedený
kontaktní formulář. Děkuji.

Další Kontaktní údaje