0

I am trying to put a basic email form on a website and have been having trouble with "unidentified index". I read around and found that "isset()" had solved this issue. I found that

$... = isset($_POST['...']);

does get rid of the error message, but my code does nothing. The page doesn't even refresh or throw another error. Here is my html:

    <form method="post" action="index.php">
        <h1>Send Us an Email</h1>
        <header class="emailbody">
           <label>Name</label>
           <input name="name" placeholder="Type Here">
        </header>
        <section class="emailbody">
            <label>Email</label>
            <input name="email" type="email" placeholder="Type Here">
        </section>
        <footer class="emailbody">
            <label>Message</label>
            <textarea name="message" placeholder="Type Here"></textarea><br>
        </footer>
        <div class="submitbutton">
            <input id="submit" type="submit" value="Send">
        </div>
    </form>

And here is my php:

<?php

    $name = isset($_POST['name']);
    $email = isset($_POST['email']);
    $message = isset($_POST['message']);
    $from = 'From: SiteDemo';
    $to = 'exemail@gmail.com';
    $subject = 'Hello';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

?>

I have tried it with and without the "isset()" around the different indexes. Without, it throws an error and with, the code doesn't do anything.

cc77
  • 115
  • 7

2 Answers2

1

Two things need to be corrected:

1) You have not written mail() function statement.

2) isset() returns only TRUE or FALSE depending upon whether variable is set or not. It does not return variable if variable is set.

Correct it to:

$name = isset($_POST['name']) ? $_POST['name'] : '';

Same for other variables.

Pupil
  • 23,834
  • 6
  • 44
  • 66
0

Yes, you can use isset() here for checking either value set or not like that:

if(isset($_POST['submit']))
{   
   // if you dont want to send empty email.
   if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message'])) 
   {
      $name = trim($_POST['name']);
      $email = trim($_POST['email']);
      $message = trim($_POST['message']);
      $header = "From: SiteDemo"; 
      $to = 'exemail@gmail.com';
      $subject = 'Hello';

      $body = "From: $name\n E-Mail: $email\n Message:\n $message";
      mail($to,$subject,$body,$header);
   }
}

And add name attribute in submit button as:

<input id="submit" type="submit" value="Send" name="submit">
devpro
  • 16,184
  • 3
  • 27
  • 38