PHP Question - send form to different email addresses

7 views
Skip to first unread message

Domanique Alicia

unread,
Oct 25, 2010, 8:22:06 PM10/25/10
to webDorks
Hey all,

I have run into a problem, and I was hoping someone could advise: I am
creating a contact form. One of the inputs (Radio Button) asks the
user if they are either a "New Client" or an "Existing Client". Based
on their selection, I'd like to send the data to a different email:
newc...@yourdomain.com and existin...@yourdomain.com



Some code:

----------------------------------------------------------------------------------------------------------------------------
CONTACT.HTML:
----------------------------------------------------------------------------------------------------------------------------
<form action="contact.php" method="post">
<p>Name: <input type="text" name="yourname" /><br />
E-mail: <input type="text" name="email" /><br />
Telephone: <input type="text" name="telephone" /><br />
Address: <input type="text" name="street" /><br />
City: <input type="text" name="city" /><br />
State: <select name="state">
<option value="blank"> </option>
<option value="AL">Alabama</option>
...
<option value="WY">Wyoming</option>
</select>
Zip: <input type="text" name="zip"></p>

<p>Are you a current client?
<input type="radio" name="client" value="Yes" /> Yes
<input type="radio" name="client" value="No" /> No</p>

<p><b>Your comments:</b><br />
<textarea name="inquiry" rows="10" cols="40"></textarea></p>

<p><input type="submit" value="Send it!"></p>
</form>


----------------------------------------------------------------------------------------------------------------------------
CONTACT.PHP
----------------------------------------------------------------------------------------------------------------------------
<?php
/* Set e-mail recipient */
if ( $_POST['client'] == 'yes' ){
$myemail = "existin...@yourdomain.com";
} elseif ( $_POST['client'] == 'no' ) {
$myemail = "newc...@yourdomain.com";
}

/* Gets all inpus */
$yourname = check_input($_POST['yourname'], "Enter your name");
$email = check_input($_POST['email']);
$telephone = check_input($_POST['telephone']);
$street = check_input($_POST['street']);
$city = check_input($_POST['city']);
$state = check_input($_POST['state']);
$zip = check_input($_POST['zip']);
$client = check_input($_POST['client']);
$inquiry = check_input($_POST['inquiry'], "Write your comments");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)){
show_error("E-mail address not valid");
}

/* Message emailed */
$message = "Hello!
Your contact form has been submitted by:
Name: $yourname
Email: $email
Telephone: $telephone

Address:
$street
$city, $state $zip

Current Client: $client

Message:
$inquiry

End of message
";

/* Send the message using mail() function */
mail($myemail, "Contact via GordonReevesLaw.com", $message);

/* Redirect visitor to the thank you page */
header('Location: thanks.html');
exit();

/* Strips message of crazy characters */
function check_input($data, $problem=''){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0){
show_error($problem);
}
return $data;
}

/* Show errors */
function show_error($myError){
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>

----------------------------------------------------------------------------------------------------------------------------



So, in testing the form, I filled it out, once as a new client, and
once as an existing client. However, regardless of which radio button
I select, both form-entries are sent to the latter email.

Any ideas on why my if-statement isn't working? Can I not look to see
if the input matches a string on a radio button? I'm a little lost,
and most of the tutorials I've looked at online address sending forms
to multiple email addresses, not different addresses.

Thanks in advance,
Domanique

Sam Keen

unread,
Oct 26, 2010, 11:31:52 AM10/26/10
to webd...@googlegroups.com
looks like you may have a case sensitivity issue
the form has 'No' and 'Yes' and you are comparing to 'no' and 'yes'

var_dump('Yes'=='yes');
output: false
var_dump('No'=='no');
output: false

you can either change the case in the if block or do a case insensitive compare (using strtolower, strcasecmp, etc)

also be weary of directly reading and using user input ($_POST) with out filtering
@see

hope this helps,
If you have more PHP questions, you may want to use the PHP mailing list at http://groups.google.com/group/pdxphp

cheers,
sam
 


--
You received this message because you are subscribed to the Google Groups "webDorks" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdorks+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdorks?hl=en.




--
Sam Keen
@samkeen

Domanique Alicia

unread,
Oct 26, 2010, 6:15:32 PM10/26/10
to webDorks
That's weird, I thought I answered this response earlier. Meh.

Of course, it was the case. *facepalm*

Sam, good point about the filtering. Good links too.

Thanks for your help,
Domanique



On Oct 26, 8:31 am, Sam Keen <sam....@gmail.com> wrote:
> looks like you may have a case sensitivity issue
> the form has 'No' and 'Yes' and you are comparing to 'no' and 'yes'
>
> var_dump('Yes'=='yes');
> output: false
> var_dump('No'=='no');
> output: false
>
> you can either change the case in the if block or do a case insensitive
> compare (using strtolower, strcasecmp, etc)
>
> also be weary of directly reading and using user input ($_POST) with out
> filtering
> @seehttp://devzone.zend.com/article/1113http://www.php.net/manual/en/intro.filter.php
>
> hope this helps,
> If you have more PHP questions, you may want to use the PHP mailing list athttp://groups.google.com/group/pdxphp
>
> cheers,
> sam
>
> On Mon, Oct 25, 2010 at 5:22 PM, Domanique Alicia <domaniqueali...@gmail.com
>
>
>
>
>
> > wrote:
> > Hey all,
>
> > I have run into a problem, and I was hoping someone could advise: I am
> > creating a contact form. One of the inputs (Radio Button) asks the
> > user if they are either a "New Client" or an "Existing Client". Based
> > on their selection, I'd like to send the data to a different email:
> > newcli...@yourdomain.com and existingcli...@yourdomain.com
>
> > Some code:
>
> > --------------------------------------------------------------------------- -------------------------------------------------
> > CONTACT.HTML:
>
> > --------------------------------------------------------------------------- -------------------------------------------------
> >          $myemail = "existingcli...@yourdomain.com";
> >     } elseif ( $_POST['client'] == 'no' ) {
> >         $myemail = "newcli...@yourdomain.com";
> > webdorks+u...@googlegroups.com<webdorks%2Bunsubscribe@googlegroups.c om>
> > .

Domanique Alicia

unread,
Oct 26, 2010, 12:30:20 PM10/26/10
to webDorks
*facepalm*
The Case. Of course.

Thanks for the tips on the filtering Sam. I hadn't considered that
originally.

- Domanique




On Oct 26, 8:31 am, Sam Keen <sam....@gmail.com> wrote:
> looks like you may have a case sensitivity issue
> the form has 'No' and 'Yes' and you are comparing to 'no' and 'yes'
>
> var_dump('Yes'=='yes');
> output: false
> var_dump('No'=='no');
> output: false
>
> you can either change the case in the if block or do a case insensitive
> compare (using strtolower, strcasecmp, etc)
>
> also be weary of directly reading and using user input ($_POST) with out
> filtering
> @seehttp://devzone.zend.com/article/1113http://www.php.net/manual/en/intro.filter.php
>
> hope this helps,
> If you have more PHP questions, you may want to use the PHP mailing list athttp://groups.google.com/group/pdxphp
>
> cheers,
> sam
>
> On Mon, Oct 25, 2010 at 5:22 PM, Domanique Alicia <domaniqueali...@gmail.com
>
>
>
> > wrote:
> > Hey all,
>
> > I have run into a problem, and I was hoping someone could advise: I am
> > creating a contact form. One of the inputs (Radio Button) asks the
> > user if they are either a "New Client" or an "Existing Client". Based
> > on their selection, I'd like to send the data to a different email:
> > newcli...@yourdomain.com and existingcli...@yourdomain.com
> >          $myemail = "existingcli...@yourdomain.com";
> >     } elseif ( $_POST['client'] == 'no' ) {
> >         $myemail = "newcli...@yourdomain.com";
> > webdorks+u...@googlegroups.com<webdorks%2Bunsu...@googlegroups.com>
> > .
Reply all
Reply to author
Forward
0 new messages