Home PHP Undefined variable for HTML Form
Reply: 1

PHP Undefined variable for HTML Form

Evan Rixom
1#
Evan Rixom Published in 2018-01-13 01:34:41Z

This question already has an answer here:

  • PHP: “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” 23 answers

I am trying to run a contact form PHP script and HTML form and when I run it I get the following errors.

Notice: Undefined variable: name in /storage/ssd4/333/4197333/public_html/heros51/confirm.php on line 17

Notice: Undefined variable: p in /storage/ssd4/333/4197333/public_html/heros51/confirm.php on line 18

Notice: Undefined variable: message in /storage/ssd4/333/4197333/public_html/heros51/confirm.php on line 18

Notice: Undefined variable: visitor_email in /storage/ssd4/333/4197333/public_html/heros51/confirm.php on line 21

<form action="confirm.php" method="POST" >
        <label>Name >></label><input id="name" type="text" /><br />
        <label>Email >></label><input id="email" type="text" /><br />
        <label>PRI >></label><input id="p" type="text" /><br />
        <label>Message >></label><textarea id="message" rows="1"></textarea><br /><br /><br />
        <input type="submit" value="submit" />
        <a class="button" alt="" href="index.html">Cancel</a>
    </form>

The PHP which is linked looks like this:

if( isset($_POST['name']) && isset($_POST['email']) && isset($_POST['p']) && isset($_POST['message']) ){
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$p = $_POST['p'];
$email_from = 'default03@securemail.com';
$email_subject = "New Form submission $name ";
$email_body = "You have received a new message from the user $p.\n".  "Here is the message:\n $message".
$to = "erixom@gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
}
else
echo " ISSET FAIL";
$email_from = 'default03@securemail.com';
$email_subject = "New Form submission $name ";
$email_body = "You have received a new message from the user $p.\n".  "Here is the message:\n $message".
$to = "test@gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
?>
hexYeah
2#
hexYeah Reply to 2018-01-13 01:44:52Z

instead of id="name" you must use name="name". The id attribute is used for JavaScript manipulation while the name attribute is used to name your POST variables

I would like to add that you should sanitize user submitted inputs. Even tho this might not be a internet facing project, it's never too early too adopt this as a habbit :)

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306434 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO