error on blank form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

error on blank form

Post 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?
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: error on blank form

Post 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
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: error on blank form

Post by danwguy »

nope, I changed them all to == and still the email goes through without one of the fields filled in.
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: error on blank form

Post 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
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: error on blank form

Post 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?
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: error on blank form

Post 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
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: error on blank form

Post by Neilos »

Oh yes I see it now, doh lol.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: error on blank form

Post 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?
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: error on blank form

Post 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);
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: error on blank form

Post 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.
Post Reply