Note Edits and narrowing down of troubleshooting in Edit section below.
I'm trying to set up a form so that it will pull data from a database to keep the email address of the recipient private, but something isn't set up correctly.
My error messages for not filling in required fields are not popping up. The coach_id is being passed through a link that adds in the variables in the URL
email_form.php?f_name=Stephanie&l_name=Smith&coach_id=14001
I put "xxxxxxx" in for the login username and password because I know those are working correctly.
If I remove the // from in front of the code:
//echo "<p class=\"leftheading\">" . $row["email"] . "</p>";
I get the email address showing up on the screen, so I know the email address is being pulled correctly from the database. Is there an error elsewhere in my code that's preventing an email from being sent?
Edit: I turned on error reporting and figured out where the error is, but I don't know how to fix it because as I stated above, removing the // from in front of that code allows the correct email address to show up. I'm getting this error: "Notice: Undefined index: coach_id in /home/presbyteriancoa8/public_html/email_form.php on line 59", and this is the line causing the problem:
$strSQL = "SELECT * FROM coaches WHERE coach_id=" . $_GET["coach_id"];
which is causing this error: "Notice: Undefined variable: to in /home/presbyteriancoa8/public_html/email_form.php on line 122", and this is the line:
if (mail($to, $subject, $body, $from, $cc)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
Any ideas on how to correct the problem?
<?php
echo "<h1>Email " . $_GET["f_name"] . " " . $_GET["l_name"] . "</h1>";
?>
<section class="body">
<?php
// Connect to database server
mysql_connect("localhost", "xxxxxxx", "xxxxxxx") or die ('Could not connect: ' . mysql_error ());
// Select database
mysql_select_db("presbyte_c") or die('Could not connect: ' . mysql_error());
// SQL query
$strSQL = "SELECT * FROM coaches WHERE coach_id=" . $_GET["coach_id"];
// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);
// Loop the recordset $rs
// Each row will be made into an array ($row) using mysql_fetch_array
while($row = mysql_fetch_array($rs)) {
//$receiveemail = $row["email"];
//echo "<p class=\"leftheading\">" . $row["email"] . "</p>";
}
$name = $_POST['name'];
$email = $_POST['sendemail'];
$to = $row["email"];
$message = $_POST['message'];
$cc = $_POST['sendemail'];
$subject = 'Presbyterian Coach Network - Coaching Contact';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
// define variables and set to empty values
$name = $email = $message = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["sendemail"]);
$message = test_input($_POST["message"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// define variables and set to empty values
$nameErr = $emailErr = $messageErr = "";
$name = $email = $message = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["sendemail"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["sendemail"]);
}
if (empty($_POST["message"])) {
$messageErr = "Message is required";
} else {
$message = test_input($_POST["message"]);
}
}
if ($_POST['submit']) {
/* Anything that goes in here is only performed if the form is submitted */
if (mail ($to, $subject, $message, $name, $from, $email, $cc)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
// Close the database connection
mysql_close();
?>
<form method="post" action="email_form.php" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<p class="important">*required fields</p>
<label>Name</label><span class="important">*</span>
<input name="name" placeholder="Name">
<span class="error"><?php echo $nameErr;?></span>
<label>Your Email Address</label><span class="important">*</span>
<input name="sendemail" type="email" placeholder="email@domain.com">
<span class="error"><?php echo $emailErr;?></span>
<label>Message</label><span class="important">*</span>
<textarea name="message" placeholder="Type message here"></textarea>
<span class="error"><?php echo $messageErr;?></span>
<label></label>
<input id="submit" name="submit" type="submit" value="Submit">
</form>
</section>