I am a designer trying to get a PHP to email script working. The HTML form is the standard Skeleton CSS one from their website at http://getskeleton.com/#forms:
<form>
<div class="row">
<div class="six columns">
<label for="exampleEmailInput">Your email</label>
<input class="u-full-width" placeholder="test@mailbox.com" id="exampleEmailInput" type="email">
</div>
<div class="six columns">
<label for="exampleRecipientInput">Reason for contacting</label>
<select class="u-full-width" id="exampleRecipientInput">
<option value="Option 1">Questions</option>
<option value="Option 2">Admiration</option>
<option value="Option 3">Can I get your number?</option>
</select>
</div>
</div>
<label for="exampleMessage">Message</label>
<textarea class="u-full-width" placeholder="Hi Dave …" id="exampleMessage"></textarea>
<label class="example-send-yourself-copy">
<input type="checkbox">
<span class="label-body">Send a copy to yourself</span>
</label>
<input class="button-primary" value="Submit" type="submit">
</form>
I am trying to amend the existing PHP script to match this and having an awful lot of trouble. This is my base plate for the PHP:
<?php
if($_REQUEST['name'] == '' || $_REQUEST['email'] == '' || $_REQUEST['message'] == ''):
return "error";
endif;
if (filter_var($_REQUEST['email'], FILTER_VALIDATE_EMAIL)):
$to = 'email@myemail.com';
$header = 'From: '. $_REQUEST['name'] . ' <'. $_REQUEST['email'] .'>'. "
";
$header .= 'Reply-To: '. $_REQUEST['name'] . ' <'. $_REQUEST['email'] .'>'. "
";
$header .= 'X-Mailer: PHP/' . phpversion();
$subject = "Hello";
$message .= 'Name: ' . $_REQUEST['name'] . "
";
$message .= 'Email: ' . $_REQUEST['email'] . "
";
$message .= 'Message: '. $_REQUEST['message'];
$event = $_GET['exampleRecipientInput']
$mail = mail( $to, $url , $message, $header );
return $mail ? "success" : "error";
else:
return "error";
endif;
?>
I cannot understand how to adapt the PHP which is probably easy for experienced devs but fairly impenetrable for designers. Can anyone help?
As ceejayoz said, you're going to need to add name attributes to the input fields, then you'll be able to access them via $_POST['whatever_name']
Your HTML code could look like the following (added name attributes):
<form>
<div class="row">
<div class="six columns">
<label for="exampleEmailInput">Your email</label>
<input class="u-full-width" placeholder="test@mailbox.com" id="exampleEmailInput" type="email" name="email">
</div>
<div class="six columns">
<label for="exampleRecipientInput">Reason for contacting</label>
<select class="u-full-width" id="exampleRecipientInput" name="reason">
<option value="Option 1">Questions</option>
<option value="Option 2">Admiration</option>
<option value="Option 3">Can I get your number?</option>
</select>
</div>
</div>
<label for="exampleMessage">Message</label>
<textarea class="u-full-width" placeholder="Hi Dave …" id="exampleMessage" name="message"></textarea>
<label class="example-send-yourself-copy">
<input type="checkbox">
<span class="label-body">Send a copy to yourself</span>
</label>
<input class="button-primary" value="Submit" type="submit">
</form>
You need to ensure you're checking if the submit button has been, in fact, submitted:
<?php
if (isset($_POST['submit']) {
if($_POST['name'] == '' || $_POST['email'] == '' || $_POST['message'] == ''):
return "error";
endif;
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)):
$to = 'email@myemail.com';
$header = 'From: '. $_POST['name'] . ' <'. $_POST['email'] .'>'. "
";
$header .= 'Reply-To: '. $_POST['name'] . ' <'. $_POST['email'] .'>'. "
";
$header .= 'X-Mailer: PHP/' . phpversion();
$subject = "Hello";
$message .= 'Name: ' . $_POST['name'] . "
";
$message .= 'Email: ' . $_POST['email'] . "
";
$message .= 'Message: '. $_POST['message'];
$event = $_POST['reason'];
$mail = mail( $to, $url , $message, $header );
return $mail ? "success" : "error";
else:
return "error";
endif;
}
?>
The if (isset($_POST['submit']))
portion will let the script know that the form has been submitted, and will run the code inside of the brackets.
Note: I changed the $_REQUEST['']
to $_POST['name']
. These will be associated to the field names I've provided at the top of the HTML edit.