Jump to content

advice about email


Recommended Posts

Ok the url is pageThe problem is the everything is working great, but the bottom section where I am sending the mail, the message that is suppose to appear if it is sent, is displaying first thing, and emailing it as soon as the page opens, don't I have the control structure set up right, I didn't want to test each and every field individually, I wanted to test them all like this, and have it display all error messages it runs across, but this didn't work, I also tried with if, the else if, then else at the end but it only displayed one error at a time, and I am not ready to start learning how to create an array with them and do it that way, I am learning very fast, getting better and better, and forcing myself into difficult situations to learn, but I am learning, and I don't want to rush that, until I have 100% understanding over control structures this is where my problem currently lies.

<?phpif (isset($_POST['submit'])){	if (empty($_POST['name'])){	print('The name field has been left blank<br />');	}if (empty($_POST['emailaddress'])){	print('The Email Address field was left blank<br />');	}if (empty($_POST['verifyemail'])){	print('The Verify Email Field was left blank<br />');	}if ($_POST['emailaddress'] != $_POST['verifyemail']){	print('The Email Addresses do not match, please fix this<br />');	}if (empty($_POST['description'])){	print('The Description Field was left blank<br />');	}}else {	$to = "businessman332211@hotmail.com";	$subject = "FunnyEmailForwards.com contact";	$name = $_POST['name'];	$emailaddress = $_POST['emailaddress'];	$verifyemail = $_POST['verifyemail'];	$description = $_POST['description'];	$from = "$emailaddress";	$message = "	Name: {$name}	Email Address: {$emailaddress}	Verify Email: {$verifyemail}	What the person had to say:	{$description}	";  if (mail($to, $subject, $message, $From)){  print('Thank you for contacting us!<br />');  print('We will try and get back to you within 24 hours.<br />');  }else{  print('There was an error sending the message, please try again!<br />');  print('If it does not work the second time, please send an email directly to<br />');  print('<a href="mailto:businessman332211@hotmail.com">Here</a>');  }};			?>

Link to comment
Share on other sites

To me it looks like "if !isset($_POST['submit']) then send an email". Look at the else statement after your error checking.I would suggest doing it differently, compile your errors in a string and if the string is empty then send an email else display the errors. That's just what I would do.

Link to comment
Share on other sites

I fixed all of that and repopulated most of the form fields, but I have having 3 problems

<!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>Contact Us</title></head><body><h3>Contact Us</h3><p><a href="index.php" title="Return Home">Return Home</a></p><p>If you need to contact us for any reason you can do so below.  We will try and respond within 24 hours.</p><br /><?phpif (isset($_POST['send'])){	if (empty($_POST['name'])){	print('The name field has been left blank<br />');	}if (empty($_POST['emailaddress'])){	print('The Email Address field was left blank<br />');	}if (empty($_POST['verifyemail'])){	print('The Verify Email Field was left blank<br />');	}if ($_POST['emailaddress'] != $_POST['verifyemail']){	print('The Email Addresses do not match, please fix this<br />');	}if (empty($_POST['description'])){	print('The Description Field was left blank<br />');	}else {	$to = "businessman332211@hotmail.com";	$subject = "FunnyEmailForwards.com contact";	$name = $_POST['name'];	$emailaddress = $_POST['emailaddress'];	$verifyemail = $_POST['verifyemail'];	$description = $_POST['description'];	$from = "$emailaddress";	$message = "	Name: {$name}	Email Address: {$emailaddress}	Verify Email: {$verifyemail}	What the person had to say:	{$description}	";  if (mail($to, $subject, $message, $From)){  print('Thank you for contacting us!<br />');  print('We will try and get back to you within 24 hours.<br />');  }else{  print('There was an error sending the message, please try again!<br />');  print('If it does not work the second time, please send an email directly to<br />');  print('<a href="mailto:businessman332211@hotmail.com">Here</a>');  }}  };			?><form name="contact" id="contact" action="contactus.php" method="post"><label for="name">Name:</label><br /><input name="name" id="name" type="text" maxlength="80" value="<?=isset($_POST['name']) ? $_POST['name'] : ''?>"/><br /><label for="emailaddress">Email Address:</label><br /><input name="emailaddress" id="emailaddress" type="text" maxlength="80" value="<?=isset($_POST['emailaddress']) ? $_POST['emailaddress'] : ''?>"/><br /><label for="verifyemail">Verify Email Address:</label><br /><input name="verifyemail" id="verifyemail" type="text" maxlength="80" value="<?=isset($_POST['verifyemail']) ? $_POST['verifyemail'] : ''?>"/><br /><label for="description">Message:</label><br /><textarea name="description" id="description" cols="20" rows="7" value="<?=isset($_POST['description']) ? $_POST['description'] : ''?>"/></textarea><br /><input name="send" id="send" type="submit" value="Contact Us!" /><input name="reset" id="reset" type="reset" value="Clear Form" /></form></body></html>

The three things I don't get are1. The description text area isn't repopulating.2. The form isn't disappearing when it is submitted.3. When I recieve an email it says from damon@ something and my client wanted it to say funnyemailforwards.com contactI know how to change the header, but when I changed it as shown in the code, it just ignored it, does there HAVE to be an email section there.

Link to comment
Share on other sites

Well first of all, it isn't disappearing when an email is sent because you don't have the form code inside of an else statement that says if(sent) then ok else print form.I'm not sure what you mean about your third problem but for your first I would not use the value property of textarea as I didn't know that existed, but rather put the text in between the textarea tags.

Link to comment
Share on other sites

asylum is right about #1, this is a textarea:<textarea name="..">text</textarea>If you want the form not to appear on success, either keep track of a success or an error variable, surround your form tags with if (isset($_POST['send']) && $errorString == "") or something like that.For #3, you aren't setting the from address correctly, so it's probably using the mail daemon or whatever. The fourth parameter in the mail function is all headers, so you need to create a From: header, and maybe a Reply-To: header as well. Here's an example from the documentation:

<?php$to      = 'nobody@example.com';$subject = 'the subject';$message = 'hello';$headers = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com';mail($to, $subject, $message, $headers);?>

Link to comment
Share on other sites

I did

<textarea name="description" id="description" cols="20" rows="7" /><?php if (isset($_POST['description'])){echo("$_POST['description']");}; ?></textarea>

But it had no affect on it, it still doesn't work, and I recently noticed it doesn't work right when you go to the website, and click submit it works, it validates, but if you fill out the fields a little and submit, it still gives errors, but if you come to teh page, and just type in teh description, then click submit, it validates, and on top of that, it automatically sends the email anyway, as soon as the description field gets filled out it sends the email why does that happen.

Link to comment
Share on other sites

if (empty($_POST['description'])){print('The Description Field was left blank<br />');}else {...

That is why you only need the description, because that's all that the else statement is on, that's why I said I would handle the errors completely differently, because your way doesn't work.

Link to comment
Share on other sites

$err = '';foreach($_POST as $k => $v)     if(empty($v))          $err .= "Please fill out the field named $v<br/>";if(empty($err))    mail(...);else    echo $err;    echo $form_code_repopulated;

Link to comment
Share on other sites

That's funny, I've been using error string for a while to deal with errors myself. I'm thinking about an error class for future things, but I've been doing things pretty similar.

$errorString = "";if ($_POST['name'] == "")  $errorString .= "blank_name;";if ($_POST['description'] == "")  $errorString .= "blank_desc;";...if (strstr($errorString, "blank_name") !== false)  echo "You need a name";if (strstr($errorString, "blank_desc") !== false)  echo "You need a description";

Also, instead of this:

<?php if (isset($_POST['description'])){echo("$_POST['description']");}; ?>

Do this:

<?php if (isset($_POST['description'])){  echo $_POST['description'];} ?>

You don't need the parens or quotes for the echo, and you don't need a semicolon after the curly brace.

Link to comment
Share on other sites

I am trying to figure this out, I changed what you said at the bottom, what about when I submit it with the description field filled in, it automatically sends hte email anyway. Should I start doing it in the other page, I use to use an external page, I could have knocked out 5 forms already due to the price of this one I am stuck on trying new things. I like learning, but by doing this, I like the look of doing small forms on the same page, and I like learning but I never encountered these difficulties I really really didn't want to start using them as arrays yet, not until I fully understood this, and if/else constructs better, and then be able to do it on my own, me having to jump into that already makes me frustrated, is there any other way

Link to comment
Share on other sites

Ok I think I get the point a little, so I can still use the same thing I am doing, I could take my current setup and just attribute them into error, I like this part

if (isset($_POST['send'])){	if (empty($_POST['name'])){	print('The name field has been left blank<br />');	}if (empty($_POST['emailaddress'])){	print('The Email Address field was left blank<br />');	}if (empty($_POST['verifyemail'])){	print('The Verify Email Field was left blank<br />');	}if ($_POST['emailaddress'] != $_POST['verifyemail']){	print('The Email Addresses do not match, please fix this<br />');	}if (empty($_POST['description'])){	print('The Description Field was left blank<br />');

I like the way that looks, the way it operates and the way it functions, doing it one at a time, I know the code is longer, but it's funner, it helps me learn it faster, and understand this, before I start using arrays to do it, which I will later after I can do this blindly with no help. Right now what I want to know is with the error handling, what is it, what's the purpose, are you speaking of trapping all of the errors into an array, and printing them ALL out at the end, or is there another purpose, thanks. That is the only reason I am currently reluctant using foreach or other type of constructs, it's just for those specific reasons, but I am starting to wonder I was going to do in external pages on anything but small contact forms, and i was trying to learn how to do it on the same page, but it's working out horribly.

Link to comment
Share on other sites

The only array I was using was the $_POST array which contains all of your form data and that's what I was looping through, then I was adding errors to the string appropriately and printing all the errors at the end and show the repopulated form again. If there were no errors then I would send the email.As far as I can tell it's not the fact that you're doing it on the same page that's causing the problem and if you want to keep the look and feel of the code you had before then try this:

$err = 0;if (isset($_POST['send'])){if (empty($_POST['name'])){$err++;print('The name field has been left blank<br />');}if (empty($_POST['emailaddress'])){$err++;print('The Email Address field was left blank<br />');}if (empty($_POST['verifyemail'])){$err++;print('The Verify Email Field was left blank<br />');}if ($_POST['emailaddress'] != $_POST['verifyemail']){$err++;print('The Email Addresses do not match, please fix this<br />');}if (empty($_POST['description'])){$err++;print('The Description Field was left blank<br />');}if($err) {//reprint form code goes here} else {//sending the email code goes here}

I hope it's starting to make more sense.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...