POST Method

The code below is to be used to receive information from an html form, format it and send it via email. It then redirects to a page where a confirmation is displayed. But, it doesn’t seem to be functioning properly so I’m hoping I could get some pointers as to why that is.

PHP:

<?php

    $errorMsg = "";
    $firstName = "";
    $lastName = "";
    $phone = "";
    $email = "";
    $repair = "";
    $color = "";

    if($_SERVER["REQUEST_METHOD"] == "POST"){

        if (empty($_POST['firstname'])){
            $errorMsg .= "Please enter your first name.";
        }
        else{
            $firstName = $_POST['firstname'];
        }

        if (empty($_POST['lastname'])){
            $errorMsg .= "Please enter your last name.";
        }
        else{
            $lastName = $_POST['lastname'];
        }

        if (empty($_POST['phone'])){
            $errorMsg .= "Please enter your phone number.";
        }
        else{
            $phone = $_POST['phone'];
        }

        if (empty($_POST['email'])){
            $errorMsg .= "Please enter your email address.";
        }
        else{
            $email = $_POST['email'];
        }

        $repair = $_POST['repairs'];
        $color = $_POST['color'];

        $fullName = $firstName . ' ' . $lastName;
        $subject = $lastName . ', ' . $firstName . ' ' . $repair;
        $sendTo = "email@email.com";

        $body = "";
        $body .= "Name: ";
        $body .= $fullName;
        $body .= "\n";
        $body .= "Email: ";
        $body .= $email;
        $body .= "\n";
        $body .= "Phone: ";
        $body .= $phone;
        $body .= "\n";
        $body .= "Phone color: ";
        $body .= $color;
        $body .= "\n";
        $body .= "Repairs: ";
        $body .= $repairs;

        mail($sendTo, $subject, $body);
        header('Location: website');
        
    }

?>

HTML:

<form action="form_handling.php" method="POST">
<div class="row" style="display:flex">
    <div class="column" style="flex:70%">
        <input type="checkbox" name="color" value="spaceGray" style="margin-left:15%"> Space Gray
        <br>
        <input type="checkbox" name="color" value="silver" style="margin-left:15%"> Silver
        <br>
        <input type="checkbox" name="color" value="gold" style="margin-left:15%"> Gold
    </div>
    <div class="column" style="flex:50%">
        <input type="checkbox" name="repair" value="screen"> Charge Port Replacement
        <br>
        <input type="checkbox" name="repair" value="battery"> Battery Replacement
        <br>
        <input type="checkbox" name="repair" value="homeButton"> Home Button Replacement
        <br>
        <input type="checkbox" name="repair" value="screen"> Screen Replacement
    </div>
</div>
    <br><hr style="margin-top:5%;border-color:black;margin-bottom:5%">
    <br>
    <h2 style="font-family:roboto;color:#f3892b;text-align:center;font-weight:300">CONTACT INFO</h2>
    <label for="firstname">First Name</label>
        <input type="text" id="firstname" name="firstname" placeholder="Your name.." required>
        <br><br>
    <label for="lastname">Last Name</label>
        <input type="text" id="lastname" name="lastname" placeholder="Your last name.." required>
        <br><br>
    <label for="phone">Phone Number</label>
        <input type="text" id="phone" name="phone" placeholder="Your phone number.." required>
        <br><br>
    <label for="email">Email Address</label>
        <input type="text" id="email" name="email" placeholder="Your email address.." required>
        <br><br>
    <div style="text-align:center">
        <input type="submit" value="Submit" style="background-color:#f3892b;font-family:roboto">
    </div>
</form>

Try adding these lines at the beginning of the file and it should show an error:

Notice : Undefined variable: $XXXX in test-002.php on line XXX

<?php
  declare(strict_types=1);
  error_reporting(-1);
  ini_set('display_errors', '1');

 // DEBUG tests to be moved further down file until errors encountered:
echo '<br>' .__LINE__;
die; // halts execution 

// existing script goes here

Could you expand on that please? In exactly what way is it not doing what you expect it to? Does it send the mail but miss out some information, or not send the mail? Why do you check all form input fields except the two check-boxes, before using their values?

I can’t see anywhere you define a “from” address, which may well prevent your SMTP server from sending the message.

Lots of people suggest not using the in-built PHP mail() function as it’s unreliable, instead choosing to use something like PHPMailer.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.