【问题标题】:How do you "transfer" a dynamic value from one php page to another php page?如何将动态值从一个 php 页面“转移”到另一个 php 页面?
【发布时间】:2018-10-29 23:29:46
【问题描述】:

您如何将从数据库中检索到的动态详细信息(例如购物车表中的详细信息)从一个页面传递到另一个页面,以便您可以使用 mail() 函数通过电子邮件发送它们?我尝试了多种使用方法“$message”,但没有奏效。我是 PHP 新手,还没有太多经验。任何帮助将不胜感激,谢谢。

第 1 页:

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

    ?>       

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

<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>
        ";


$_SESSION['pID'] = $_POST['pID'];
$_SESSION['pName'] = $_POST['pName'];
$_SESSION['pPrice'] = $_POST['pPrice'];
$_SESSION['qty'] = $_POST['qty'];

}


    if(!(isset($_SESSION['cart']))){ 
                            echo "
                            <script>
                            alert('Shopping cart is empty!')
                            window.location.href='index.php';   
                            </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">

        <input id="input" type="submit" name="ctable"/><a href="cart.php?index=<?php echo $index;?>" onClick="return confirm('Please confirm deletion of the chosen product.')">Remove</a></input></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>

第 2 页:

<?php session_start();

require 'obj.php';
include("adminarea/includes/DBcon.php");


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

    $subject = "Purchase Details:";
    $message = $_SESSION['pID'];
    $message .= $_SESSION['pName']."\r\n";
    $message .= $_SESSION['pPrice']."\r\n";
    $message .= $_SESSION['qty']."\r\n";
    $headers = 'From: techologyy@gmail.com' . "\r\n"; //from

    //mail paramter with correct order
    mail($to, $subject, $message, $headers);


    //echo to display alert
        echo "
            <script>
            alert('The checkout has been done successfully! Thank you')
            window.location.href='index.php';   
            </script>
        "; //returns the user back to homepage

    ?>

【问题讨论】:

  • 快速谷歌搜索发现了另一个问题,这似乎与您所问的问题非常相似 - stackoverflow.com/questions/871858/…。看看一些将信息从一个页面传输到另一个页面的机制的公认答案。

标签: php session phpmyadmin xampp session-variables


【解决方案1】:

请说明您目前尝试过的内容。
您想将数据从一个页面发送到另一个页面,请尝试在 html 中使用表单标签 并将 method 属性设置为 post 并将 action 属性设置为您要发送数据的位置。
html 代码如下所示

您可以使用javascript设置输入值

document.getElementById("val").value="YourValue";

getdata.php 看起来像这样

> if ($_SERVER["REQUEST_METHOD"] == "POST"){ > $cartval=$_POST['cart']; > 回声 $cartval; }

确保在执行用户输入之前验证数据并检查是否有任何隐藏代码

【讨论】:

  • 感谢您的回复,我按照您的要求在问题中添加了代码。
  • @Redent 你想从第一页和第二页发送数据?
  • 是的,这是我的目标,然后将这些数据打印到电子邮件中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-17
  • 2012-04-27
  • 2012-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多