Send email if specific option value is selected

Hello,

I have been tasked with adding a function to an existing program (that I did not write). I have some exposure to PHP, but it’s not much depth.

The short of it is; when status is changed to option value=“3” (which means the quote has been converted to a sale), the managers want to be alerted with an email.

Here is how it exists at present. I have not fooled much with it as it is a live page on our admin.

<strong>Update Quote:</strong><br><br>
                    
                        <input type="hidden" name="qid" value="<?php echo $quoteinfo->fields['qid'];?>">
                        <input type="hidden" name="filename" value="<?php echo $quoteinfo->fields['filename'];?>">
                        <input type="hidden" name="email" value="<?php echo $quoteinfo->fields['email'];?>">
                        <?php if($quoteinfo->fields['status']!='-1') { ?>
                        Status: <select name="status" id="status">
                            <option value="-1" <?php if($quoteinfo->fields['status']=='-1') { ?> selected <?php } ?>><?php echo QUOTE_STATUS_1;?></option>
                            <option value="0" <?php if($quoteinfo->fields['status']=='0') { ?> selected <?php } ?>><?php echo QUOTE_STATUS_2;?></option>
                            <option value="1" <?php if($quoteinfo->fields['status']=='1') { ?> selected <?php } ?>><?php echo QUOTE_STATUS_3;?></option>
                            <option value="2" <?php if($quoteinfo->fields['status']=='2') { ?> selected <?php } ?>><?php echo QUOTE_STATUS_4;?></option>
                            <option value="3" <?php if($quoteinfo->fields['status']=='3') { ?> selected <?php } ?>><?php echo QUOTE_STATUS_5;?></option>
                        </select><br>
                        <?php } ?>
                        Follow-up: <input type="text" name="followup-date" id="followup-date" value="<?php echo $quoteinfo->fields['followup_date']!='' ? date('n/j/Y',$quoteinfo->fields['followup_date']) : date('n/j/Y');?>"><input type="button" name="clear" value="clear" onClick="document.getElementById('followup-date').value='';">
                        <textarea name="comments" rows="4"></textarea><br><br>
                        <?php if($quoteinfo->fields['status']=='-1') { ?>
                        	<?php if($quoteinfo->fields['approval']!='1') { ?>
                                <input type="submit" value="Place Comment" name="update-quote">
                                <input type="submit" value="Approve Quote" name="update-quote">
                                <input type="submit" value="Remove Quote" name="update-quote">
                        	<?php } else { ?>
                                <span style="color:red;">* Requires approval from management.</span><br><br>
                                <input type="submit" value="Place Comment" name="update-quote">
                                <input type="submit" value="Approve Quote" name="update-quote" <? if($admin_name->fields['admin_name']!='rjordan' && $admin_name->fields['admin_name']!='gschlatter' && $admin_name->fields['admin_name']!='admin' && $admin_name->fields['admin_name']!='lhoneycutt') { ?> disabled<?php } ?>>
                                <input type="submit" value="Remove Quote" name="update-quote">
                                <input type="hidden" name="approver" value="<?php echo $_SESSION['admin_name'];?>"/>
                        	<?php } ?>
                        <?php } else { ?>
                            <input type="submit" value="Update Quote" name="update-quote">
                        <?php } ?>
                        <input type="button" onClick="window.location='quotes.php?page=<?php echo $_GET['page'];?>'" value="back">

My instinct is that I will need an if statement somehow in conjunction with the submit button - but not entirely clear how to integrate that. Any help to get me pointed in the right direction or at some good sample code would be greatly appreciated.

Thanks.

Max

Somewhere there is a line with an action=“something” value you would have to write the if then statement in that file so basically it goes

get the submitted data
if status=3 then
email so and so
else
do what you would normally do.

Hope this helps… if you need more information and you can post the whole field I can try and write something more precise.

I think I have an idea based on what you’re saying - this is the code; any pitfalls I should be aware of in the way it is here?

Can I put an if statement inside of a form line? or is that a separate chunk?

<form action="quotes.php?page=<?php echo $_GET['page'];?>" method="post" onSubmit="return validateQuoteUpdate(this);">
            <hr>
            <table border="0" width="100%">
            <tr valign="top" width="50%">
                <td>
                    <table width="100%" cellspacing="0" cellpadding="2" border="0">
                        <tr>
                            <td valign="top"><strong>ASI/PPAI/SAGE:</strong></td>
                            <td>
                                <?php echo $quoteinfo->fields['asippaisage'] ? $quoteinfo->fields['asippaisage'] : 'n/a';?><br>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <img height="5" width="1" border="0" alt="" src="images/pixel_trans.gif"/>
                            </td>
                        </tr>
                        <tr>
                            <td valign="top"><strong>Customer:</strong></td>
                            <td>
                                <?php echo $quoteinfo->fields['name'];?><br>
                                <?php echo $quoteinfo->fields['company'];?><br>
                                <?php echo $quoteinfo->fields['address'];?><br>
                                <?php echo $quoteinfo->fields['city'];?>, <?php echo $quoteinfo->fields['state'];?>  <?php echo $quoteinfo->fields['zip'];?><br>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <img height="5" width="1" border="0" alt="" src="images/pixel_trans.gif"/>
                            </td>
                        </tr>
                        <tr>
                            <td valign="top"><strong>Telephone #:</strong></td>
                            <td><?php echo $quoteinfo->fields['phone'];?></td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <img height="5" width="1" border="0" alt="" src="images/pixel_trans.gif"/>
                            </td>
                        </tr>
                        <tr>
                            <td valign="top"><strong>Email Address:</strong></td>
                            <td><?php echo $quoteinfo->fields['email'];?></td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <img height="5" width="1" border="0" alt="" src="images/pixel_trans.gif"/>
                            </td>
                        </tr>
                    </table>
                </td>
                <td valign="top" width="50%">
                    <table width="100%" cellspacing="0" cellpadding="2" border="0">
                        <tr>
                            <td valign="top"><strong>Quote #:</strong></td>
                            <td><?php echo $quoteinfo->fields['qid'];?></td>
                        </tr>
                        <tr>
                            <td>
                                <img height="5" width="1" border="0" alt="" src="images/pixel_trans.gif"/>
                            </td>
                        </tr>
                        <tr>
                            <td valign="top"><strong>Item #:</strong></td>
                            <td>PPD-<?php echo $quoteinfo->fields['item_number'];?></td>
                        </tr>
                        <tr>
                            <td valign="top"><strong>Quantity:</strong></td>
                            <td><?php echo number_format($quoteinfo->fields['quantity']);?></td>
                        </tr>
                        <tr>
                            <td>
                                <img height="5" width="1" border="0" alt="" src="images/pixel_trans.gif"/>
                            </td>
                        </tr>
                        <tr>
                            <td valign="top"><strong>Logo/End-User Name:</strong></td>
                            <td width="50%"><?php echo $quoteinfo->fields['logoname'];?></td>
                        </tr>
                        <tr>
                            <td>
                                <img height="5" width="1" border="0" alt="" src="images/pixel_trans.gif"/>
                            </td>
                        </tr>
                        <tr>
                            <td valign="top"><strong>Quote Comments:</strong></td>
                            <td width="50%"><?php echo $quoteinfo->fields['comments'];?></td>
                        </tr>
                        <tr>
                            <td>
                                <img height="5" width="1" border="0" alt="" src="images/pixel_trans.gif"/>
                            </td>
                        </tr>
                        <tr>
                          <td valign="top"><strong>Sales Rep:</strong></td>
                          <td>
								<div id="sales_rep_1"><?php echo $quoteinfo->fields['sales_rep']!='' ? $quoteinfo->fields['sales_rep'] : 'n/a';?> <img src="images/button_icon_edit.png" style="padding-left:5px; vertical-align:bottom; cursor:pointer;" onClick="document.getElementById('sales_rep_2').style.display='inline';document.getElementById('sales_rep_1').style.display='none';"></div>
                            	<div id="sales_rep_2" style="display:none;"><select name="sales_rep"><option 
								<?php if($quoteinfo->fields['sales_rep']=='' || $quoteinfo->fields['sales_rep']=='n/a') { echo "selected";}?>>n/a</option><option 
								<?php if($quoteinfo->fields['sales_rep']=='Courtney') { echo "selected";}?>>Courtney</option><option 
								<?php if($quoteinfo->fields['sales_rep']=='Krystal') { echo "selected"; }?>>Krystal</option><option
								<?php if($quoteinfo->fields['sales_rep']=='Lavina') { echo "selected"; }?>>Lavina</option><option 
								</select>
                                </div>
                          </td>
                        </tr>
                        <tr>
                          <td colspan="2"><hr/></td>
                        </tr>
                        <tr>
                            <td valign="top"><strong>Credit Score:</strong></td>
                            <td width="50%"><?php echo $quoteinfo->fields['creditscore'];?></td>
                        </tr>
                        <tr>
                            <td>
                                <img height="5" width="1" border="0" alt="" src="images/pixel_trans.gif"/>
                            </td>
                        </tr>
                        <tr>
                            <td valign="top"><strong>Sales History:</strong></td>
                            <td width="50%"><?php echo $quoteinfo->fields['saleshistory'];?></td>
                        </tr>
                        <tr>
                            <td>
                                <img height="5" width="1" border="0" alt="" src="images/pixel_trans.gif"/>
                            </td>
                        </tr>
                        <tr>
                            <td valign="top"><strong>ASI High Credit Amount:</strong></td>
                            <td width="50%"><?php echo $quoteinfo->fields['highcredit'];?></td>
                        </tr>
                        <tr>
                          <td colspan="2"><hr/></td>
                        </tr>
                        <tr>
                          <td valign="top"><strong>View Quote (PDF):</strong></td>
                          <td><a href="http://www.leashables.com/quotes/<?php echo $quoteinfo->fields['filename'];?>" target="_blank">Click Here</a></td>
                        </tr>
                        <tr>
                            <td>
                                <img height="5" width="1" border="0" alt="" src="images/pixel_trans.gif"/>
                            </td>
                        </tr>
                    </table>
                </td>
             </tr>
             <tr>
             	<td style="background:#F2F1EE;" valign="top">
                	<strong>Update Quote:</strong><br><br>
                    
                        <input type="hidden" name="qid" value="<?php echo $quoteinfo->fields['qid'];?>">
                        <input type="hidden" name="filename" value="<?php echo $quoteinfo->fields['filename'];?>">
                        <input type="hidden" name="email" value="<?php echo $quoteinfo->fields['email'];?>">
                        <?php if($quoteinfo->fields['status']!='-1') { ?>
                        Status: <select name="status" id="status">
                            <option value="-1" <?php if($quoteinfo->fields['status']=='-1') { ?> selected <?php } ?>><?php echo QUOTE_STATUS_1;?></option>
                            <option value="0" <?php if($quoteinfo->fields['status']=='0') { ?> selected <?php } ?>><?php echo QUOTE_STATUS_2;?></option>
                            <option value="1" <?php if($quoteinfo->fields['status']=='1') { ?> selected <?php } ?>><?php echo QUOTE_STATUS_3;?></option>
                            <option value="2" <?php if($quoteinfo->fields['status']=='2') { ?> selected <?php } ?>><?php echo QUOTE_STATUS_4;?></option>
                            <option value="3" <?php if($quoteinfo->fields['status']=='3') { ?> selected <?php } ?>><?php echo QUOTE_STATUS_5;?></option>
                        </select><br>
                        <?php } ?>
                        Follow-up: <input type="text" name="followup-date" id="followup-date" value="<?php echo $quoteinfo->fields['followup_date']!='' ? date('n/j/Y',$quoteinfo->fields['followup_date']) : date('n/j/Y');?>"><input type="button" name="clear" value="clear" onClick="document.getElementById('followup-date').value='';">
                        <textarea name="comments" rows="4"></textarea><br><br>
                        <?php if($quoteinfo->fields['status']=='-1') { ?>
                        	<?php if($quoteinfo->fields['approval']!='1') { ?>
                                <input type="submit" value="Place Comment" name="update-quote">
                                <input type="submit" value="Approve Quote" name="update-quote">
                                <input type="submit" value="Remove Quote" name="update-quote">
                        	<?php } else { ?>
                                <span style="color:red;">* Requires approval from management.</span><br><br>
                                <input type="submit" value="Place Comment" name="update-quote">
                                <input type="submit" value="Approve Quote" name="update-quote" <? if($admin_name->fields['admin_name']!='rjordan' && $admin_name->fields['admin_name']!='gschlatter' && $admin_name->fields['admin_name']!='admin' && $admin_name->fields['admin_name']!='lhoneycutt') { ?> disabled<?php } ?>>
                                <input type="submit" value="Remove Quote" name="update-quote">
                                <input type="hidden" name="approver" value="<?php echo $_SESSION['admin_name'];?>"/>
                        	<?php } ?>
                        <?php } else { ?>
                            <input type="submit" value="Update Quote" name="update-quote">
                        <?php } ?>
                        <input type="button" onClick="window.location='quotes.php?page=<?php echo $_GET['page'];?>'" value="back">
                    
                </td>
                <td style="background:#E7E6E0;" valign="top">
                	<table width="100%" cellpadding="5">
                    	<?php while (!$quotecomments->EOF) { 
						$style = $style=='' ? 'background:#F2F1EE' : '';
						?>
                        <tr style="<?php echo $style;?>">
                        	<td width="25%"><?php echo $quotecomments->fields['author'];?> - <br>(<?php echo date('m/d/y h:i:s',$quotecomments->fields['date']);?>)</td>
                        	<td width="75%"><?php echo $quotecomments->fields['comment'];?></td>
                        </tr>
                        <?php $quotecomments->MoveNext();
						} ?>
                    </table>
                </td>
             </tr>
          </table>
          </form>

I don’t think so I would add the if then statement to the quotes.php file unless that is this file… then we will need some fancier footwork.

assuming your want to do X to the information after the email and do X when no email is needed you can turn the X piece into a function and just call it in the if then statement.

If you need an example let me know.

this is the basic structure of the if then statement
[php]<?php
if ($_REQUEST[‘status’] = ‘3’) {
echo “send and email
”;
echo “do other stuff”;
} else {
echo “do other stuff”;
}
?>[/php]

An example might be very helpful :smiley:

Here’s what I was playing with - this feels right, but seems too simple (if it’s even correct!)

<form action="quotes.php?page=<?php echo $_GET['page'];?>" method="post" onSubmit="
	<?php 
    if ($_REQUEST['status'] == '3') {
    $to = "Sales";
        $subject = $_POST['QUOTE_STATUS_5'];
        $name_field = $_POST['name'];
        $company_field = $_POST['company'];
        $item = $_POST['quote-product-1'];
        $description = $_POST['quote-product-1-description'];
        $quantity = $_POST['quote-product-1-quantity'];
        $price = $_POST['quote-product-1-price'];
        $extended = $_POST['quote-product-1-extended'];	
        $body = "From: $name_field\nCompany: $company_field\nItem#: $item\nDescription: $description\nQuantity:$quantity\nPrice: $price\nExtended: $extended";
    
        mail($to, $subject, $body);
    
    echo "return validateQuoteUpdate(this);";
    
    } else {
    
    echo "return validateQuoteUpdate(this);";
    
    }
?>">

I dont know what the tag is doing in there but that looks right, I am not at work anymore so I wont be able to double check until tomorrow, but it all looks like the right idea.

Sorry to intrude on your post, but, I would not do it exactly as you are both talking about.
First, from the code posted, the object would be to send the email AFTER the form is posted.

It is always helpful to us if you tell us what you are posting. (What file the code you post is from.)
So, if the quotes.php file is what you posted, then you are posting the form onto the same page.
Is that true?

If quotes.php is a separate file, then you would do Andrew’s code in it. I would do it slightly different.
If it is a separate file, then, you have code in it that “does something”. Andrew says:

if ($_REQUEST['status'] = '3') { echo "send and email
"; echo "do other stuff"; } else { echo "do other stuff"; }
I would do it more this way: (just do not duplicate the "do other stuff"...) [php] <?php if ($_REQUEST['status'] = '3') { echo "send and email
"; } else { echo "do other stuff"; } ?> [/php] The "do other stuff" would be the code currently in the file. You would just send the email and then continue with the rest of the current code.

Here is a good tutorial on sending emails:
http://www.w3schools.com/php/php_mail.asp
If you click on next-chapter, it will show more tutorial pages…
Hope this helps, Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service