0

I have created a WP template and added to it a form. Below the form I have added the wp_mail() function to send the data the user has input to their email.

Now, I'm getting notices that:

Notice: Undefined index: name in mypath.

Any help how to resolve this would be most welcome.

Edit 1: to get the notice I had to remove the if statement.

Edit 2: this is literally all the code I have concerning the form. Am I missing something? Do I need to add add_action() somewhere or something like that?

The code:


<form id="form" method="post" action="">
    <label for="name">Name:</label>
    <input type="text" id="name" name="name">
    </br>
    <label for="surname">Surname:</label>
    <input type="text" id="surname" name="surname">
    </br>
    <label for="email">Email:</label>
    <input type="email" id="email" name="email">
    </br>
    <label for="tel">Phone number:</label> 
    <input type="tel" id="tel" name="tel">
    </br>
    <input type="submit" value="Submit" name="submit">
</form>

<?php
if(isset($_POST['submit'])) {
    $name = $_POST['name'];
    $surname = $_POST['surname'];
    $email = $_POST['email'];
    $tel = $_POST['tel'];
                    
    $to = $_POST[$email]; //sendto@example.com
    $subject = 'Reservation';
    $body = 'Name: ' . $name .  '\r\n' .
            'Surname: ' . $surname .  '\r\n' .
            'Email ' . $email .  '\r\n' .
            'Phone number: ' . $tel .  '\r\n';
                            
    wp_mail( $to, $subject, $body );
    echo "Sent!";
}
?>


The anwser

Edit 3: just to make it clear the solution was $to = $_POST['email']; //sendto@example.com so ['email'] not [$email].

WeAreDoomed
  • 248
  • 1
  • 14

1 Answers1

0

The if statement is a sensible check - you don't want that code running until the $_POST variable has some values in it.

But, none of your form elements have names! eg this <input type="tel" id="tel"> should be <input type="tel" id="tel" name="tel">

Without that, you're not going to get very much.

PS Checkout PHP heredoc. That code at the end could be much more neatly written as:

    $body = <<< EOT
      Name: $name
      Surname: $surname
      Email: $email
      Phone number: $tel
    EOT;
John C
  • 96
  • 1
  • 5
  • Thank you for the reply. Adding the name "components" did not solve the undefined index issue though =) any idea how I could remedy this? – WeAreDoomed Feb 15 '21 at 19:05
  • is still missing a name. That's why you've had to uncomment the IF. Fix that, and the IF can stay (which it should) and you won't get the warning, because the indexes will exist. You can use isset to check others, although newer versions of PHP allow it to be written like this: `$value = $_POST['my-field'] ?? '';` – John C Feb 15 '21 at 22:39
  • Hey, thanks. I actually did update it, just forgot to edit it here on SO. The thing is, if I remove the if statement I get the notices (but I guess that's all right since the fields are empty on refresh). If I add the if statement the notices are gone after I reload BUT... once I click submit, I still get one notice (which is still way better) and it sayes: *Notice: undefined index of *input email address. It's pointing at this `$to = $_POST[$email]; //sendto@example.com` Any ideas? – WeAreDoomed Feb 15 '21 at 23:23
  • `$email = $_POST['email']` There is no `$email = $_POST[$email];` – John C Feb 15 '21 at 23:28
  • To be clear `$_POST['email']` exists, `$_POST[$email]` never will because $email at that point is equal to the email address they typed in. – John C Feb 15 '21 at 23:29
  • So your saying change that line to `$to = $_POST[email];`? No.. that throws: Warning: Use of undefined constant email - assumed 'email'. Trying `$to = $_POST['email'];` Yea that did it. Thank you! – WeAreDoomed Feb 15 '21 at 23:34
  • The $_POST indexes as driven by the names of your input fields. So `` becomes `$_POST['WeAreDoomed']`. Usefully, in PHP you can do this: `` becomes `$_POST['WeAreDoomed']['NotReally']` – John C Feb 16 '21 at 13:07
  • Hey, thanks. Its been a while since I had any interacton with PHP an I'm quite rusty. I also searched for "this" code, so I don't understand why someone would declare a variable inside the `= $_POST[$varaible']` – WeAreDoomed Feb 16 '21 at 13:17