-1

How can you pass dynamic details which are retrieved from a database (e.g. details in a shopping cart table) from one page to another page so that you could send them via email using the mail() function?I tried many ways using the "$message" but none worked. I am new to PHP and do not have much experience with it yet. Any help would be appreciated, thank you.

page 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>

page 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

    ?>
Redent
  • 33
  • 7
  • 1
    Quick googling turned up this other question which seems eerily similar to what you're asking - https://stackoverflow.com/questions/871858/php-pass-variable-to-next-page. Take a look at the accepted answer for some mechanisms for transferring information from one page to another. – janoulle May 19 '18 at 23:02

1 Answers1

0

Please specify what you have tried so far.
you want to send the data from one page to another, try using form tag in html and set the method attribute to post and action attribute to where you want to send the data.
The html code will look like this


<form action="post" action="getdata.php">  <input id="val" type="text" value="" name="cart" style="display:none;">  <button type="submit" >hit me</button> </form>

You can set the value of input using javascript


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

getdata.php will look like this


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

be sure to validate the data and check for any hidden code before executing the user input