Hey so I currently have a form working pretty much how I want except one thing I am trying to make sure the emails that are being entered are valid email addresses I have been told I can use FILTER_VALID_EMAIL but if I do I'm going to get the same error message saying you are already subscribed.
here is where i'm up to at the minute could anybody shed some light on my issue? would be much apreciated! :)
<?php
// $_POST might be empty even if the request is a POST
if ('POST' === $_SERVER['REQUEST_METHOD']) {
try {
$db = new PDO('mysql:host=localhost;dbname=test', $subsc_username, $subsc_password);
// default error mode is to return FALSE
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die($e->getMessage());
}
$subsc_email = $_POST['sub_email'];
// Insert form valudes in to query and process sql query
$sql = 'INSERT INTO subscribers (subsc_email) VALUES (:subsc_email)';
try {
if ( ! filter_var( $subsc_email , FILTER_VALID_EMAIL )) throw new Exception('Invalid Email');
$query = $db->prepare($sql);
$query->execute(array(
':subsc_email' => $subsc_email,
));
echo '<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Success!</strong> You have been added to our mailing list.</div>'; // Success Message
} catch( PDOException $e ) {
echo '<div class="alert alert-danger alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Error!</strong> You are already subsribed to our mailing list...</div>'; // Error Message
}
}
?>
<form method="post" action="">
<div class="form-group">
<label>Email:</label>
<input class="form-control" type="text" name="sub_email" />
<br />
<div class="pull-right">
<input class="btn btn-success" type="submit" value="Subscribe" />
</div>
</div>
</form>