Forum Moderators: coopster
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>SymphoniRose.com</title>
</head>
<body>
<?php
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$email = $_POST['email'];
$hostname='host';
$username='user';
$password='pass';
$dbname='dbname';
$dbc = mysqli_connect($hostname, $username, $password, $dbname) OR DIE ('Unable to connect to database! Please try again later.');
$query = "INSERT INTO email_list (first_name, last_name, email) " .
"VALUES ('$first_name', '$last_name', '$email')";
mysqli_query($dbc, $query) OR DIE ('Error querying database.');
mysqli_close($dbc);
echo 'Thank you for joining our Mailing List.<br />';
header( 'Location: http://www.symphonirose.com/index.html');
?>
</body>
</html>
get rid of all the html tags on the page, you don't need them when you are just redirectign back to the main page - that is also causing your error
<?php
if ($_POST['email-address']) {
// Process your form here
echo 'Thank you for your interest.';
}
else { ?>
<form method="post" action="main.php">
<p><label for="email">Email:</label>
<input type="text" name="email-address" id="email" value=""></p>
<p><input type="submit" value="subscribe"></p>
</form>
<?php }?>
Never begin your PHP script with a DOCTYPE.
Put all the PHP "logic" at the beginning of the file, and then the HTML page will begin half way down the script (and the HTML page part will begin with a DOCTYPE).
This allows the PHP "logic" part of the PHP script to add redirect and other HTTP headers before the HTML page is begun to be sent.
I think its saying, "IF there is an email address, then say something like, 'email already subscribed?' else add it?"
<?php
if ($_POST['email-address']) {
// "Check input" would be some function that
// you write to check the email address,
// see if it already exists in the database, and
// if there's an error, store it in $err. The function
// would return a string containing the error, such as
// "invalid email address" or "email already exists"
$err = check_input($_POST['email-address']);
if ($err) { echo display_form($err); }
else {
// Some logic or function to add to DB, then
echo 'Thank you for your interest.';
}
}
else { echo display_form(); }
?>
<?php
function display_form($error=null) {
$form=null;
if ($error) { $form .= "<p>$error</p>"; }
$form .= '
<form method="post" action="main.php">
<p><label for="email">Email:</label>
<input type="text" name="email-address" id="email" value=""></p>
<p><input type="submit" value="subscribe"></p>
</form>
';
return $form;
}
?>