doucai9270 2018-05-19 11:58
浏览 63
已采纳

结账时使用PHP发送电子邮件

So I have been working on a website and I tried to make a functional shopping cart with a checkout system. However, the shopping cart does not allow removal of added items in Mozilla Firefox, on other browsers it works well; I do not know why this is so.

Furthermore, my second problem is that in the checkout.php function, whenever I press the checkout button in the shopping cart page, it does not send the email of order to the business email. Is there a way how I can make this work please? Below I have put all the necessary code. Thank you.

cart.php:

<link rel="stylesheet" href = "styles/styling.css" media="all" />

<?php
   session_start();
   include("adminarea/includes/DBcon.php");
   include ("functions/php1.php");
   include ("header.php");
   require 'obj.php';

    ?>       



<body>

<?php
                    //Fetches information from the database and displays them with the help of obj.php
if(isset($_GET['pID'])){
    $res = mysqli_query($connection, 'select * from product where pID='.$_GET['pID']);
    $prod = mysqli_fetch_object($res);
    $obj = new obj();
    $obj->pID = $prod->pID;
    $obj->pName = $prod->pName;
    $obj->pPrice = $prod->pPrice;
    $obj->qty = 1;

    //to check if products exists in cart or not
    $index = -1;
    $cart = unserialize(serialize($_SESSION['cart']));
        for($i=0;$i<count($cart);$i++)
        if($cart[$i]->pID==$_GET['pID'])
        {
            $index = $i;
            break;
        }

    if($index==-1)
         $_SESSION['cart'][] = $obj;
         else{
             $cart[$index]->qty++;
             $_SESSION['cart']=$cart;
         }
         echo "
         <script> 
         window.open('cart.php','_self')
        </script>
        ";

}


    if(!(isset($_SESSION['cart']))){ 
                            echo "
                            <script>
                            alert('Shopping cart is empty!')
                            </script>
                            ";
                            echo "
                            <script>
                            window.open('products.php','_self')
                            </script>
                            ";
                            }

//if statement to delete the chosen product inside the cart
if(isset($_GET['index']))
{
    $cart = unserialize(serialize($_SESSION['cart']));
    unset ($cart[$_GET['index']]);
    $cart = array_values($cart);
    $_SESSION['cart'] = $cart;
}


?>

<!-- This is to display the shopping cart table-->

<table cellpadding="5" cellspacing="4" border ="9" align="center" width="100%" border="9" bgcolor="darkred">
    <td style="color:#FFF" colspan="10" align="center"><h2><u><i>Shopping Cart:</i></u></h2> 
    <tr>
        <th style="color:#FFF">Option</th>
        <th style="color:#FFF">Id</th>
        <th style="color:#FFF">Name</th>
        <th style="color:#FFF">Price</th>
        <th style="color:#FFF">Quantity</th>
        <th style="color:#FFF">SubTotal</th>
    </tr>
   <?php

   $cart = unserialize(serialize($_SESSION['cart']));
   $s = 0;
   $index = 0;
   for($i=0; $i<count($cart); $i++){
       $s += $cart[$i] ->pPrice * $cart[$i]->qty;

    ?>
    <tr>
    <td>
<div class="shopcart">

        <button style="width:150px; height:50px;"><a href="cart.php?index=<?php echo $index;?>" onClick="return confirm('Please confirm deletion of the chosen product.')">Remove</a></button></td>

        <td style="color:#FFF" align="center"><?php echo $cart[$i] ->pID; ?> </td>
        <td style="color:#FFF" align="center"><?php echo $cart[$i] ->pName; ?></td>
        <td style="color:#FFF" align="center">€<?php echo $cart[$i] ->pPrice; ?></td>
        <td style="color:#FFF" align="center"><?php echo $cart[$i] ->qty; ?></td>
        <td style="color:#FFF" align="center">€<?php echo $cart[$i] ->pPrice * $cart[$i]->qty;?></td>
    </tr>

    <?php }
            $index++;


   ?>
   <tr>
    <td colspan="5" align="right" style="color:#FFF">Total</td>
    <td style="color:#FFF" align="center">€<?php echo $s;?></td>
   </tr>
</table>

<br>
    <a id="a" style="margin-left: 10px;" href="products.php"> Go back</a><br><br>   

    <div id="checkout">

        <form id="checkout" method="post" action="checkout.php"> 
        <input  id="input" type="submit" name="check" value="Checkout" style="background-color:gray; width:200px; margin-right: 10px;">       
    </div>

</div>


<?php include("footer.php") ?>

</body>
</html>

checkout.php:

<?php 
//starting the session
session_start();

    $from = "techologyy@gmail.com"; //from
    $feedback = "Purchase details";


    $to = "techologyy@gmail.com";//direction

    $email = "Email from: $from 
";


    mail("techologyy@gmail.com", $feedback, $from);

    header("Location: index.php"); //returns the user back to homepage

    echo "
            <script>
            alert('The checkout has been done successfully! Thank you')     
            </script>
        ";



    ?>
  • 写回答

1条回答 默认 最新

  • douxue4395 2018-05-19 12:30
    关注

    It looks to me like your mail() call is incorrect

    The order of the parameters should be: to address, subject, email body and then sender. The sender should be in the format From: sender@email.com

    So, adjust your code to something like this:

    $from = "techologyy@gmail.com"; //from
    $feedback = "Purchase details";
    
    $to = "techologyy@gmail.com";//direction
    
    $email = "Email from: $from 
    ";
    
    mail($to, $feedback, $email, "From: $from");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大