-2

I'm trying to send a form's data to someone's email via PHP.

However, I get a undefined index error on every variable, on every line.

PHP Notice: Undefined index: name in /path/to/file.php on line 2

Form: https://jsfiddle.net/dg1ur9fc/

PHP:

<?php
/* Error Reporting */
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

if(isset($_POST)){
    $name   = $_POST['fullname'];
    $number = $_POST['number'];
    $email  = $_POST['email'];
    $brand  = $_POST['brand'];
    $model  = $_POST['model'];
    $variant = $_POST['variant'];
    $screen = $_POST['screen'];
    $body = $_POST['body'];
    $power = $_POST['power'];
    $battery = $_POST['battery'];
    $charge = $_POST['charge'];
    $calls = $_POST['calls'];
    $fcamera = $_POST['fcamera'];
    $rcamera = $_POST['rcamera'];
    $water = $_POST['water'];
    $wifi = $_POST['wifi'];
    $formcontent = "From: $name \n Contact: $number \n Device Brand: $brand \n Device Model: $model \n Device Variant: $variant \n Screen: $screen \n Housing: $body \n Is your device able to power on? $power \n Does your device have a fault battery? $battery \n Is your device able to charge? $charge \n Does your front camera work? $fcamera \n Does your rear camera work? $rcamera \n Is your device water damaged? $water \n Is your device able to connect using WiFi? $wifi";
    $recipient = "test@example.com";
    $subject = "Buyback Request";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader);
    /* Redirect browser */
    header("Location: https://example.com");
    /* Make sure that code below does not get executed when we redirect. */
    exit;
}
?>

May I know what the issue is, and how to fix it? Thank you.

narypigeon
  • 103
  • 1
  • 10

1 Answers1

0

they are undefined on first load as there is no post yet. So you should always check for the existence of any post using

if(isset($_POST['name']) && isset($_POST['number']) && isset($_POST['email']) && pass all parameter here ){
Rahul Kr Daman
  • 387
  • 3
  • 15