Page 1 of 1

php form to write in a database and send email

Posted: Mon Jan 11, 2010 1:01 am
by rodica
Hi,

I have this form that is writing now in a database; I would like it to send an email with the data submitted by the user in an email. The send email code is at the end. Please let me know if you can help. Thank you!

Here is the code:

<?php
if (isset($_POST['sub']) && count($errors)==0) {
?>


<style>
.wr_left_cell {
text-align:right;
font-weight:bold;
width:150px;
}

.wr_right_cell {
width:200px;
font-size:12px;
}

.wr_header_cell {
font-size:14px;
font-weight:bold;
padding-top:10px;
}

.wr_table tr {
vertical-align:top;
}

</style>



Thank you for registering your warranty. Please print this page for your records.
<br /><br />

<table class="wr_table">

<tr><td colspan="2" class="wr_header_cell">Pacbrake Product Info</td></tr>
<tr>
<td class="wr_left_cell">Kit Number Purchased:</td>
<td class="wr_right_cell"><?php echo(val('product')); ?></td>
</tr>

<tr>
<td class="wr_left_cell">Product Serial Number:</td>
<td class="wr_right_cell"><?php echo(val('product_serial_number')); ?></td>
</tr>

<tr>
<td class="wr_left_cell">Controller Serial Number:</td>
<td class="wr_right_cell"><?php echo(val('controller_serial_number')); ?></td>
</tr>

<?php if (val('product_install')=='dealer') { ?>
<tr><td colspan="2" class="wr_header_cell">Dealer / Installer</td></tr>
<tr>
<td class="wr_left_cell">Dealer Name:</td>
<td class="wr_right_cell"><?php echo(val('dealer_company')); ?></td>
</tr>

<tr>
<td class="wr_left_cell">City:</td>
<td class="wr_right_cell"><?php echo(val('dealer_city')); ?></td>
</tr>
<tr>
<td class="wr_left_cell">State/Province:</td>
<td class="wr_right_cell"><?php echo(val('dealer_state')); ?></td>
</tr>
<tr>
<td class="wr_left_cell">Date Installed:</td>
<td class="wr_right_cell"><?php echo(val('date_installed')); ?></td>
</tr>
<?php } ?>

<tr><td colspan="2" class="wr_header_cell">My Information</td></tr>
<tr>
<td class="wr_left_cell">First Name:</td>
<td class="wr_right_cell"><?php echo(val('first_name')); ?></td>
</tr>
<tr>
<td class="wr_left_cell">Last Name:</td>
<td class="wr_right_cell"><?php echo(val('last_name')); ?></td>
</tr>
<tr>
<td class="wr_left_cell">Email Address:</td>
<td class="wr_right_cell"><?php echo(val('email_address')); ?></td>
</tr>
<tr>
<td class="wr_left_cell">Street Address:</td>
<td class="wr_right_cell"><?php echo(val('cust_address')); ?></td>
</tr>
<tr>
<td class="wr_left_cell">City:</td>
<td class="wr_right_cell"><?php echo(val('cust_city')); ?></td>
</tr>
<tr>
<td class="wr_left_cell">Province/State:</td>
<td class="wr_right_cell"><?php echo(val('cust_state')); ?></td>
</tr>
<tr>
<td class="wr_left_cell">Postal Code/ZIP:</td>
<td class="wr_right_cell"><?php echo(val('cust_postal_code')); ?></td>
</tr>
<tr>
<td class="wr_left_cell">Home Phone:</td>
<td class="wr_right_cell"><?php echo(val('cust_home_phone')); ?></td>
</tr>
<!--<tr>
<td class="wr_left_cell">Cell Phone:</td>
<td class="wr_right_cell"><?php echo(val('cust_cell_phone')); ?></td>
</tr>-->

<tr><td colspan="2" class="wr_header_cell">My Truck Info</td></tr>
<tr>
<td class="wr_left_cell">Year:</td>
<td class="wr_right_cell"><?php echo(val('year')); ?></td>
</tr>
<tr>
<td class="wr_left_cell">Make:</td>
<td class="wr_right_cell"><?php echo(val('make')); ?></td>
</tr>
<tr>
<td class="wr_left_cell">Model:</td>
<td class="wr_right_cell"><?php echo(val('model')); ?></td>
</tr>
<tr>
<td class="wr_left_cell">Engine Serial No.:</td>
<td class="wr_right_cell"><?php echo(val('engine_serial_number')); ?></td>
</tr>
<tr>
<td class="wr_left_cell">VIN No.:</td>
<td class="wr_right_cell"><?php echo(val('vin_number')); ?></td>
</tr>
<tr>
<td class="wr_left_cell">Fuel:</td>
<td class="wr_right_cell"><?php echo(val('fuel')); ?></td>
</tr>
<tr>
<td class="wr_left_cell">Transmission:</td>
<td class="wr_right_cell"><?php echo(val('transmission')); ?></td>
</tr>
<tr>
<td class="wr_left_cell">Existing Aftermarket Products Already Installed:</td>
<td class="wr_right_cell"><?php echo(val('aftermarket')); ?></td>
</tr>
<tr>
<td class="wr_left_cell">What do you tow?:</td>
<td class="wr_right_cell"><?php echo(val('tow')); ?></td>
</tr>
<tr>
<td class="wr_left_cell">Comments:</td>
<td class="wr_right_cell"><?php echo(val('comments')); ?></td>
</tr>


</table>

<?php

} else {

?>

<style>
.wr_section_header {
font-weight:bold;
margin-top:10px;
margin-bottom:4px;
}

.wr_input_descriptor {
float:left;
text-align:right;
width:150px;
padding-top:5px;
padding-right:10px;
}

.wr_input_element {

}


</style>

<script>
function toggle_dealer_section(input) {
if(input==true) {
document.getElementById('dealer_section').style.display='block';
} else if(input==false) {
document.getElementById('dealer_section').style.display='none';
}
}
</script>


<!--<h1>Warranty Registration</h1>-->
<div>
<img src="/uploads/images/Headers/warranty_registration3.png" />
</div><br />

Thank you for purchasing a Pacbrake product. Warranty registrations must be submitted within 30 days of installation date. Please complete all the required fields. After you hit SUBMIT you will be taken to a printable warranty registration page with your details. Please print a copy for your records.
<br /><br />
Enjoy the ride with your new Pacbrake product and be sure to come back soon after to share your experience with us!
<br /><br />
Required fields are marked *
<br /><br />

<!-- error div -->
<?php if(isset($_POST['sub'])) { ?>
<div style="color:red;background-color:#ffeeee;border:1px solid red;display:<?php (isset($errors)&&count($errors)>0)?'block':'none'; ?>">
<?php
if (isset($errors)&&count($errors)==1) {
?>The following error has been reported:<?php
} elseif (isset($errors)&&count($errors)>1) {
?>The following errors have been reported:<?php
}
?>
<ul>
<?php

foreach($errors as $key => $value) {

echo("<li>".$errors[$key]."</li>");

}


?>
</ul>
</div>
<?php } ?>
<form action="" method="post">

<span class="wr_section_header">Pacbrake Product Info</span><br />
<div class="wr_input_descriptor">Kit Number Purchased</div>
<div class="wr_input_element"><input type="text" name="product" value="<?php echo(val('product')); ?>" style="border:1px solid <?php echo(is_missing('product',$missing_fields)?'red;':'black;'); ?>"/>*</div>
<div class="wr_input_descriptor">Serial Number<br />(for engine brakes and exhaust brakes ONLY)</div>
<div class="wr_input_element"><input type="text" name="product_serial_number" value="<?php echo(val('product_serial_number')); ?>" style="border:1px solid <?php echo(is_missing('product_serial_number',$missing_fields)?'red;':'black;'); ?>"/>*</div>
<br /><br /><br />
<div class="wr_input_descriptor" style="padding-top: 0;">Controller Serial Number<br />(for exhaust brakes equipped with controllers ONLY)</div>
<div class="wr_input_element"><input type="text" name="controller_serial_number" value="<?php echo(val('controller_serial_number')); ?>" style="border:1px solid <?php echo(is_missing('controller_serial_number',$missing_fields)?'red;':'black;'); ?>"/>*<br /><span>(ex: CC 12345)</span></div>
<br /><br /><br />
<span>PLEASE NOTE: Serial Numbers are required to complete warranty registration. However, if your purchased product does not have a serial number (for example, air springs) simply put N/A in the box.</span>

<br /><br /><br />


<span class="wr_section_header">Dealer/Installer</span><br />
<div class="wr_input_descriptor">Who installed this product?</div>
<div class="wr_input_element">&nbsp;&nbsp;<input onclick="toggle_dealer_section(false);" type="radio" name="product_install" value="myself" <?php if(!isset($_POST['product_install']) || (isset($_POST['product_install']) && $_POST['product_install'] == "myself")) { echo('checked'); } ?>/>Myself&nbsp;&nbsp;<input onclick="toggle_dealer_section(true);" type="radio" name="product_install" value="dealer" <?php if (isset($_POST['product_install']) && $_POST['product_install'] == "dealer") { echo('checked'); } ?>/>Dealer</div>

<?php
//figure out if dealer section is displayed or not..
if ((isset($_POST['product_install']) && $_POST['product_install'] == "no")) {
$display_section="block";
} else {
$display_section="none";
}
?>

<div id="dealer_section" style="display:<?php echo($display_section); ?>;">
<br /><br />
<div class="wr_input_descriptor">Dealer Name</div>
<div class="wr_input_element"><input type="text" name="dealer_company" value="<?php echo(val('dealer_company')); ?>" style="border:1px solid <?php echo(is_missing('dealer_company',$missing_fields)?'red;':'black;'); ?>"/>*</div>
<div class="wr_input_descriptor">City</div>
<div class="wr_input_element"><input type="text" name="dealer_city" value="<?php echo(val('dealer_city')); ?>" style="border:1px solid <?php echo(is_missing('dealer_city',$missing_fields)?'red;':'black;'); ?>"/>*</div>
<div class="wr_input_descriptor">State/Province</div>
<div class="wr_input_element"><input type="text" name="dealer_state" value="<?php echo(val('dealer_state')); ?>" style="border:1px solid <?php echo(is_missing('dealer_state',$missing_fields)?'red;':'black;'); ?>"/>*</div>
<div class="wr_input_descriptor">Date Installed</div>
<div class="wr_input_element"><input type="text" name="date_installed" value="<?php echo(val('date_installed')); ?>" style="border:1px solid <?php echo(is_missing('date_installed',$missing_fields)?'red;':'black;'); ?>"/>*</div>
</div>
<br /><br />
<span class="wr_section_header">My Information</span><br />
<div class="wr_input_descriptor">First Name</div>
<div class="wr_input_element"><input type="text" name="first_name" value="<?php echo(val('first_name')); ?>" style="border:1px solid <?php echo(is_missing('first_name',$missing_fields)?'red;':'black;'); ?>"/>*</div>
<div class="wr_input_descriptor">Last Name</div>
<div class="wr_input_element"><input type="text" name="last_name" value="<?php echo(val('last_name')); ?>" style="border:1px solid <?php echo(is_missing('last_name',$missing_fields)?'red;':'black;'); ?>"/>*</div>
<div class="wr_input_descriptor">Email Address</div>
<div class="wr_input_element"><input type="text" name="email_address" value="<?php echo(val('email_address')); ?>" style="border:1px solid <?php echo(is_missing('email_address',$missing_fields)?'red;':'black;'); ?>"/>*</div>
<div class="wr_input_descriptor">Confirm Email Address</div>
<div class="wr_input_element"><input type="text" name="email_address_2" value="<?php echo(val('email_address_2')); ?>" style="border:1px solid <?php echo(is_missing('email_address_2',$missing_fields)?'red;':'black;'); ?>"/>*</div>
<div class="wr_input_descriptor">Street Address</div>
<div class="wr_input_element"><input type="text" name="cust_address" value="<?php echo(val('cust_address')); ?>" style="border:1px solid <?php echo(is_missing('cust_address',$missing_fields)?'red;':'black;'); ?>"/>*</div>
<div class="wr_input_descriptor">City</div>
<div class="wr_input_element"><input type="text" name="cust_city" value="<?php echo(val('cust_city')); ?>" style="border:1px solid <?php echo(is_missing('cust_city',$missing_fields)?'red;':'black;'); ?>"/>*</div>
<div class="wr_input_descriptor">Province/State</div>
<div class="wr_input_element"><input type="text" name="cust_state" value="<?php echo(val('cust_state')); ?>" style="border:1px solid <?php echo(is_missing('cust_state',$missing_fields)?'red;':'black;'); ?>"/>*</div>
<div class="wr_input_descriptor">Postal Code/ZIP</div>
<div class="wr_input_element"><input type="text" name="cust_postal_code" value="<?php echo(val('cust_postal_code')); ?>" style="border:1px solid <?php echo(is_missing('cust_postal_code',$missing_fields)?'red;':'black;'); ?>"/>*</div>
<div class="wr_input_descriptor">Home Phone</div>
<div class="wr_input_element"><input type="text" name="cust_home_phone" value="<?php echo(val('cust_home_phone')); ?>" style="border:1px solid <?php echo(is_missing('cust_home_phone',$missing_fields)?'red;':'black;'); ?>"/>*</div>
<!--<div class="wr_input_descriptor">Cell Phone</div>
<div class="wr_input_element"><input type="text" name="cust_cell_phone" value="<?php echo(val('cust_cell_phone')); ?>" style="border:1px solid <?php echo(is_missing('cust_cell_phone',$missing_fields)?'red;':'black;'); ?>"/>*</div>-->

<br /><br />
<span class="wr_section_header">My Truck Info</span><br />
<div class="wr_input_descriptor">Year</div>
<div class="wr_input_element"><input type="text" name="year" value="<?php echo(val('year')); ?>" style="border:1px solid <?php echo(is_missing('year',$missing_fields)?'red;':'black;'); ?>"/>*</div>
<div class="wr_input_descriptor">Make</div>
<div class="wr_input_element"><input type="text" name="make" value="<?php echo(val('make')); ?>" style="border:1px solid <?php echo(is_missing('make',$missing_fields)?'red;':'black;'); ?>"/>*</div>
<div class="wr_input_descriptor">Model</div>
<div class="wr_input_element"><input type="text" name="model" value="<?php echo(val('model')); ?>" style="border:1px solid <?php echo(is_missing('model',$missing_fields)?'red;':'black;'); ?>"/>*</div>
<div class="wr_input_descriptor">Complete VIN No.</div>
<div class="wr_input_element"><input type="text" name="vin_number" value="<?php echo(val('vin_number')); ?>" style="border:1px solid <?php echo(is_missing('vin_number',$missing_fields)?'red;':'black;'); ?>"/>*</div>
<br />
<div class="wr_input_descriptor">Engine Serial No.<br />(for engine brakes ONLY)</div>
<div class="wr_input_element"><input type="text" name="engine_serial_number" value="<?php echo(val('engine_serial_number')); ?>" style="border:1px solid <?php echo(is_missing('engine_serial_number',$missing_fields)?'red;':'black;'); ?>"/></div>
<br /><br />
<div class="wr_input_descriptor">Transmission</div>
<div class="wr_input_element">&nbsp;&nbsp;<input type="radio" name="transmission" value="Auto" <?php if(!isset($_POST['transmission']) || (isset($_POST['transmission']) && $_POST['transmission'] == "Auto")) { echo('checked'); } ?>/>Automatic&nbsp;&nbsp;<input type="radio" name="transmission" value="Standard" <?php if (isset($_POST['transmission']) && $_POST['transmission'] == "Standard") { echo('checked'); } ?>/>Standard</div>
<br />
<div class="wr_input_descriptor">Fuel</div>
<div class="wr_input_element">&nbsp;&nbsp;<input type="radio" name="fuel" value="Gas" <?php if(!isset($_POST['fuel']) || (isset($_POST['fuel']) && $_POST['fuel'] == "Gas")) { echo('checked'); } ?>/>Gas&nbsp;&nbsp;<input type="radio" name="fuel" value="Diesel" <?php if (isset($_POST['fuel']) && $_POST['fuel'] == "Diesel") { echo('checked'); } ?>/>Diesel</div>

<br /><br />
<span class="wr_section_header">Existing Aftermarket Products Already Installed</span><br />
<textarea name="aftermarket" rows="5" cols="40"><?php echo(val('aftermarket')); ?></textarea>

<br /><br />
<span class="wr_section_header">What do you tow? (if nothing, enter "nothing")</span><br />
<textarea name="tow" rows="5" cols="40"><?php echo(val('tow')); ?></textarea>

<br /><br />
<span class="wr_section_header">Comments</span><br />
<textarea name="comments" rows="5" cols="40"><?php echo(val('comments')); ?></textarea>

<br /><br />
<input type="checkbox" name="keep_up_to_date" <?php if (isset($_POST['keep_up_to_date'])) { echo("checked"); } ?>/>&nbsp;Yes, please keep me up-to-date on Pacbrake products.

<br /><br /><br />
<div style="width:280px;">
For information on how Pacbrake maintains personal information you provide, view our <a href="index.php?page=terms-conditions">Privacy Policy</a>.
</div>

<br /><br />
<input type="hidden" name="sub" value="sub" />
<input type="submit" value="SUBMIT" />
</form>
<br />
<span>Invoice or Installation receipt may be required for warranty claims.</span>

<?php

}

?>

<?php
$to = "rm@mybluenet.com" ;
$from = $_REQUEST['email_address'] ;
$name = $_REQUEST['last_name'] ;
$headers = "From: Warrany Registration Form";
$subject = "Web Warrany Registration Form - Data";

$fields = array();
$fields{"first_name"} = "first_name";
$fields{"last_name"} = "last_name";
$fields{"email_address"} = "email_address";
$fields{"Phone"} = "Phone";
$fields{"list"} = "Mailing List";
$fields{"Message"} = "Message";

$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

$headers2 = "From: noreply@YourCompany.com";
$subject2 = "Thank you for contacting us";


if($from == '') {print "You have not entered an email, please go back and try again";}
else {
if($last_name == '') {print "You have not entered your Last Name, please go back and try again";}
else {
$send = mail($to, $subject, $body, $headers);
if($send)
{print( "The form has been sent. Thank you!" );}
else
{print "We encountered an error sending your mail, please notify webmaster@YourCompany.com"; }
}
}
?>

Re: php form to write in a database and send email

Posted: Mon Jan 11, 2010 10:15 am
by social_experiment
The mail() part seems to be in order, what problems did you experience when testing the script?