Jump to content

Adding content to a database from a form


unplugged_web

Recommended Posts

The action of the form needs to be the PHP page that contains the email code. Form data does not automatically get sent in an email, you need to say everything that goes into it. The code up there has a to address and a subject, but no text in the email body. You need to add whatever information you want from the form into a message and then set the message as the body of the email. There are several examples on the php.net page of how to do that.

Link to comment
Share on other sites

Sorry to be so stupid, but if I add:
<?php$to	  = 'someone@wherever.com';$subject = 'website form';mail($to, $subject);?>

to the html of the page that has the form this will e-mail all of the information? And what do I need to put for the action of the form?

I found some code in this forum that said you could use it to submit the information to an e-mail address, but it didn't work. Am I doing something seriously wrong here?The code was:
<?php$data = $_POST;$message = '';$errors = "";foreach($data as $key=>$value){if(!empty($data) OR preg_match("/[a-z0-9]{3,}\s?/i",$value)){	  $message.= "$key : $value\n";  }else{	  $errors.= "You left $key blank. Please, go back and fill it in.<br />\n";   }}$to = "your@email.address";$subject = "Form Mailed from website";if(strlen($errors) > 0 ){	 echo $errors;}else{	 mail($to,$subject,$message);}?>

the post I found is here

Link to comment
Share on other sites

What didn't work? Did you see an error? That regular expression check will exclude everything that is not in the format it is looking for, which looks like an alpha-numeric string of at least 3 characters.
I just got a blank page and no e-mail arrived. Sorry to keep posting here, but I don't really understand php that well and really want this to work. I keep trying different things and none of them seem to work. I do appreciate your help
Link to comment
Share on other sites

Change the foreach loop to just this:

foreach($data as $key=>$value){  $message.= "$key : $value\n";}

And make sure to check the spam or trash folders for the email. Also, add these lines to the top of the code:ini_set("display_errors", 1);error_reporting(E_ALL);

Link to comment
Share on other sites

Change the foreach loop to just this:
foreach($data as $key=>$value){  $message.= "$key : $value\n";}

And make sure to check the spam or trash folders for the email. Also, add these lines to the top of the code:ini_set("display_errors", 1);error_reporting(E_ALL);

It still isn't coming through. I've tried different e-mail addresses and checked the spam and trash folders. I'm sure it's something I'm doing, but don't know what it is.The php file is now:
<?phpini_set("display_errors", 1);error_reporting(E_ALL);$data = $_POST;$message = '';$errors = "";foreach($data as $key=>$value){  $message.= "$key : $value\n";}$to = "someone@somewhere.com";$subject = "Form Mailed from website";if(strlen($errors) > 0 ){	 echo $errors;}else{	 mail($to,$subject,$message);}?>

and the html file is:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>melovesushi</title><meta name="description" content=""><meta name="keywords"  content=""><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link href="css/style.css" rel="stylesheet" type="text/css"><style type="text/css"><!--.style1 {color: #FF0000}--></style></head><body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" bgcolor="#000000"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">		<form name="stanmore" method="post" action="mail.php">	<tr>	  <td width="35%" valign="top" class="text">Title:</td>	  <td width="65%" valign="top" class="text"><select name="Title" id="Title">		<option value="mr" selected>Mr</option>		<option value="ms">Ms</option>		<option value="miss">Miss</option>		<option value="mrs">Mrs</option>		<option value="dr">Dr</option>	  </select></td>	</tr>	<tr>	  <td valign="top" class="text"><label> First name: </label></td>	  <td valign="top" class="text"><input name="firstname" type="text" size="37"></td>	  </tr>	<tr>	  <td valign="top" class="text">Last name: </td>	  <td valign="top" class="text"><input name="lastname" type="text" size="37"></td>	  </tr>	<tr>	  <td valign="top" class="text">		<label> Address: </label></td>	  <td valign="top" class="text"><input name="address" type="text" id="address" size="37"></td>	  </tr>	<tr>	  <td valign="top" class="text"><label>Town:</label></td>	  <td valign="top" class="text"><input name="town" type="text" size="37"></td>	  </tr>	<tr>	  <td valign="top" class="text"><label>City:</label></td>	  <td valign="top" class="text"><input name="city" type="text" size="37"></td>	  </tr>	<tr>	  <td valign="top" class="text"><label>Postcode:</label></td>	  <td valign="top" class="text"><input name="postcode" type="text" size="37"></td>	  </tr>	<tr>	  <td valign="top" class="text"><label>Telephone no:</label></td>	  <td valign="top" class="text"><input name="phone" type="text" size="37"></td>	  </tr>	<tr>	  <td valign="top" class="text"><label>Mobile no: </label></td>	  <td valign="top" class="text"><input name="mobile" type="text" size="37"></td>	  </tr>	<tr>	  <td valign="top" class="text"><label>Email address:</label></td>	  <td valign="top" class="text"><input name="email" type="text" size="37"></td>	  </tr>	<tr>	  <td valign="top" class="text"><label>Date of birth:</label></td>	  <td valign="top" class="text"><input name="day" type="text" value="dd" size="2">	  <input name="month" type="text" value="mm" size="2">	  <input name="year" type="text" value="yyyy" size="4"><br><img src="images/spacer.jpg" alt="spacer" name="spacer" id="spacer"></td>	  </tr>	<tr>	  <td colspan="2" valign="top" class="text">How did you here about usi:</td>	  </tr>	<tr>	  <td colspan="2" valign="top" class="text"><input name="heard" type="text" size="51"><br><img src="images/spacer.jpg" alt="spacer" name="spacer" id="spacer"></td>	  </tr>	<tr>	  <td colspan="2" valign="top" class="text">Please feel free to pass on any other comments you may have: </td>	  </tr>	<tr>	  <td colspan="2" valign="top" class="text">				  <textarea name="comments" cols="48" rows="5"></textarea>		  <br>		  <img src="images/spacer.jpg" alt="spacer" name="spacer" id="spacer"></td>	  </tr>	<tr>	  <td colspan="2" valign="top" class="text_form">From time to time we would like to send you other promotions,please check the boxes to indicate how you would like to receive theseoffers.</td>	  </tr>	<tr>	  <td colspan="2" valign="top" class="text_form"><input name="sms" type="checkbox" value="sms">I would like to receive Free SMS promotions to my mobile phone.		</td>	  </tr>	<tr>	  <td colspan="2" valign="top" class="text_form"><input name="post" type="checkbox" value="post">I would like to receive Free offers and promotions via email or post.		</td>	  </tr>	<tr>	  <td colspan="2" valign="top" class="text_form">We guarantee not to pass your details onto any third party, unless youauthorise us to do so by checking the box below. </td>	  </tr>	<tr>	  <td colspan="2" valign="top" class="text_form"><input name="thirdparty" type="checkbox" value="thirdparty">I do not mind receiving other offers from charities and other reputablecompanies.		</td>	  </tr>	<tr>	  <td colspan="2" valign="top" class="text_form"><input name="add" type="submit" id="add" value="Send"></td>	  </tr>	  </form></table></body></html>

Link to comment
Share on other sites

The PHP code is fine, there's not a lot to screw up. Why do you think it's something you're doing? Are you trying this on a home computer or are the PHP pages on an online server? You might also try to use a from address with the email, there are examples of that on the php.net reference.

Link to comment
Share on other sites

The PHP code is fine, there's not a lot to screw up. Why do you think it's something you're doing? Are you trying this on a home computer or are the PHP pages on an online server? You might also try to use a from address with the email, there are examples of that on the php.net reference.
The pages are on a live server, but I don't understand why it won't work. I didn't quite understand what you meant by 'from address'
Link to comment
Share on other sites

Most emails have a "to" and a "from" that are both email addresses. Your email does not have a from address.
It doesn't seem to matter what I do no e-mails come through. I've changed the php to:
<?phpini_set(SMTP, 'smtp.smptaddress.com');$title = $_POST['title'];$firstname = $_POST['firstname'];$lastname = $_POST['lastname'];$address = $_POST['address'];$town = $_POST['town'];$city = $_POST['city'];$postcode = $_POST['postcode'];$phone = $_POST['phone'];$mobile = $_POST['mobile'];$email = $_POST['email'];$day = $_POST['day'];$month = $_POST['month'];$heard = $_POST['heard'];;$comments = $_POST['comments'];$sms = $_POST['sms'];$post = $_POST['post'];$thirdparty = $_POST['thirdparty'];$to = 'email@address.com';$headers = 'From: email@address.com';$email_body = $_POST['title'];$_POST['firstname'];$_POST['lastname'];$_POST['address'];$_POST['town'];$_POST['city'];$_POST['postcode'];$_POST['phone'];$_POST['mobile'];$_POST['email'];$_POST['day'];$_POST['month'];$_POST['heard'];$_POST['comments'];$_POST['sms'];$_POST['post'];$_POST['thirdparty'];mail($to, 'My Subject', $email_body, $headers);?>

but again no e-mails seem to be coming through. I can't fingure out why

Link to comment
Share on other sites

It should be sending emails out. If it's not, you might want to ask your host why you're having problems. Keep the script simple, this is all you need to test:

<?phpif (!mail("test@test.com", "test message", "This is a test", "From: test@test.com")){  echo "The email was not accepted for delivery by the mail server.";}else{  echo "The email was accepted for delivery by the mail server.";}?>

Obviously change the addresses to your own, but that's what you want to test with.

Link to comment
Share on other sites

It should be sending emails out. If it's not, you might want to ask your host why you're having problems. Keep the script simple, this is all you need to test:
<?phpif (!mail("test@test.com", "test message", "This is a test", "From: test@test.com")){  echo "The email was not accepted for delivery by the mail server.";}else{  echo "The email was accepted for delivery by the mail server.";}?>

Obviously change the addresses to your own, but that's what you want to test with.

Thank you for all of your help, it's still not working so I'll call the hosting company tomorrow
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...