Rob in hood Posted February 26, 2010 Report Share Posted February 26, 2010 I have some php code to send me an email with all of an HTML form's data layed out. I load up the page, type in some random stuff and press submit. I then get this response: The from address provided is invalid! I press back - change nothing - and click submit again, and then I get this: (the correct response) The from address provided is invalid! ff55@gg name => Test from => ff55@gg contactno => 050505 msg => MESSAGE The same thing happens when I put in a correctly formed email address. I first get the error, press back, submit again and it goes through without a problem and I receive the email. Once it's worked once, it will continue to work when I press back and re-submit. Until I change anything in the form - then it gives the erroneous response once. Click back, submit again and it's back to working properly. This is happening in the pages dished out by the server, not on something I'm running locally. I am really at a loss here - don't know how I'm supposed to debug code which works every second time it's run O_o. Any suggestions would be appreciated. Here's the strange php code. <?php //first make sure from is sanitary $from = $_POST["from"]; $from=filter_var($from, FILTER_SANITIZE_EMAIL); //this first cleans it, so that it contains the right kind of characters if(filter_var($from, FILTER_VALIDATE_EMAIL)){ //this checks the resulting clean address for well-formedness $to = "myaddress@gmail.com"; $subject = "Feedback"; //$message = $_POST["msg"] + ; reset($_POST); while (list($key, $val) = each($_POST)) { $message .= "$key => $val \n"; } $headers = "CC: anotheraddress@gmail.com"; //mail($to,$subject,$message,$headers); echo "Thank you! We will get back to you shortly."; } else { echo 'The from address provided is invalid!'; echo ' '.$from.' '; reset($_POST); while (list($key, $val) = each($_POST)) { echo "$key => $val "; } } ?> Quote Link to comment Share on other sites More sharing options...
falkencreative Posted February 26, 2010 Report Share Posted February 26, 2010 You mind posting the filter_var() function code? Quote Link to comment Share on other sites More sharing options...
Rob in hood Posted February 26, 2010 Author Report Share Posted February 26, 2010 You mind posting the filter_var() function code? It's a built-in PHP function. I learnt about it on w3schools. I was also wondering if that was what was screwing things up - but there's missing output in the error case: it's as if the form post data is not actually getting through on the first shot. Quote Link to comment Share on other sites More sharing options...
krillz Posted February 26, 2010 Report Share Posted February 26, 2010 Hmm let's see if we can spot it, just a couple of quick observations: filter_var($from, FILTER_VALIDATE_EMAIL) Do not rely on FILTER_VALIDATE_EMAIL especially in online mail forms, I'll quote a very good phrase that indicates why you shouldn't rely on this filter option alone: It will happily pronounce "yourname" as valid because presumably the "@localhost" is implied, so you still have to check that the domain portion of the address exists. Now let's continue: Most important first step, sounds like a stupid mistake, but people tend to miss this somehow. Are you running the code without checking if the form was sent, which basically means that first time you enter that page the whole validation code starts, but the problem is that nothing has been sent thus $_POST is not filled with what you are looking for. And based on the error code generated I got a feeling it might have something to do with this. if that wasn't the case try the below things: Have you tried printing out $message after the loop to see if you get the string builds up correctly? The from address provided is invalid! The filter options removes all characters except letters, digits and !#$%&'*+-/=?^_`{|}~@.[]. Are you using any symbol that is not allowed? Quote Link to comment Share on other sites More sharing options...
Rob in hood Posted February 27, 2010 Author Report Share Posted February 27, 2010 Most important first step, sounds like a stupid mistake, but people tend to miss this somehow.Are you running the code without checking if the form was sent, which basically means that first time you enter that page the whole validation code starts, but the problem is that nothing has been sent thus $_POST is not filled with what you are looking for. And based on the error code generated I got a feeling it might have something to do with this. It does seem like the $_POST is not filled on the first submit. What do I do to fix this? This code while (list($key, $val) = each($_POST)) { echo "$key => $val "; } prints out all the key-value pairs inside the $_POST. On the first run it doesn't print out anything, implying $_POST is empty. I can't see the reason why the $_POST is empty on the first go, but after I press back and Submit again it works as expected. Quote Link to comment Share on other sites More sharing options...
krillz Posted February 27, 2010 Report Share Posted February 27, 2010 Most important first step, sounds like a stupid mistake, but people tend to miss this somehow.Are you running the code without checking if the form was sent, which basically means that first time you enter that page the whole validation code starts, but the problem is that nothing has been sent thus $_POST is not filled with what you are looking for. And based on the error code generated I got a feeling it might have something to do with this. It does seem like the $_POST is not filled on the first submit. What do I do to fix this? This code while (list($key, $val) = each($_POST)) { echo "$key => $val "; } prints out all the key-value pairs inside the $_POST. On the first run it doesn't print out anything, implying $_POST is empty. I can't see the reason why the $_POST is empty on the first go, but after I press back and Submit again it works as expected. For post to be filled you need to send something to the page using the POST method. So that's your problem you are trying to run the email stuff even though no one has filled out and submited the form . You can do something as check if $_POST is set or not, and control the script using that. if ( isset($_POST["from"])) { //first make sure from is sanitary $from = $_POST["from"]; $from=filter_var($from, FILTER_SANITIZE_EMAIL); //this first cleans it, so that it contains the right kind of characters if(filter_var($from, FILTER_VALIDATE_EMAIL)){ //this checks the resulting clean address for well-formedness $to = "myaddress@gmail.com"; $subject = "Feedback"; //$message = $_POST["msg"] + ; reset($_POST); while (list($key, $val) = each($_POST)) { $message .= "$key => $val \n"; } $headers = "CC: anotheraddress@gmail.com"; //mail($to,$subject,$message,$headers); echo "Thank you! We will get back to you shortly."; } else { echo 'The from address provided is invalid!'; echo ' '.$from.' '; reset($_POST); while (list($key, $val) = each($_POST)) { echo "$key => $val "; } } } this way the code will only be ran if $_POST["from"] has been sent. Quote Link to comment Share on other sites More sharing options...
Rob in hood Posted February 27, 2010 Author Report Share Posted February 27, 2010 So that's your problem you are trying to run the email stuff even though no one has filled out and submited the form . Unfortunately, that is not the problem: I am filling in the form and then pressing submit. The html is sending the data method="post". The problem is that the first time I press submit, it's as if the .php file gets no POST data. I press back and press submit again - without changing anything - and then it miraculously works. Quote Link to comment Share on other sites More sharing options...
krillz Posted February 27, 2010 Report Share Posted February 27, 2010 post the whole code, sounds strange as shit, need to take a look at the whole picture. Quote Link to comment Share on other sites More sharing options...
Rob in hood Posted February 27, 2010 Author Report Share Posted February 27, 2010 Form code: Name *: Email address *: Contact no: Message *: email.php: <?php //first make sure from is sanitary $from = $_POST["from"]; $from=filter_var($from, FILTER_SANITIZE_EMAIL); //this first cleans it, so that it contains the right kind of characters if(filter_var($from, FILTER_VALIDATE_EMAIL)){ //this checks the resulting clean address for well-formedness $to = "fdfdfd@gmail.com"; $subject = "Feedback"; //$message = $_POST["msg"] + ; reset($_POST); while (list($key, $val) = each($_POST)) { $message .= "$key => $val \n"; } $headers = "CC: gdfdfdd@gmail.com"; //mail($to,$subject,$message,$headers); echo "Thank you! We will get back to you shortly."; } else { echo 'The from address provided is invalid!'; echo ' '.$from.' '; reset($_POST); while (list($key, $val) = each($_POST)) { echo "$key => $val "; } } ?> I just did another test, running the same, non-working email.php on a different HTML form -- had the same problem (submit 1st time=error, 2nd time=works). When I use a different, older email.php of mine then it works first time as expected. So it would seem there's something wrong with the PHP code then? An older, working email.php: <?php //first make sure from is sanitary $from = $_POST["from"]; $from=filter_var($from, FILTER_SANITIZE_EMAIL); //this first cleans it, so that it contains the right kind of characters if(filter_var($from, FILTER_VALIDATE_EMAIL)){ //this checks the resulting clean address for well-formedness $to = "dfdfdfs@gmail.com"; $subject = "Feedback"; $message = $_POST["msg"]; if ($_POST["name"]=="") $headers = "From: $from"; else $headers = "From: "+$_POST["name"]; mail($to,$subject,$message,$headers); echo "Mail Sent."; } else { echo 'The from address provided is invalid!'; echo ' $from'; } ?> Quote Link to comment Share on other sites More sharing options...
krillz Posted February 28, 2010 Report Share Posted February 28, 2010 //first make sure from is sanitary $from = $_POST["from"]; $from=filter_var($from, FILTER_SANITIZE_EMAIL); //this first cleans it, so that it contains the right kind of characters if(filter_var($from, FILTER_VALIDATE_EMAIL)){ //this checks the resulting clean address for well-formedness $to = "fdfdfd@gmail.com"; $subject = "Feedback"; $message = ''; while (list($key, $val) = each($_POST)) { $message .= "$key => $val "; } $headers = "CC: gdfdfdd@gmail.com"; //mail($to,$subject,$message,$headers); echo "Thank you! We will get back to you shortly."; } else { echo 'The from address provided is invalid!'; echo ' '.$from.' '; reset($_POST); while (list($key, $val) = each($_POST)) { echo "$key => $val "; } } Just tried the code, only error I recieved was $message undefined warning, so I declared it as empy. Can't generate any other errors. The code works if you fill out the email correctly you see the thank you message a long with the other values. And if not you get the incorrect message. So the code works for me. Quote Link to comment Share on other sites More sharing options...
Rob in hood Posted February 28, 2010 Author Report Share Posted February 28, 2010 Should I phone up my hosts and ask them wth is going on? Maybe it's a php problem on their side? Quote Link to comment Share on other sites More sharing options...
krillz Posted February 28, 2010 Report Share Posted February 28, 2010 what's the url to the page on the host you are experiencing this problem, so I can see it myself. Quote Link to comment Share on other sites More sharing options...
Rob in hood Posted February 28, 2010 Author Report Share Posted February 28, 2010 (edited) http://harmonydesignsolutions.com/ (It's still a work-in-progress) edit: I asked a friend to try it out and it worked for him! (He's using a mac, I'm on windows, both using firefox) Edited March 7, 2010 by Rob in hood Quote Link to comment Share on other sites More sharing options...
krillz Posted February 28, 2010 Report Share Posted February 28, 2010 http://harmonydesignsolutions.com/testhome/ (It's still a work-in-progress) first time submited I get the response : Thank you! We will get back to you shortly. Seems to work. Quote Link to comment Share on other sites More sharing options...
Rob in hood Posted February 28, 2010 Author Report Share Posted February 28, 2010 I'll try and get a few other people to test it, see if anyone else has the problem or if it's only me. Thanks for all the help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.