我对使用单引号和双引号,echo和$ message感到困惑。=
我无法弄清楚如何在$ message中编写foreach循环来输出购物车项目。 thead部分有效,但tbody区域有缺陷。任何帮助表示赞赏。
<?php
session_start();
// Initialize variables
$name = $telephone = $email = $jim_gmail = '';
// Post data from @quote-form.html.php
if( $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'] ) ) {
// Sanitize and post data to variables
$company = sanitize( $_POST['company'] );
$name = sanitize( $_POST['name'] );
$telephone = sanitize( $_POST['telephone'] );
$email = sanitize( $_POST['email'] );
// Assign $name value to SESSION variable for use @thankyou.html.php
$_SESSION['name'] = $name;
// Initialize and declare variables for script validation
$errMsg = '';
$telephone_pattern = '/^((([0-9]{1})*[- .(]*([0-9]{3})[- .)]*[0-9]{3}[- .]*[0-9]{4})+)*$/';
$alpha_only_pattern = '/^[a-zA-Z]*$/';
$email_pattern = '/^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})*$/';
$integers_pattern = '/^[0-9]*$/';
// Validate user data before submitting to server
if(empty($name) || empty($telephone) || empty($email))
{
$errMsg = "*Name, telephone and email address required.<br>";
include 'error.html.php';
}
elseif (preg_match($email_pattern,$email) === 0 )
{
$errMsg = "*Please enter a valid email address.<br>";
include 'error.html.php';
}
else
{
/* Prepare message for e-mail */
/* set e-mail recipient */
$jim_gmail = 'jim@gmail.com';
// Three required arguments ($to, $subject, $message)
$to = "$jim_gmail";
$subject = "Buyer for CraneHeli";
$from = "$email";
$message = // contents of report in $message
"
<html>
<head></head>
<body>
<h3>Parts Buyer</h3>
<p>Company: $company</p>
<p>Name: $name</p>
<p>Telephone: $telephone</p>
<p>Email: $email</p>
<h3>Please quote the following:</h3>
<table name='contact_seller' style='border-collapse:collapse';>
<thead>
<tr>
<th>ID</th>
<th>Part Number</th>
<th>Description</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<?php foreach($cart as $item): ?>
<tr>
<td>$item['id']</td>
<td>$item['part_number']</td>
<td>$item['description']</td>
<td>$item['quantity']</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<p>End of buyer data report</p>
<hr />
</body>
</html>
"; //end of $message
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n"; // code to send HTML on UNIX
$headers .= 'Content-type:text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Bcc: ' . $jim_gmail . "\r\n"; // works
$headers .= 'Bcc: ' . $jim_gmail . "\r\n"; // works
// Send message using mail() function
mail($to, $subject, $message, $headers);
// Check to see if headers not sent. If true, redirect to thank_you.php page
if(!headers_sent()){
header('Location: thankyou.html.php');
exit();
}else{
echo "<span class='errMsg'>Message sent successfully!</span><br><br>" .
"Cannot redirect, please click this <a " .
"href=\".\">link</a> instead\n";
}
exit();
}
/******* Functions used *******/
function sanitize($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
答案 0 :(得分:2)
你快到了:
$message = // contents of report in $message
"
<html>
<head></head>
<body>
<h3>Parts Buyer</h3>
<p>Company: $company</p>
<p>Name: $name</p>
<p>Telephone: $telephone</p>
<p>Email: $email</p>
<h3>Please quote the following:</h3>
<table name='contact_seller' style='border-collapse:collapse';>
<thead>
<tr>
<th>ID</th>
<th>Part Number</th>
<th>Description</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>";
foreach($cart as $item) {
$message .="<tr>
<td>" . $item['id'] ."</td>
<td>".$item['part_number']."</td>
<td>".$item['description']."</td>
<td>".$item['quantity']."</td>
</tr>";
}
$message .= "</tbody>
</table>
<p>End of buyer data report</p>
<hr />
</body>
</html>"; //end of $message
特别注意:
<tbody>";
foreach($cart as $item) {
$message .="<tr>
<td>" . $item['id'] ."</td>
<td>".$item['part_number']."</td>
<td>".$item['description']."</td>
<td>".$item['quantity']."</td>
</tr>";
}
$message .= "</tbody>
</table>