Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help sending form data to email. 2

Status
Not open for further replies.

invalid4564

Programmer
Jul 13, 2010
16
US
I have created a form and when the submit button is pressed i would like the data from the form to be sent to my email but i keep getting the error that 'no data has been submitted'.

here is the form:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
<head>

	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<link rel="stylesheet" type="text/css" href="default.css" />
	<title>test</title>
</head>
<body>
	<div id="form">
		<form action="emailform.php" method="post" enctype="text/plain">
			<p><label>Title:</label>
			<select name="title">
				<option value="Mr.">Mr.</option>
				<option value="Mrs.">Mrs.</option>
				<option value="Ms.">Ms.</option>
				<option value="Miss">Miss</option>
				<option value="Dr.">Dr.</option>
			</select> &nbsp;
			<label>Name:</label> <input type="text" name="name" size="20" />
			<br /><br />
			<label>Phone:</label> <input type="text" name="name" size="20" />
			<br /><br />
			<label>Reason:</label>
			<select name="reason">
				<option value="option1">Option1</option>
				<option value="option2">Option2</option>
				<option value="option3">Option3</option>
				<option value="option4">Option4</option>
			</select>
			<br /><br />
			<label>Best Time:</label>
			<select name="time">
				<option value="Morning">Morning</option>
				<option value="Afternoon">Afternoon</option>
			</select>
			<br /><br />
			<label>Email:</label> <input type="text" name="email" size="30" />
			<br /><br />
			<label>Comment:</label><br />
			<textarea rows="3" cols="30"></textarea>
			<br /><br />
			<input type="submit" value="Send" />
			<input type="reset" value="Reset" />
			</p>
		</form>
	</div>

</body>
</html>


</div>
</body>
</html>

I found the following php code to send data to email address:

PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
	<title>Emailing Form Data</title>
<style type="text/css">
code {color:#F00C4D;font-weight:bold;font-size:1.2em}
i {color: #6D0CF0}
th, td {padding:.1em;border:1px solid blue;text-align:left}
</style>
</head>
<body>


<?php
//This is a very simple PHP script that outputs the name of each bit of information (that corresponds to the <code>name</code> attribute for that field) along with the value that was sent with it right in the browser window, and then sends it all to an email address (once you've added it to the script).

if (empty($_POST)) {
	print "<p>No data was submitted.</p>";
	print "</body></html>";
	exit();
}

//Creates function that removes magic escaping, if it's been applied, from values and then removes extra newlines and returns to foil spammers. Thanks Larry Ullman!
function clear_user_input($value) {
	if (get_magic_quotes_gpc()) $value=stripslashes($value);
	$value= str_replace( "\n", '', trim($value));
	$value= str_replace( "\r", '', $value);
	return $value;
	}


if ($_POST['comments'] == 'Please share any comments you have here') $_POST['comments'] = '';	

//Create body of message by cleaning each field and then appending each name and value to it

$body ="Here is the data that was submitted:\n";

foreach ($_POST as $key => $value) {
	$key = clear_user_input($key);
	$value = clear_user_input($value);
	if ($key=='extras') {
		
	if (is_array($_POST['extras']) ){
		$body .= "$key: ";
		$counter =1;
		foreach ($_POST['extras'] as $value) {
				//Add comma and space until last element
				if (sizeof($_POST['extras']) == $counter) {
					$body .= "$value\n";
					break;}
				else {
					$body .= "$value, ";
					$counter += 1;
					}
				}
		} else {
		$body .= "$key: $value\n";
		}
	} else {

	$body .= "$key: $value\n";
	}
}

extract($_POST);
//removes newlines and returns from $email and $name so they can't smuggle extra email addresses for spammers
$email = clear_user_input($email);
$name = clear_user_input($name);

//Create header that puts email in From box along with name in parentheses and sends bcc to alternate address
$from='From: '. $email . "(" . $name . ")" . "\r\n" . 'Bcc: yourmail@yourdomain.com' . "\r\n";


//Creates intelligible subject line that also shows me where it came from
$subject = 'Bed Order from Web Site';

//Sends mail to me, with elements created above
mail ('yourmail@yourdomain.com', $subject, $body, $from);
?>

<p>Thank you. We will contact you soon.</p>


</body>
</html>

How can i correct this error so data is sent to an email address?

Thank you for your help.
 
Remove the enctype="..." portion of the form, it prevents the form from being correctly submitted. So its in fact basically empty when it gets to the PHP part.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Ok made the change and it no longer says that 'no data has been submitted'.

Now my question is how can i correctly test the user input form and php code to see if it sends the data to my email?

I downloaded xampp and used it to run the form but i get the following error when i submit the data:

Code:
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\dentist3\emailform.php on line 79
 
you need to set up an smtp server on your local computer and ensure that your client has relay rights. doing this is outside the scope of this forum.

alternatively you could use phpmailer() which comes with its own smtp class.
 
Yup. The actual mailing is the smtp server territory.

Judging by the error, you don't have an smtp server installed or properly configured.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
After doing some research on how to configure php.ini and sendmail.ini in Mercury Mail for xammp, I no longer get the error message that I posted in my previous message. Instead i get the confirmation message that the data has been sent (Yay!).

But i have another question: how can i get the confirmation message to display on the same page where the form is located and not on a different blank page with only the confirmation message?

Thank you for your help.
 
Preferably move your PHP code to the same file as the form.

Alternatively display a copy of the form in your PHP file.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top