Skip to content
Advertisement

Notice: Undefined index: name in /home/kivat619/siyum.me/sup/ez/assets/php/message.php on line 2

I am trying to create a contact form with PHP.

I am getting these errors.

error

This happens when only I add this to one of the my websites It works fine on a static webpage with only the contact form.

The HTML form:

            <div class="form-floating mb-4">
              <input type="text" class="form-control" placeholder="Name" id="companyName" required>
              <label for="loginName">Company name </label>
            </div>

            <div class="form-floating mb-4">
              <input type="text" class="form-control" placeholder="Name" id="address" required>
              <label for="loginName">Address In Full</label>
            </div>

            <div class="form-floating mb-4">
              <input type="text" class="form-control" placeholder="Name" id="wayBillAddress" required>
              <label for="loginName">Address to be printed on way bill </label>
            </div>

            <div class="form-floating mb-4">
              <input type="text" class="form-control" placeholder="Name" id="managerName" required>
              <label for="loginName">Name of the Business owner/ Manager</label>
            </div>

            <div class="form-floating mb-4">
              <input type="text" class="form-control" placeholder="Name" id="nic" required>
              <label for="loginName">NIC Number</label>
            </div>

            <div class="form-floating mb-4">
              <input type="tel" class="form-control" placeholder="Name" id="tel" required>
              <label for="loginName">Telephone No:       </label>
            </div>
            
            <div class="form-floating mb-4">
              <input type="email" class="form-control" placeholder="Email" id="email" required>
              <label for="loginEmail">Email</label>
            </div>
            
            <div class="form-floating mb-4">
              <input type="textarea" class="form-control" placeholder="Email" id="description" required>
              <label for="bankdetails">Product description</label>
            </div>


            <div class="form-floating mb-4">
              <input type="textarea" class="form-control" placeholder="bankdetails" id="bankDetails" required>
              <label for="bankdetails">Banking Details (Account Name, Account Number and Branch)</label>
            </div>
            
            <div class="form-floating mb-4">
              <input type="textarea" class="form-control" placeholder="bankdetails" id="rates" required>
              <label for="bankdetails">Expected courier rates (courier rates for Colombo 1-15, suburbs, outstation, regional)</label>
            </div>

            <div class="form-floating mb-4">
              <input type="textarea" class="form-control" placeholder="bankdetails" id="comments" required>
              <label for="bankdetails">Additional Requirements / Comments</label>
            </div>
           
            <input class="btn btn-primary rounded-pill btn-login w-100 mb-2" type="submit" name="submit" value="Send"/>
          </form>

The PHP file:

       <?php

use PHPMailerPHPMailerPHPMailer;

$errors = [];
$errorMessage = '';

if (!empty($_POST)) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    if (empty($name)) {
        $errors[] = 'Name is empty';
    }

    if (empty($email)) {
        $errors[] = 'Email is empty';
    } else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors[] = 'Email is invalid';
    }

    if (empty($message)) {
        $errors[] = 'Message is empty';
    }


    if (!empty($errors)) {
        $allErrors = join('<br/>', $errors);
        $errorMessage = "<p style='color: red;'>{$allErrors}</p>";
    } else {
        $mail = new PHPMailer();

        // specify SMTP credentials
        $mail->isSMTP();
        $mail->Host = 'cpl16.main-hosting.eu';
        $mail->SMTPAuth = true;
        $mail->Username = 'info@siyum.me';
        $mail->Password = '27f211b3fcad87';
        $mail->SMTPSecure = 'tls';
        $mail->Port = 2525;

        $mail->setFrom("me@siyum.me", 'Mailtrap Website');
        $mail->addAddress('nethsarasiyum2@gmail.com', 'Me');
        $mail->Subject = 'New message from your website';

        // Enable HTML if needed
        $mail->isHTML(true);

        $bodyParagraphs = ["Name: {$name}", "Email: {$email}", "Message:", nl2br($message)];
        $body = join('<br />', $bodyParagraphs);
        $mail->Body = $body;

        echo $body;
        if($mail->send()){

            header('Location: thank-you.html'); // redirect to 'thank you' page
        } else {
            $errorMessage = 'Oops, something went wrong. Mailer Error: ' . $mail->ErrorInfo;
        }
    }
}

?>

I am new to PHP, I tried to find some solutions online, but failed.

I appreciate your help. Thanks

Advertisement

Answer

The browser uses the name attribute of an <input> element to pass data back to the script on the server.

<input type="text" class="form-control" placeholder="Name" id="companyName" required>

So you need to add the name attribute

<input name="name" type="text" class="form-control" placeholder="Name" id="companyName" required>
       ^^^^^^^^^^^

This will cause the browser to pass the relevent data in the $_POST['name'] variable

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement