问题描述
我的联系表格有问题,需要代码方面的帮助。
这是“购物车”的图像,用户可以从表中添加/删除项目。 一旦他们选择了他们想要的所有产品,在本例中,我添加了2个项目,然后他们单击“订购表单”按钮,并在左侧弹出一个显示两个项目的表单。
我的问题是,当电子邮件通过时,它仅显示最后一个项目,而不显示表单中的两个项目,这就是电子邮件中的样子
我如何做到这一点,以便它可以将两个项目或所有项目从左侧的表单中拉到我的电子邮件正文中。 下面是我的代码,如果有人可以浏览并建议我必须更改或我做错了。
<section class="order">
<?PHP
$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
$sql = $con->prepare("SELECT * FROM products WHERE product_id = ?");
$sql->bind_param("i",$id);
$sql->execute();
$result = $sql->get_result();
$rows = $result->num_rows;
{
while($row = $result->fetch_assoc())
{
?>
<div class="product-info">
<form method="post" action="cart.PHP?action=add&id=<?PHP echo $row["product_id"]; ?>">
<img src="images/<?PHP echo $row["image"]; ?>" class="image" /><br /><br>
<h3 class="name"><?PHP echo $row["productname"]; ?></h3><br>
<h5 class="desc"><?PHP echo $row["description"]; ?></h5><br>
<h4 class="code"><?PHP echo $row["code"]; ?></h4><br>
<input type="text" name="quantity" value="1" class="form-control" /><br>
<input type="hidden" name="hidden_name" value="<?PHP echo $row["productname"]; ?>" />
<input type="hidden" name="hidden_description" value="<?PHP echo $row["description"]; ?>" />
<input type="hidden" name="hidden_code" value="<?PHP echo $row["code"]; ?>" />
<input type="submit" name="add_to_cart" style="margin-top:5px;" class="addtocart" value="Add to Cart" />
</form>
</div>
<?PHP
}
}
?>
<?PHP
if(isset($_POST["add_to_cart"]))
{
if(isset($_SESSION["shopping_cart"]))
{
$item_array_id = array_column($_SESSION["shopping_cart"],"item_id");
if(!in_array($_GET["id"],$item_array_id))
{
$count = count($_SESSION["shopping_cart"]);
$item_array = array(
'item_id' => $_GET["id"],'item_name' => $_POST["hidden_name"],'item_description' => $_POST["hidden_description"],'item_code' => $_POST["hidden_code"],'item_quantity' => $_POST["quantity"]);
$_SESSION["shopping_cart"][$count] = $item_array;
}
else
{
echo ("<br><br><div class='alert alert-danger'>Item already added.</div>");
}
}
else
{
$item_array = array(
'item_id' => $_GET["id"],'item_quantity' => $_POST["quantity"]);
$_SESSION["shopping_cart"][0] = $item_array;
}
}
if(isset($_GET["action"]))
{
if($_GET["action"] == "delete")
{
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
if($values["item_id"] == $_GET["id"])
{
unset($_SESSION["shopping_cart"][$keys]);
echo ("<br><br><div class='alert alert-success'>Successfully Removed Item</div>");
}
}
}
}
?>
<br />
<h2>Order Details</h2>
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th width="35%">Item Name</th>
<th width="45%">Description</th>
<th width="18%">Code</th>
<th width="2%">Qty</th>
<th width="5%">Action</th>
</tr>
<?PHP
if(!empty($_SESSION["shopping_cart"]))
{
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
?>
<tr>
<td><?PHP echo $values["item_name"]; ?></td>
<td><?PHP echo $values["item_description"]; ?></td>
<td><?PHP echo $values["item_code"]; ?></td>
<td><?PHP echo $values["item_quantity"]; ?></td>
<td><a href="cart.PHP?action=delete&id=<?PHP echo $values["item_id"];?>"><span class="text-danger">Remove</span></a></td>
</tr>
<?PHP
}
?>
<?PHP
}
?>
</table>
</div>
<td><button class="open-button" onclick="openForm()">Open Form</button></td>
</section>
<section>
<div class="form-popup" id="myForm">
<form method="post" action="cart.PHP">
<input type="text" name="name" placeholder="Name"><br><br>
<input type="text" name="mail" placeholder="Your e-mail"><br><br>
<input type="text" name="number" placeholder="Your contact number"><br><br>
<input type="text" name="subject" placeholder="Subject"><br><br>
<h2>Order Details</h2>
<?PHP
if(!empty($_SESSION["shopping_cart"]))
{
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
?>
<tr>
<td>Item Name:<input type="text" name="productname" value="<?PHP echo $values["item_name"]; ?>"></td><br>
<td>Description:<input type="text" name="description" value="<?PHP echo $values["item_description"]; ?>"></td><br>
<td>Code:<input type="text" name="code" value="<?PHP echo $values["item_code"]; ?>"></td><br>
<td>Qty:<input type="text" name="qty" value="<?PHP echo $values["item_quantity"]; ?>"></td><br><br>
</tr>
<?PHP
}
?>
<?PHP
}
?>
<button class="button" type="submit" name="submit">SEND</button><br>
<?PHP
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$number = $_POST['number'];
$product = $_POST['productname'];
$description = $_POST['description'];
$code = $_POST['code'];
$qty = $_POST['qty'];
$mailTo = "[email protected]";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from ".$name.".\n\n"."Email address: ".$mailFrom.".\n\n"."Contact number: ".$number.".\n\n"."Order Details:\n\n".$product.".\n\n"."Description:".$description.".\n\n"."Product Code:".$code.".\n\n"."Quantity Ordered:".$qty;
mail($mailTo,$subject,$txt,$headers);
header("Location: cart.PHP?mailsend");
}
?>
</form>
</div>
</section>
您的帮助将不胜感激:)
解决方法
问题出在这里:
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$number = $_POST['number'];
$product = $_POST['productname'];
$description = $_POST['description'];
$code = $_POST['code'];
$qty = $_POST['qty'];
$mailTo = "[email protected]";
$headers = "From: " . $mailFrom;
$txt = "You have received an e-mail from " . $name . ".\n\n" . "Email address: " . $mailFrom . ".\n\n" . "Contact number: " . $number . ".\n\n" . "Order Details:\n\n" . $product . ".\n\n" . "Description:" . $description . ".\n\n" . "Product Code:" . $code . ".\n\n" . "Quantity Ordered:" . $qty;
mail($mailTo,$subject,$txt,$headers);
header("Location: cart.php?mailsend");
}
$txt
变量负责电子邮件中的文本,您正在将其值设置为通过表单发布的最后订单。您必须连接每个订单。因此,您必须执行类似$txt .= "You...."
的操作,每次都使用(。)运算符连接产品详细信息