Page 1 of 1
error on blank form
Posted: Tue Jan 11, 2011 6:20 pm
by danwguy
so I have a form that people fill out to get their login details sent to them. No database or anything, just give me your username, account number, and email address and it's sent off to someone that sends them their login password. I want to have it so if the form is blank it can't be sent off, this is what I have but it sends the form regardless...
Code: Select all
$err = '0';
if($user === '' || $acct === '' || $email === '') {
$err = '1';
}
if($err !== 1) {
mail($to, $subject, $message, $headers);
}else{
echo "<p class='error'>There was an error processing your information. Please use your browser\'s back button and try again.</p>";
}
Like I said though, I tested the form and it sent off fine, so i tested again leaving the Account number field blank and it still sent off. I need some help on this one. Thank you.
Edit: Guess I should mention I already set everything with...
Code: Select all
$user = $_POST['user']; $acct = $_POST['account']; $email = $_POST['email'];
Is it just the syntax of my if statement?
Re: error on blank form
Posted: Tue Jan 11, 2011 6:54 pm
by Neilos
Does it work if you use the equal to operator instead of the identical operator? ie == and != instead of === and !==
http://www.php.net/manual/en/language.o ... arison.php
Re: error on blank form
Posted: Tue Jan 11, 2011 6:57 pm
by danwguy
nope, I changed them all to == and still the email goes through without one of the fields filled in.
Re: error on blank form
Posted: Tue Jan 11, 2011 7:13 pm
by Neilos
hmmm, i thought it might be some type difference using the === operator, as the server won't know what type the empty variable is supposed to be, I don't know what type an empty post variable has, but that might cause you grief if it is ambiguous.
Try using empty()
http://php.net/manual/en/function.empty.php
Re: error on blank form
Posted: Tue Jan 11, 2011 7:15 pm
by Neilos
An empty post variable does in fact return an empty string I just read it, as long as the source is sound lol, it was in a comment on php.net maybe someone wants to confirm this?
Re: error on blank form
Posted: Tue Jan 11, 2011 7:28 pm
by danwguy
Fixed it.... forgot to put the sinlge quotes around part of it... Here is the finished result that works.
Code: Select all
$err = '0';
if($user == '' || $acct == '' || $email == '') {
$err = '1';
}
if($err != '1') {
mail($to, $subject, $message, $headers);
echo "<div id='order'><p><img src='../images/title-login-retrieval.png' width='427' height='50' alt='Login Retrieval' title='Login Retrieval' /></p><h2>Your login credential request has been received!</h2> <br /><p>An email will be sent to the email address on file for your account with the username and password. If you do not have access to this email, or you need additional assistance, please contact us by calling (800) 995-6694</div>";
}else{
echo "<br /><br /><br /><p class='error'>There was an error processing your information. Please use your browser's back button and try again.</p><br /><br /><br />";
}
Thanks for all your help.
EDIT: One question I do have is.... is there a way to make the "from" part of the email a certain email addy? Like right now it's coming from ip@blahblahblah, whatever my webhosting sends it as but I would like it to show as
no-reply@whatever.com
Re: error on blank form
Posted: Tue Jan 11, 2011 7:33 pm
by Neilos
Oh yes I see it now, doh lol.
Re: error on blank form
Posted: Tue Jan 11, 2011 7:38 pm
by danwguy
How about the other question? is there a way for the email that is sent out to say that it's from
no-reply@whateveriput.com?
Re: error on blank form
Posted: Tue Jan 11, 2011 7:48 pm
by Neilos
yes:
Code: Select all
$headers = "From: My site<noreply@my_site.com>\r\n";
$headers .= "Reply-To: noreply@my_site.com\r\n";
$headers .= "Return-Path: noreply@my_site.com\r\n";
mail($recipient, $subject, $message, $headers);
Re: error on blank form
Posted: Wed Jan 12, 2011 11:53 am
by danwguy
When I add that to my script the email doesn't send.
EDIT: Fixed it have to add . after the code like this...
Code: Select all
$headers .= 'From: No Reply<no-reply@company_name.com>' . "\r\n";
$headers .= 'Reply-To: no-reply@company_name.com' . "\r\n";
$headers .= 'Return-Path: no-reply@company_name.com' . "\r\n";
Thank you again for all your help, I really appreciate it.