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

Time:05-24

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 >
              <input type="text"  placeholder="Name" id="companyName" required>
              <label for="loginName">Company name </label>
            </div>

            <div >
              <input type="text"  placeholder="Name" id="address" required>
              <label for="loginName">Address In Full</label>
            </div>

            <div >
              <input type="text"  placeholder="Name" id="wayBillAddress" required>
              <label for="loginName">Address to be printed on way bill </label>
            </div>

            <div >
              <input type="text"  placeholder="Name" id="managerName" required>
              <label for="loginName">Name of the Business owner/ Manager</label>
            </div>

            <div >
              <input type="text"  placeholder="Name" id="nic" required>
              <label for="loginName">NIC Number</label>
            </div>

            <div >
              <input type="tel"  placeholder="Name" id="tel" required>
              <label for="loginName">Telephone No:       </label>
            </div>
            
            <div >
              <input type="email"  placeholder="Email" id="email" required>
              <label for="loginEmail">Email</label>
            </div>
            
            <div >
              <input type="textarea"  placeholder="Email" id="description" required>
              <label for="bankdetails">Product description</label>
            </div>


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

            <div >
              <input type="textarea"  placeholder="bankdetails" id="comments" required>
              <label for="bankdetails">Additional Requirements / Comments</label>
            </div>
           
            <input  type="submit" name="submit" value="Send"/>
          </form>

The PHP file:

       <?php

use PHPMailer\PHPMailer\PHPMailer;

$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 = '[email protected]';
        $mail->Password = '27f211b3fcad87';
        $mail->SMTPSecure = 'tls';
        $mail->Port = 2525;

        $mail->setFrom("[email protected]", 'Mailtrap Website');
        $mail->addAddress('[email protected]', '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

CodePudding user response:

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

<input type="text"  placeholder="Name" id="companyName" required>

So you need to add the name attribute

<input name="name" type="text"  placeholder="Name" id="companyName" required>
       ^^^^^^^^^^^

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

  • Related