Skip to content

Instantly share code, notes, and snippets.

@Maikuolan
Forked from rushdimohamed09/contact.php
Last active March 19, 2016 21:43
Show Gist options
  • Save Maikuolan/62edcd757569675df143 to your computer and use it in GitHub Desktop.
Save Maikuolan/62edcd757569675df143 to your computer and use it in GitHub Desktop.
Refer to the included comments.
<?php
/**
* "Undefined index" means that you're trying to fetch data from a variable
* that doesn't exist (or isn't "defined"). Looking at your code here,
* personally, what I would do here, firstly, is check whether your $_REQUEST
* variables are empty, and if they're not, *then* set your $action variable,
* rather than before.
*/
if (!empty($_REQUEST['action'])) {
$action = $_REQUEST['action'];
?>
<form action="contact.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30"/><br>
Your email:<br>
<input name="email" type="text" value="" size="30"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" name="submit" value="Send email"/>
</form>
<?php
} else {
/** Same principle as with the "action" variable, we'll do the same here. */
if (empty($_REQUEST['name']) || empty($_REQUEST['email']) || empty($_REQUEST['message'])) {
echo 'All fields are required, please fill <a href=\'\'>the form</a> again.';
} else {
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
$from = 'From:' . $name . $email . "\r\nReturn-path: " . $email;
$subject = 'Message sent using your contact form';
mail('youremail@yoursite.com', $subject, $message, $from);
echo 'Email sent!';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment