Reputation: 33
I am trying to submit an email to subscribe to a newsletter using php and bootstrap. The var_dump($_POST) outputs this: array(1) { 'subscribe_submit' => string(0) "" }
This is my html:
<form method="post" action="subscribe.php" class="form-inline" role="form">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" placeholder="Enter `enter code here`email" required>
</div>
<button type="submit" name="subscribe_submit" class="btn btn-default">Submit</button>
</form>
subscribe.php
<?php
session_start();
include("connect.php");
if(isset($_POST['subscribe_submit']))
{
// Define $email
$email=$_POST['email'];
// Protect from injection
$email = stripslashes($email);
$email = mysqli_real_escape_string($db, $email);
// Check database to see if email is already subscribed
$sql="SELECT id FROM minutes WHERE email='$email'";
$result=$db->query($sql);
$row=$result->fetch_assoc();
if($result->num_rows == 1)
{
echo "You are already subscribed to our minutes.";
}else
{
$sql = "INSERT INTO minutes (email)
VALUES ($email)";
if ($db->query($sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($db);
}
$db->close();
}
}
?>
I have been racking my brain all day trying to figure out why it is not working. Thanks in advance.
Upvotes: 2
Views: 2061
Reputation: 927
you forgot the most important attribute name="email"
<input type="email" name="email" class="form-control" id="email"
placeholder="Enter `enter code here`email" required>
Upvotes: 7