Using "if" in mail forms

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
sonicthehedgehog
Forum Newbie
Posts: 8
Joined: Fri Sep 02, 2005 1:10 pm
Contact:

Using "if" in mail forms

Post by sonicthehedgehog »

I'm currently using a mail form to achieve a simple order form task. However, I want to be able to have an if statement to see if the payment method selected was PayPal or not and if it is to display something entirely different than if something else would have been selected.

Here is the mail form:

Code: Select all

<form action="processorder.php" method="post" >
<font face="Arial Rounded MT Bold" size="2" color="#860101">User Information</font><br>
<p><font size="1" face="Arial Rounded MT Bold">Desired Username:<br>
<input type="text" name="username" size="20" style="color:000000; background-color:FFFFFF; border-width: 1px; border-color:black"><br>
Password:<br>
<input type=password name="password" size="20" style="color:000000; background-color:FFFFFF; border-width: 1px; border-color:black"><br>
Confirm password:<br>
<input type=password name="password2" size="20" style="color:000000; background-color:FFFFFF; border-width: 1px; border-color:black"><br>
<br><font face="Arial Rounded MT Bold" size="2" color="#860101">Basic Information</font>
<p><font size="1" face="Arial Rounded MT Bold">First Name:<br>
<input type="text" name="name1" style="color:000000; background-color:FFFFFF; border-width: 1px; border-color:black"></font><font size="1" face="Arial Rounded MT Bold"><br>
Last Name:<br>
<input type="text" name="name2" size="20" style="color:000000; background-color:FFFFFF; border-width: 1px; border-color:black"><br>
Email Address:<br>
<input type="text" name ="email" style="color:black; background-color:FFFFFF; border-width: 1px; border-color:black"></font><br>
</font> 
			<font face="Arial Rounded MT Bold" size="1">Payment Method: </font> <br>
<select name="payment">
	<option value="PayPal">PayPal</option>
	<option value="Money Order">Money Order</option>
	<option value="Chashier's Check">Cashier's Check</option>
</select><br><br>
<font face="Arial Rounded MT Bold" size="2" color="#860101">Billing Information</font><br><br>
			<font face="Arial Rounded MT Bold" size="1">Street Address: </font> </font> <br>
<input type="text" name="street" size="35" style="color:black; background-color:white; border-width: 1px; border-color:black"><br>

<font face="Arial Rounded MT Bold" size="1">Country: </font> </font> <br>
<select name="country" size="1">
<option value="US">United States</option>
<option value="AF">Afghanistan</option>
//A lot more countries are posted, but there's no need for them here.
<br>
</select><br>
			<font face="Arial Rounded MT Bold" size="1">City: </font> </font> <br>
<input type="text" name="city" size="20" style="color:black; background-color:white; border-width: 1px; border-color:black"><br>
			<font face="Arial Rounded MT Bold" size="1">ZIP Code: </font> </font> <br>
<input type="text" name="zip" size="10" style="color:black; background-color:white; border-width: 1px; border-color:black"><br>
<font size="1">
<br>
<input type="submit" value="Submit" border-width: 1px; border-color:black"><br>
<br>
<font face="Arial Rounded MT Bold">All fields are required. 
&nbsp; </font></font><br>
&nbsp;</p>
		</form>
And here is the process form:

Code: Select all

<?
$username=$_POST['username'];
$password=$_POST['password'];
$password2=$_POST['password2'];
$firstname=$_POST['name1'];
$lastname=$_POST['name2'];
$email=$_POST['email'];
$payment=$_POST['payment'];
$address=$_POST['address'];
$country=$_POST['country'];
$state=$_POST['state'];
$city=$_POST['city'];
$to="fxdesigning@gmail.com";
$message="This person has ordered a membership package:\n----------------------------------------------------------\nName: $firstname $lastname\nEmail: $email\n----------------------------------------------------------\n Desired username: $username\nPassword: $password\nConfirmed password: $password2\nPayment Method: $payment\n\nBilling Information: \nStreet Address: $street\nCountry: $country\nState: $state\nCity: $city\n ZIP Code: $zip\n ";
if(mail($to,"$firstname $lastname has purchased a membership!",$message,"From: $email\n")) {
echo "Thank you, $firstname, for purchasing a membership. The administrator will need to approve and set up your account. Please be patient and you will receive an email shortly on the confirmation of your account.<br><br>If you did not fill in all of the fields correctly, your order will not be processed correctly.";
} else {
echo "Sorry $firstname but there was a problem placing the order, please check you filled out the form correctly.";
}
?>
Thank you in advance.
Last edited by sonicthehedgehog on Fri Sep 02, 2005 1:54 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

if($payment == 'PayPal')
{
//....
}
:?:
sonicthehedgehog
Forum Newbie
Posts: 8
Joined: Fri Sep 02, 2005 1:10 pm
Contact:

Post by sonicthehedgehog »

That didn't seem to work. Here is my code after being altered with your script:

Code: Select all

<html>
<body bgcolor="#FFFFFF">

<?
$username=$_POST['username'];
$password=$_POST['password'];
$password2=$_POST['password2'];
$firstname=$_POST['name1'];
$lastname=$_POST['name2'];
$email=$_POST['email'];
$payment=$_POST['payment'];
$address=$_POST['address'];
$country=$_POST['country'];
$state=$_POST['state'];
$city=$_POST['city'];
$to="fxdesigning@gmail.com";
$message="This person has ordered a membership package:\n----------------------------------------------------------\nName: $firstname $lastname\nEmail: $email\n----------------------------------------------------------\n Desired username: $username\nPassword: $password\nConfirmed password: $password2\nPayment Method: $payment\n\nBilling Information: \nStreet Address: $street\nCountry: $country\nState: $state\nCity: $city\n ZIP Code: $zip\n ";
if($payment == 'PayPal') 
{
"Hi."
} else {
if(mail($to,"$firstname $lastname has purchased a membership!",$message,"From: $email\n")) {
echo "Thank you, $firstname, for purchasing a membership. The administrator will need to approve and set up your account. Please be patient and you will receive an email shortly on the confirmation of your account.<br><br>If you did not fill in all of the fields correctly, your order will not be processed correctly.";
} else {
echo "Sorry $firstname but there was a problem placing the order, please check you filled out the form correctly.";
}
?>

</body>
</html>
And I received the following error:

Parse error: parse error, unexpected '}', expecting ',' or ';' in processorder.php on line 20

I also tried using echo, but nothing changed no matter what I tried using in addition.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're missing a semi-colon on the "Hi." line, nor will that text echo out. ;)
sonicthehedgehog
Forum Newbie
Posts: 8
Joined: Fri Sep 02, 2005 1:10 pm
Contact:

Post by sonicthehedgehog »

Stuck the semi-colon in there and am now getting the following error:

Parse error: parse error, unexpected $ in processorder.php on line 31
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Count the number of opening curly brackets versus the number of closing curly brackets. :wink:

Code: Select all

if($payment == 'PayPal') {
    "Hi.";
} 
else {
    if(mail($to,"$firstname $lastname has purchased a membership!",$message,"From: $email\n")) {
        echo "Thank you, $firstname, for purchasing a membership. The 
                  administrator will need to approve and set up your account. 
                  Please be patient and you will receive an email shortly on the 
                  confirmation of your account.<br><br>If you did not fill in all 
                  of the fields correctly, your order will not be processed correctly.";
    } 
    else {
        echo "Sorry $firstname but there was a problem placing the order, please check you filled out the form correctly.";
    }
}


should fix your error
sonicthehedgehog
Forum Newbie
Posts: 8
Joined: Fri Sep 02, 2005 1:10 pm
Contact:

Post by sonicthehedgehog »

Now I'm not getting an error but the "Hi" isn't showing up either. So I now get a blank page when the order is processed.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I said it wouldn't echo.
sonicthehedgehog
Forum Newbie
Posts: 8
Joined: Fri Sep 02, 2005 1:10 pm
Contact:

Post by sonicthehedgehog »

Ok, it works now and I also entered an html form by ending the php right after the echo and then starting it right after the form complete. I also added another if statement to send the order as an email no matter what payment was selected. Like so:

Code: Select all

if($payment == 'PayPal') 
if(mail($to,"$firstname $lastname has purchased a membership!",$message,"From: $email\n"))
{
echo ?>Please complete your order by clicking "Pay."<br><br>
	<form action="https://www.paypal.com/cgi-bin/webscr" method="post">

     <input type="hidden" name="cmd" value="_cart">

     <input type="hidden" name="upload" value="1">

     <input type="hidden" name="business" value="email@myaddress.com">

     <input type="hidden" name="item_name_1" value="Membership Access">


     <input type="hidden" name="amount_1" value="20.00">

     <input type="submit" name="submit" value="Pay">

     </form>
<?
}
else {
    if(mail($to,"$firstname $lastname has purchased a membership!",$message,"From: $email\n")) {
        echo "Thank you, $firstname, for purchasing a membership. The
                  administrator will need to approve and set up your account.
                  Please be patient and you will receive an email shortly on the
                  confirmation of your account.<br><br>If you did not fill in all
                  of the fields correctly, your order will not be processed correctly.";
    }
    else {
        echo "Sorry $firstname but there was a problem placing the order, please check you filled out the form correctly.";
    }
} 
?>
It works fine. Does anyone see any problems with it that might come up later?

Thanks so much for helping me through this. It's greatly appreciated.
sonicthehedgehog
Forum Newbie
Posts: 8
Joined: Fri Sep 02, 2005 1:10 pm
Contact:

Post by sonicthehedgehog »

Actually, now that I try it with the options of Money Order or Cashier's Check, the order isn't sent and nothing shows on the page. I know there's something simple that I'm missing that'll solve it. Ideas?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your newest code only supports PayPal. The else immediately before the second mail() is attached to the if with the first mail() call. Adding an opening brace after the PayPal check, and a closing brace before the first else should get the behaviour back. Note: you will not have an errors showing for the PayPal mail() call unless you add them.
sonicthehedgehog
Forum Newbie
Posts: 8
Joined: Fri Sep 02, 2005 1:10 pm
Contact:

Post by sonicthehedgehog »

I don't think I'm uderstanding just right. I tried both of these options:

Code: Select all

if($payment == 'PayPal')
if(mail($to,"$firstname $lastname has purchased a membership!",$message,"From: $email\n"))
{
echo ?>Please complete your order by clicking "Pay."<br><br>
	<form action="https://www.paypal.com/cgi-bin/webscr" method="post">

     <input type="hidden" name="cmd" value="_cart">

     <input type="hidden" name="upload" value="1">

     <input type="hidden" name="business" value="robbinsnest01@comcast.net">

     <input type="hidden" name="item_name_1" value="Membership Access">


     <input type="hidden" name="amount_1" value="20.00">

     <input type="submit" name="submit" value="Pay">

     </form>
<?
} 
{ else {
    	if($payment == 'Money Order')
And

Code: Select all

if($payment == 'PayPal') {
if(mail($to,"$firstname $lastname has purchased a membership!",$message,"From: $email\n"))
{
echo ?>Please complete your order by clicking "Pay."<br><br>
	<form action="https://www.paypal.com/cgi-bin/webscr" method="post">

     <input type="hidden" name="cmd" value="_cart">

     <input type="hidden" name="upload" value="1">

     <input type="hidden" name="business" value="robbinsnest01@comcast.net">

     <input type="hidden" name="item_name_1" value="Membership Access">


     <input type="hidden" name="amount_1" value="20.00">

     <input type="submit" name="submit" value="Pay">

     </form>
<?
} else {
    	if($payment == 'Money Order')
Neither worked. I think I'm misunderstanding exactly where to put the braces. I received two different parse errors.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

if($payment == 'PayPal')
{
  if(mail($to,"$firstname $lastname has purchased a membership!",$message,"From: $email\n"))
  {
?>Please complete your order by clicking "Pay."<br><br>
    <form action="https://www.paypal.com/cgi-bin/webscr" method="post">

     <input type="hidden" name="cmd" value="_cart">

     <input type="hidden" name="upload" value="1">

     <input type="hidden" name="business" value="email@myaddress.com">

     <input type="hidden" name="item_name_1" value="Membership Access">


     <input type="hidden" name="amount_1" value="20.00">

     <input type="submit" name="submit" value="Pay">

     </form>
<?
  }
} else {
    if(mail($to,"$firstname $lastname has purchased a membership!",$message,"From: $email\n")) {
        echo "Thank you, $firstname, for purchasing a membership. The
                  administrator will need to approve and set up your account.
                  Please be patient and you will receive an email shortly on the
                  confirmation of your account.<br><br>If you did not fill in all
                  of the fields correctly, your order will not be processed correctly.";
    }
    else {
        echo "Sorry $firstname but there was a problem placing the order, please check you filled out the form correctly.";
    }
}
?>
you were close.
sonicthehedgehog
Forum Newbie
Posts: 8
Joined: Fri Sep 02, 2005 1:10 pm
Contact:

Post by sonicthehedgehog »

Awesome. It all works now. Thanks so much for your help. I've never gotten better responses from any other forums.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

We get that alot :lol:
Post Reply