pxlcreations 10-22-2009, 07:34 PM Hi again, I have another question. So I have this PHP code which corresponds with an html form:
<?
$errors = '';
$myemail = $_POST['email'];//<-----Put Your email address here.
// Assuming the code stays the same each time ...
if(!strtolower($_POST['verify']) == "a12098"){
print 'Uh oh! You have forgotten to enter in the correct verification code! <br><br><a href="javascript:window.close();">Please click here to close this window.</a>';
exit;
}
if($myemail == ""){
print 'Uh oh! You have forgotten to enter in an email address! <br><br><a href="javascript:window.close();">Please click here to close this window.</a>';
}else{
$inputs=$_POST["i"];
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "Checklist:\n";
foreach($inputs as $item){
if($item!=""){
$email_body.=$item."\n";
}
}
$headers = "From: $myemail";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
print 'Your checklist has succesfully been submitted. The user your checklist is being sent to will receive ane mail shortly with the checklist information. Thank you.<br><br>Click here to close this window.';
}
?>
And it's all working really well. The only problem is when the user enters in an un-valid email address, it will give it the default "Internal Service Error" message. I would like the user to be presented with a custom message. Thanks in advance!
mlseim 10-22-2009, 07:45 PM double quotes are causing an error ... escape them with a slash ...
print 'Uh oh! You have forgotten to enter in an email address! <br><br><a href=\"javascript:window.close();\">Please click here to close this window.</a>';
pxlcreations 10-22-2009, 07:51 PM Oh, no. That's not what the error is. It's when the user enters in a non valid email I want them to see a custom error message. Here's a working copy:
http://www.pxlcreations.com/paraguide/checklist.html
mlseim 10-22-2009, 08:22 PM Maybe we should just stay with the thread below ... LOL!
http://www.webdesignforums.net/showthread.php?t=33468&page=2
I just figured-out it's you and I on different forums.
pxlcreations 10-22-2009, 08:31 PM Maybe we should just stay with this thread ... LOL!
http://www.webdesignforums.net/showthread.php?t=33468&page=2
I just figured-out it's you and I on different forums.
Haha! Oh, wow, this is a small world. Lol, ok, to WDF :D
pxlcreations 10-23-2009, 01:18 AM Ok, well if there is anybody out there who can help me a little bit more, that will be great (thanks mlseim). So I have these two pieces of code:
HTML:
<script language="Javascript" type="text/javascript">
var counter = 1;
var limit = 30;
function addInput(divName){
if (counter == limit) {
alert("You have reached the limit of adding " + counter + " inputs");
}
else {
var newdiv = document.createElement('div');
newdiv.innerHTML = "<br><input type='text' name='i[]'>";
document.getElementById(divName).appendChild(newdiv);
counter++;
}
}
</script>
<script type="text/javascript">
function createPopup(target){
var w = window.open("", target, "width=400, height=200");
w.focus();
}
</script>
</head>
<body>
<form name="form" action="email.php" method="post" target="submit" onsubmit="createPopup(this.target)">
<div id="dynamicInput">
<br><input type="text" name="i[]">
</div>
<input type="button" value="Add another text input" onClick="addInput('dynamicInput');">
<br>
<input name="email" type="text"> <br />
<input name="submit" type="submit">
<input type="reset"/>
</form>
PHP:
<?
$errors = '';
$myemail = $_POST['email'];//<-----Put Your email address here.
if($myemail == ""){
print 'Uh oh! You have forgotten to enter in an email address! <br><br><a href="javascript:window.close();">Please click here to close this window.</a>';
}else{
$inputs=$_POST["i"];
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "Checklist:\n";
foreach($inputs as $item){
if($item!=""){
$email_body.=$item."\n";
}
}
$headers = "From: $myemail";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
print 'Your checklist has succesfully been submitted. The user your checklist is being sent to will receive ane mail shortly with the checklist information. Thank you.<br><br>Click here to close this window.';
}
?>
I would like to add 2 features to these items. I would like a verification code required when the user submits the form (if it's wrong, they are asked to enter the correct code) and the form to display an error message when the email address entered is not valid (can't be sent). Thanks in advance.
pxlcreations 10-23-2009, 07:11 PM So anyone know how to fix this?
tomws 10-24-2009, 02:32 AM Google for "php captcha". They can be a little troublesome depending upon which you pick to use. I've used this one (http://www.phpcaptcha.org/) on a couple of sites.
pxlcreations 10-24-2009, 05:52 PM Ok, thanks! I'll look into that, do you know how to verify the email address in my PHP too?
tomws 10-24-2009, 06:52 PM The PHP mail() function doesn't/can't know if an email address is legitimate or if a message has been received. For that matter, it can't even tell you if a message has been sent anywhere by the server. It's only "aware" of whether a mail server has accepted a message for delivery or rejected it.
However, PHP can check if an email address is in a correct email address format. That requires regular expressions and PHP functions like preg_match. The search term would be something like "php email address validation". Should be lots of results and usable snippets on this forum.
pxlcreations 10-25-2009, 05:25 PM That php captcha I can't get to work. It will still send the email even if the verification code isn't correct. I'll keep working with it and let you know what I find.
pxlcreations 10-26-2009, 08:08 PM Ok, so I have this code:
<?
$errors = '';
$myemail = $_POST['email'];//<-----Put Your email address here.
if (eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $myemail))
{
print 'Invalid email';
}
if($myemail == ""){
print 'Uh oh! You have forgotten to enter in an email address! <br><br><a href="javascript:window.close();">Please click here to close this window.</a>';
}else{
$inputs=$_POST["i"];
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "Checklist:\n";
foreach($inputs as $item){
if($item!=""){
$email_body.=$item."\n";
}
}
$headers = "From: $myemail";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
print 'Your checklist has succesfully been submitted. The user your checklist is being sent to will receive ane mail shortly with the checklist information. Thank you.<br><br><a href="javascript:window.close();">Click here to close this window.</a>';
}
?>
And it works fine, except for this part:
if (eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $myemail))
{
print 'Invalid email';
}
It isn't printing that message if the email is invalid. How come?
tomws 10-26-2009, 08:20 PM Unrelated, but the ereg functions are deprecated as of PHP 5.3.0. You may want to update to a preg_match version.
The eregi function (http://php.net/eregi) returns the length of the match or FALSE for no match. Your conditional statement, then, translates as: "If this is a valid email address, tell them it's invalid. Else, no problem." You need to invert that with a not operator: !.
pxlcreations 10-26-2009, 08:57 PM Ok, I'm not completely following, where does the ! go? Here's my new code:
if (preg_match("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $myemail))
{
print 'Invalid email';
}
tomws 10-26-2009, 09:09 PM Your preg_match syntax is off. Check the man page (http://php.net/preg_match) for examples. The pattern should be wrapped by forward slashes, for example.
Negate the test by placing the not in front of the function name:
if (!preg_match($pattern, $myemail))
{
print 'Invalid email';
}
pxlcreations 10-26-2009, 09:19 PM Hmmm, I'm sorry I seem like a dummy but I'm still confused, so here's my full code:
<?
$errors = '';
$myemail = $_POST['email'];//<-----Put Your email address here.
if (!preg_match($pattern, $myemail))
{
print 'Invalid email';
}
if($myemail == ""){
print 'Uh oh! You have forgotten to enter in an email address! <br><br><a href="javascript:window.close();">Please click here to close this window.</a>';
}else{
$inputs=$_POST["i"];
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "Checklist:\n";
foreach($inputs as $item){
if($item!=""){
$email_body.=$item."\n";
}
}
$headers = "From: $myemail";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
print 'Your checklist has succesfully been submitted. The user your checklist is being sent to will receive ane mail shortly with the checklist information. Thank you.<br><br><a href="javascript:window.close();">Click here to close this window.</a>';
}
?>
And I get this error message when the wrong email is submitted:
Warning: preg_match() [function.preg-match]: Empty regular expression in /home/content/p/x/l/pxlcreations/html/paraguide/email.php on line 5
Invalid email/home/content/p/x/l/pxlcreations/dead.letter... Saved message in /home/content/p/x/l/pxlcreations/dead.letter Your checklist has succesfully been submitted. The user your checklist is being sent to will receive ane mail shortly with the checklist information. Thank you.
Click here to close this window.
tomws 10-26-2009, 09:22 PM Well, right. That's because you didn't replace my $pattern var with your pattern to match against.
Lamped 10-26-2009, 09:26 PM You took his example too literally, here use this:
if (!((strpos('..', $myemail) === False) && preg_match('/^\s*([a-z0-9\.&\'\+\-=_]{1,})@([a-z0-9\.\-]{2,}\.[a-z]{2,})\s*$/is', $myemail, $match) && (trim($match[1], '.') == $match[1]) && (trim($match[2], '.') == $match[2])))
{
print 'Invalid email';
}
This is a email verfication check I developed for a large project I'm working on. It's pretty good, you can have it.
It ensures the pieces before and after the @ do not begin or end with ".", that the domain name is a minimum of xx.xx (2 letters, a dot, and 2 letters), and that the section before the @ abides by the same email naming restrictions as hotmail. This is a slightly stricter version of the RFC email standard, but close enough.
pxlcreations 10-26-2009, 09:30 PM Thanks for the code computerX. The only problem is I get this error when there is a wrong email address:
Invalid email/home/content/p/x/l/pxlcreations/dead.letter... Saved message in /home/content/p/x/l/pxlcreations/dead.letter Your checklist has succesfully been submitted. The user your checklist is being sent to will receive ane mail shortly with the checklist information. Thank you.
Click here to close this window.
It looks like it is taking the error is still taking the text from the valid email address part of the PHP code.
Lamped 10-26-2009, 09:45 PM Thanks for the code computerX. The only problem is I get this error when there is a wrong email address:
Invalid email/home/content/p/x/l/pxlcreations/dead.letter... Saved message in /home/content/p/x/l/pxlcreations/dead.letter Your checklist has succesfully been submitted. The user your checklist is being sent to will receive ane mail shortly with the checklist information. Thank you.
Click here to close this window.
It looks like it is taking the error is still taking the text from the valid email address part of the PHP code.
if (!((strpos('..', $myemail) === False) && preg_match('/^\s*([a-z0-9\.&\'\+\-=_]{1,})@([a-z0-9\.\-]{2,}\.[a-z]{2,})\s*$/is', $myemail, $match) && (trim($match[1], '.') == $match[1]) && (trim($match[2], '.') == $match[2])))
{
print 'Invalid email';
die(); // Die makes the script stop.
}
Lamped 10-26-2009, 09:48 PM Actually...
<?
$errors = '';
$myemail = $_POST['email'];//<-----Put Your email address here.
if (!((strpos('..', $myemail) === False) && preg_match('/^\s*([a-z0-9\.&\'\+\-=_]{1,})@([a-z0-9\.\-]{2,}\.[a-z]{2,})\s*$/is', $myemail, $match) && (trim($match[1], '.') == $match[1]) && (trim($match[2], '.') == $match[2])))
{
print 'Invalid email';
} elseif($myemail == ""){
print 'Uh oh! You have forgotten to enter in an email address! <br><br><a href="javascript:window.close();">Please click here to close this window.</a>';
}else{
$inputs=$_POST["i"];
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "Checklist:\n";
foreach($inputs as $item){
if($item!=""){
$email_body.=$item."\n";
}
}
$headers = "From: $myemail";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
print 'Your checklist has succesfully been submitted. The user your checklist is being sent to will receive ane mail shortly with the checklist information. Thank you.<br><br><a href="javascript:window.close();">Click here to close this window.</a>';
}
?>
Would be better - integrate that "if" into your existing "if, else" structure to maintain readability.
pxlcreations 10-26-2009, 09:54 PM That worked perfectly! Thank you! Thank you tomws also for all the help!
pxlcreations 11-01-2009, 01:32 PM Ok, the verification and such is now complete! Thanks to all those that helped. Another questions, I have this final code:
<?
$errors = '';
$myemail = $_POST['email'];//<-----Put Your email address here.
if (!((strpos('..', $myemail) === False) && preg_match('/^\s*([a-z0-9\.&\'\+\-=_]{1,})@([a-z0-9\.\-]{2,}\.[a-z]{2,})\s*$/is', $myemail, $match) && (trim($match[1], '.') == $match[1]) && (trim($match[2], '.') == $match[2])))
{
print 'Invalid email';
} elseif($myemail == ""){
print 'Uh oh! You have forgotten to enter in an email address! <br><br><a href="javascript:window.close();">Please click here to close this window.</a>';
}else{
$inputs=$_POST["i"];
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "Checklist:\n";
foreach($inputs as $item){
if($item!=""){
$email_body.=$item."\n";
}
}
$headers = "From: $myemail";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
print 'Your checklist has succesfully been submitted! You will receive an email shortly with the checklist information. Thank you!<br><br><a href="javascript:window.close();">Click here to close this window.</a>';
}
?>
That works fine but I would to be able to add a second field. Something like this..
$inputs=$_POST["i"];
$inputs=$_POST["a"];
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "Checklist:\n";
foreach($inputs as $item){
if($item!=""){
$email_body.=$item."\n";
}
}
So it will post the $inputs=$_POST["i"]; and $inputs=$_POST["a"]; in the final checklist. How would this be accomplished?
pxlcreations 11-03-2009, 09:06 PM Any help?
tomws 11-03-2009, 09:11 PM I'm confused what you're asking as it seems you only want to drop some extra output into the $email_body variable, which is just a trivial matter of concatenation.
shadowmaniac 11-04-2009, 04:08 AM $inputs = array($_POST["i"], $_POST["a"]);
pxlcreations 11-04-2009, 02:42 PM Thanks shadowmaniac, except now when the email is sent, it just says "array" instead of what was entered.
pxlcreations 11-04-2009, 02:54 PM Ok, this is a more pressing error right now. I enter in a not valid email and I get the "Invalid Email" text but when I enter in no email at all, I get this message:
Warning: strpos() [function.strpos]: Empty delimiter. in /home/content/p/x/l/pxlcreations/html/paraguide/email.php on line 5
Invalid email
And here's my PHP code:
<?
$errors = '';
$myemail = $_POST['email'];//<-----Put Your email address here.
if (!((strpos('..', $myemail) === False) && preg_match('/^\s*([a-z0-9\.&\'\+\-=_]{1,})@([a-z0-9\.\-]{2,}\.[a-z]{2,})\s*$/is', $myemail, $match) && (trim($match[1], '.') == $match[1]) && (trim($match[2], '.') == $match[2])))
{
print 'Invalid email';
} elseif($myemail == ""){
print 'Uh oh! You have forgotten to enter in an email address! <br><br><a href="javascript:window.close();">Please click here to close this window.</a>';
}else{
$inputs=$_POST["i"];
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "Checklist:\n";
foreach($inputs as $item){
if($item!=""){
$email_body.=$item."\n";
}
}
$headers = "From: $myemail";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
print 'Your checklist has succesfully been submitted! You will receive an email shortly with the checklist information. Thank you!<br><br><a href="javascript:window.close();">Click here to close this window.</a>';
}
?>
tomws 11-04-2009, 03:07 PM shadowmaniac's code is wrong for your usage. All it does is create an array of arrays, and your code is set up to iterate over an array, which in this case would be the "parent" array.
Regarding the strpos error, that should be expected. No email address means you're passing an empty string to the strpos function (http://php.net/strpos) where an argument is required. Set up an additional pre-test for empty($myemail).
pxlcreations 11-04-2009, 03:28 PM Thanks tom for the quick reply, except I don't know what you're talking about. I am a huge NOOB when it comes to PHP, HTML and CSS are my strong points but PHP is not. What do you mean by setting up an additional pre-test?
tomws 11-04-2009, 03:36 PM I mean test it before you use it. You're currently not testing whether the variable has a value before passing it to a function that requires it to have a value. So, test it before you use it by passing it through the empty() function to determine whether it has a value.
EDIT: I just noticed you're already testing for an empty string. However, it's in your else statement. You need to make that the first test, and then test the strpos after that.
pxlcreations 11-04-2009, 03:39 PM So here's what I did... I entered in an invalid email and I got the "Invalid Email" text then I entered in nothing and I got the error I talked about a page before. Is that what you mean?
tomws 11-04-2009, 03:43 PM That just sounds like the problem description again, so no, that's not what I'm saying. I recognize the problem.
One of us is having a comprehension fail. Let's try it this way. I'll fix your code the way I just said to fix it.
if($myemail == ""){
print 'Uh oh! You have forgotten to enter in an email address! <br><br><a href="javascript:window.close();">Please click here to close this window.</a>';
} elseif (!((strpos('..', $myemail) === False) && preg_match('/^\s*([a-z0-9\.&\'\+\-=_]{1,})@([a-z0-9\.\-]{2,}\.[a-z]{2,})\s*$/is', $myemail, $match) && (trim($match[1], '.') == $match[1]) && (trim($match[2], '.') == $match[2])))
{
print 'Invalid email';
}
else{
pxlcreations 11-04-2009, 03:50 PM Ok, so the if($myemail == ""){ needed to come first? That way it would see if an email was present and if not, it would give that error. Then, if the email was present but invalid, it would give the error for invalid email. I think I see what you did. Thank you!
tomws 11-04-2009, 03:53 PM That's right. It needs to be in that order because you're using the same variable as an argument to strpos. Since strpos complains about an empty string (see the previous error), you need to filter out that possibility first.
EDIT: I should also add that checking variables before using them is good programming practice. It's a great way to make sure that functions or blocks of code are getting the data into them that you think they should be getting into them. It makes the process of writing code a bit longer and more tedious, but your scripts/programs are also more robust (and, perhaps, your job more secure).
shadowmaniac 11-04-2009, 04:21 PM Back to your question regarding multiple items in the checklist.
$inputs = array($_POST["i"], $_POST["a"]);
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "Checklist:\n";
foreach($inputs as $item)
{
if(!empty($item))
{
if(is_array($item))
{
foreach($item as $value)
{
$email_body.=$value."\n";
}
} // if(is_array($item))
else
{
$email_body.=$item."\n";
} // else
} // if(!empty($item))
} // foreach($inputs as $item)
pxlcreations 11-04-2009, 08:41 PM Ooh! Thank works out but when the email is sent, each input is on separate lines. I want the a and i to be on the same lines. Like, here's my code:
<br><input type="text" name="i[]" style="border:solid 1px #999999;width:10px;margin-right:4px;"><input type="text" name="i[]" style="border:solid 1px #999999;width:380px;margin-right:4px;"><input type="checkbox" name="1" onChange = "enableDisableTextField(this)" />
<br><input type="text" name="i[]" style="border:solid 1px #999999;width:10px;margin-right:4px;"><input type="text" name="i[]" style="border:solid 1px #999999;width:380px;margin-right:4px;"><input type="checkbox" name="1" onChange = "enableDisableTextField(this)" />
so I want the "a" and "i" that are together to be right next to each other when the email is sent.
pxlcreations 11-05-2009, 10:22 PM I might need to clarify that more.. so I'm going to have two text boxes on one line. I then want each box to be together per line, so like the 2 on one line will be together, then a line space which will have the second line below.
pxlcreations 11-07-2009, 12:56 PM Anyone know how to accomplish this?
tomws 11-08-2009, 01:55 AM You're going to need to change your code to loop through them and interlace them like that. The existing loop and array of arrays won't work for that.
pxlcreations 11-08-2009, 01:08 PM Um, so what does loop mean and how would I accomplish it? Thank you for the tip btw!
pxlcreations 11-10-2009, 08:37 PM So how would I do a loop?
tomws 11-10-2009, 08:48 PM If you're unfamiliar with loops, you're most likely well over your head with any of the rest of this. I'd recommend going back and checking out some basic programming tutorials. Here's one for PHP loops (http://www.w3schools.com/php/php_looping.asp).
|
|