Forum Moderators: coopster

Message Too Old, No Replies

PHP Issues with an email form

         

gjcluttrell

7:17 pm on Mar 17, 2011 (gmt 0)

10+ Year Member



The attached code pulls data from a mysql database & displays it in a form so I can edit it. I need to be able, when a user clicks the submit button in this code, the data is updated in the mysql database, and sends that data to an email address based off of the location. At this time I just have first name, lastname, and location in there. So if a user chose office1 the data would go to office1@mail.com. EVERYTHING works fine except the mail part.




<?php # edit_dqa.php

$page_title = 'Edit a Record';

$con = mysql_connect("localhost","uname","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("psrflow", $con);

if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { // Accessed through view_users.php
$id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { // Form has been submitted.
$id = $_POST['id'];
} else { // No valid ID, kill the script.
echo '<h1 id="mainhead">Page Error</h1>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
include ('./includes/footer.html');
exit();
}

$query = "UPDATE psrinfo SET fname='$fn', lname='$ln', location='$loc' WHERE fid=$id";
$result = @mysql_query ($query); // Run the query.

// Send mail according to location
switch($_REQUEST['location']) {
case "office1":
$email = "myemail@yahoo.com";
break;
default:
echo "Error, no location selected!";
break;
}
$Message = "<p>An employee has edited the information below.</p><br> Location : {$_REQUEST['location']}<br>First Name : {$_REQUEST['fname']}<br>Last Name : {$_REQUEST['lname']}";
$Headers = "MIME-Version: 1.0\n";
$Headers .= "Content-type: text/html; charset=iso-8859-1\n";
$Subject = "Please Review";
$Headers .= "To: DQA <$EmailAddress>\n";
$Headers .= "From: The Database <sender email>\n";
$SendMail = mail($EmailAddress, $Subject, $Message, $Headers);

if (mail($EmailAddress, $Subject, $Message, $Headers)) {

echo "<center>An email has been sent for editing";
} else {

echo "This system is not working properly. Please contact a tech.";

}

$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') OR (substr($url, -1) == '//')) {
$url = substr ($url, 0, -1);
}
$url ='/flow/index.html';

header("Location: $url");

exit();

} else { // Report the errors.

echo '<h1 id="mainhead">Error!</h1>
<p class="error">The following error(s) occurred:<br />';
foreach ($errors as $msg) { // Print each error.
echo " - $msg<br />\n";
}
echo '</p><p>Please try again.</p><p><br /></p>';

} // End of if (empty($errors)) IF.

} // End of submit conditional.

// Retrieve the user's information.
$query = "SELECT pacts, fname, lname, location WHERE fid = " . $_REQUEST['id'];
$result = @mysql_query ($query); // Run the query.

if (mysql_num_rows($result) == 1) { // Valid user ID, show the form.

$row = mysql_fetch_array ($result, MYSQL_NUM);

?>

<?php


echo '<form action="edit_dqa.php" method="post">
<fieldset><legend><h1> You are editing a record!</h1></legend>

<b>First Name:</b> <br><input type="text" name="fname" size="15" maxlength="30" value="'.$row[1].'" /><br />
<b>Last Name:</b> <br><input type="text" name="lname" size="15" maxlength="30" value="'.$row[2].'" /><br />
<b>Location: </b><br><input type="text" name="location" size="15" maxlength="30" value="'.$row[5].'" /><br>
br>
<br>
</fieldset>

<div align="left"><input type="submit" name="submit" value="Submit" /></div>

<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="id" value="' . $id . '" />
</form>';

} else { // Not a valid user ID.
echo '<h1 id="mainhead">Page Error</h1>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
}

mysql_close(); // Close the database connection.

?>




coopster

8:27 pm on Mar 17, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, gjcluttrell.

What part of the mail is not working? Is it returning false? What error messages are you getting?

LSD4me

9:40 pm on Mar 17, 2011 (gmt 0)

10+ Year Member



Just an observation but $EmailAddress is not defined...$email is however defined (depends on switch)...

coopster

1:26 pm on Mar 19, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Nice catch, and welcome to WebmasterWorld.