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.



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


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

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

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

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

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

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

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



<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
        <input type="checkbox" name="color" value="silver" style="margin-left:15%"> Silver
        <input type="checkbox" name="color" value="gold" style="margin-left:15%"> Gold
    <div class="column" style="flex:50%">
        <input type="checkbox" name="repair" value="screen"> Charge Port Replacement
        <input type="checkbox" name="repair" value="battery"> Battery Replacement
        <input type="checkbox" name="repair" value="homeButton"> Home Button Replacement
        <input type="checkbox" name="repair" value="screen"> Screen Replacement
    <br><hr style="margin-top:5%;border-color:black;margin-bottom:5%">
    <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>
    <label for="lastname">Last Name</label>
        <input type="text" id="lastname" name="lastname" placeholder="Your last name.." required>
    <label for="phone">Phone Number</label>
        <input type="text" id="phone" name="phone" placeholder="Your phone number.." required>
    <label for="email">Email Address</label>
        <input type="text" id="email" name="email" placeholder="Your email address.." required>
    <div style="text-align:center">
        <input type="submit" value="Submit" style="background-color:#f3892b;font-family:roboto">


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

  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.