Forum Moderators: coopster

Message Too Old, No Replies

Sending query results in email form

How to implement a query function in form and send email

         

pixeldiver

10:08 pm on Nov 23, 2009 (gmt 0)

10+ Year Member



Hi,

I need help with a problem to send a form to email.
I have a function called showCart(). This function pulls data from the database and displays it in a table within a form.

The form has further data like email address, company name, name to collect from the user. When the user clicks the request button, the form does get send to a predefined email address.

How do I include the function showCart() within the form, so that the additional information is sent as well?

Any ideas?

Thanks

//The form:

<form action="quote_request_stec.php" method="post">
<table width="968" height="179" border="1" cellpadding="3" cellspacing="3" id="schedule_table">
<tr>
<td height="45" colspan="2" align="center">PROJECT NAME</td>
<td colspan="2" align="center">COMPANY</td>
<td align="center">CONTACT NAME</td>
<td align="center">CONTACT EMAIL</td>
<td align="center">PRINT</td>
</tr>
<tr>
<td colspan="2" align="center"><input name="Project" type="text" id="project" size="30" maxlength="50" /></td>
<td colspan="2" align="center"><input name="Company" type="text" id="company" size="30" maxlength="50" /></td>
<td align="center"><input name="Name" type="text" id="name" size="20" maxlength="30" /></td>
<td align="center"><input name="Email" type="text" id="email" size="30" maxlength="50" /></td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td colspan="7" align="center">&nbsp;</td>
</tr>
<tr>
<td width="90" height="23" align="center">Quantity</td>
<td width="84" align="center">Model</td>
<td width="111" align="center">Minimum Airflow</td>
<td width="110" align="center">Maximum Airflow</td>
<td width="186" align="center">&nbsp;</td>
<td width="183" align="center">&nbsp;</td>
<td width="227" align="center">&nbsp;</td>
</tr>
<tr>
<td height="34" colspan="7"><?php echo showCart(); ?></td>
</tr>
</table>

<input type="submit" name="request" id="request" value="Request Quote" /></form>

//The function to query the database:

function showCart() {

$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}

$output[] = '<table width="800">';

foreach ($contents as $id=>$qty) {
$query = 'SELECT * FROM base WHERE id = '.$id;
$result = mysql_query($query) or die(mysql_error());

$row = mysql_fetch_assoc($result); {
extract($row);

$output[] = '<tr>';
$output[] = '<td align="left" width="70"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td align="left" width="70">'.$model.'</td>';
$output[] = '<td align="left" width="110">'.$boundary_minair.'</td>';
$output[] = '<td align="left" width="110">'.$boundary_maxair.'</td>';
$output[] = '<td align="left" width="242"><a href="slimtec-schedule.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
}

}} else {
$output[] = '<p>Your project schedule is empty.</p>';
}
return join('',$output);
}
?>

rocknbil

10:37 pm on Nov 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One way might be

$response = showCart();

// Now you have the output of the item
// stored in $response.

$body = <html><head>Confirmation</head><body>' .
'<p>Dear ' . $fname . ',</p><p>here is a receipt of your submission.</p>' .
'<table width="90%" style="margin:auto;">' .
$response . '</table></body></html>';

//Then create a content-type: text/html for the email
// header, use $body as the mail content.

$headers = "From: $from\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $email_subj, $body, $headers);

If there are some things you want in the response, but not in the email, you can alter the showCart to return an array of two outputs, one for email, one for response.

function showCart() {
$both = 'stuff for both, response and emails';
//..........
$response_only = $both . 'Stuff for response only';
$email_only = $both . 'Stuff for email only';

return ($response_only,$email_only);
}

Then

list ($response,$email_body) = showCart();

Then email, send response to browser.

Another method is to call showCart twice, and pass it a parameter. A little inefficient, but may make for more readable code.

$response = showCart();
$body = showCart(1);

function showCart($isEmail) {
$output = 'Stuff for both output and response';
if ($isEmail) { $output .= 'Stuff for email only.'; }
$output = 'More stuff for both output and response';
if ($isEmail) { $output .= 'Even more stuff for email only.'; }
else { $output .= 'Stuff for response only'; }
// Note that the first if will not affect anything
// between itself and the next if, it will go in both
// emails. But the second if and it's else makes
// the stuff in the else go into response only.
$output = 'More stuff for both output and response';
return $output;
}

pixeldiver

12:35 am on Nov 24, 2009 (gmt 0)

10+ Year Member



Thanks rocknbil,

I am trying to integrate your code. And it makes no sense to me (I am still new to the php world).

Could you please specify where I put $response = showCart();
And how I retrieve the $ response in the email.
I also have the following script to handle the emails:

<?php
$to = "test@test.com" ;
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;

$subject = "QUOTE REQUEST";
$headers = "MIME-Version: 1.0\r\n";
$headers.= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers.= "From: $from\r\n";

$fields = array();
$fields{"Name"} = "Name";
$fields{"Company"} = "Company";
$fields{"Email"} = "Email";
$fields{"Phone"} = "Tel.";
$fields{"Project"} = "Project Name";
$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";
$autoreply = "Thank you for your enquiry, we will contact you";

if($from == '') {print "You have not entered an email, please go back and try again";}
else {
if($name == '') {print "You have not entered a name, please go back and try again";}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{header( "Location: [not_shown_here.php"...] );}
else
{print "We encountered an error sending your mail, please notify webmaster@YourCompany.com"; }
}
}
?>

pixeldiver

1:18 am on Nov 24, 2009 (gmt 0)

10+ Year Member



Thanks again rocknbil,

I have worked it out, I got it all working, you put me on the right path.

I had to get a third coffee and that refreshed the brain cells!