Jump to content

Recommended Posts

Hi guys, i have a shopping cart made in php, now it works very well but i have a problem with sending its content to my email. So basicly when a customer chooses all the stuff he wants to buy i need it to send me the cart content to my email.

 

Here si the while loop :

while($row=mysql_fetch_array($query))
								{
									$subtotal=$_SESSION['cart'][$row['product_id']]['quantity']*$row['price'];
									$totalprice+=$subtotal;
									?>
									
                                    	<tr>
                                        	<td><?php echo $row['name'] ?></td>
                                            <td><input type="text" name="quantity[<?php echo $row['product_id']?>]" size="5" value="<?php echo $_SESSION['cart'][$row['product_id']]['quantity'] ?>"/></td>
											<td><?php echo $row['price']?>€</td>
                                            <td><?php echo $_SESSION['cart'][$row['product_id']]['quantity']*$row['price']?>€</td>
                                            
                                            </tr>
                                            <?php
								}

So how could i copy the content of the cart that is created in this while loop and send it to email. I used

<form action="checkout.php" method="post">

<textarea name="message">Full price: <?php echo $totalprice; ?></textarea>

</form>

Which send me a total price but i can't figure out how to send all the bought products with it.

Link to comment
https://forums.phpfreaks.com/topic/292544-sending-a-dynamic-table-to-email/
Share on other sites

Something like this

$tabledata = '';
while($row=mysql_fetch_array($query))
{
    $subtotal=$_SESSION['cart'][$row['product_id']]['quantity']*$row['price'];
    $totalprice+=$subtotal;
    $tabledata .= <<<ROW
        <tr>
            <td>{$row['name']}</td>
            <td><input type="text" name="quantity[{$row['product_id']}]" size="5" value="{$_SESSION['cart'][$row['product_id']]['quantity']}"/></td>
            <td>{$row['price']}€</td>
            <td>$subtotal</td>
        </tr>
ROW;
}

so can you please tell me where to put that while loop you wrote inside my while loop or under it.

<h1 align="center"> Cart </h1>
<form method="post" action="cart.php">

<table align="center">

	<tr>
    	<th>Name</th>
        <th>Quantity</th>
        <th>Price 1</th>
        <th>Price all </th>
     </tr>
     
     <?php
	 
	 	$sql="SELECT * FROM Products WHERE product_id IN (";
						
							foreach($_SESSION['cart'] as $id => $value)
							{
								$sql.=$id.",";
							}
							
								$sql=substr($sql, 0, -1).") ORDER BY name ASC";
								$query=mysql_query($sql);
								$totalprice=0;
								if(!empty($query)){
								while($row=mysql_fetch_array($query))
								{
									$subtotal=$_SESSION['cart'][$row['product_id']]['quantity']*$row['price'];
									$totalprice+=$subtotal;
									?>
									
                                    	<tr>
                                        	<td><?php echo $row['name'] ?></td>
                                            <td><input type="text" name="quantity[<?php echo $row['product_id']?>]" size="5" value="<?php echo $_SESSION['cart'][$row['product_id']]['quantity'] ?>"/></td>
											<td><?php echo $row['price']?>€</td>
                                            <td><?php echo $_SESSION['cart'][$row['product_id']]['quantity']*$row['price']?>€</td>
                                            
                                            </tr>
                                            <?php
								}
								}
								else{
									echo "<p> Your cart is empty </p>";
								}
								?>
                              
       
								<tr>
                                	<td> 
                                     For pay: 
									 <?php echo $totalprice ?> €
                                     
                                    </td>
                                </table>
                                
     
           
          
               <h2 align="center"><br/><button type="submit" name="submit"> Refresh cart</button></h2>
               
                                
                                
                
                
                 </form>
                 </tr>
                 </table>
                 
                 <h3 align="center"><p> To remove from cart, set to 0.</p></h3>
                 

So i fixed it

$Productname=array();
$Quantity=array();
$Allprice=array();

while($row=mysql_fetch_array($query))
								{	
									$subtotal=$_SESSION['cart'][$row['product_id']]['quantity']*$row['price'];
									$totalprice+=$subtotal;
									?>
									
                                    	<tr>
                                        	<td><?php echo $row['name'] ?></td>; 
                                            <td><input type="text" name="quantity[<?php echo $row['product_id']?>]" size="5" value="<?php echo $_SESSION['cart'][$row['product_id']]['quantity'] ?>"/></td>
											<td><?php echo $row['price']?>€</td>
                                            <td><?php echo $_SESSION['cart'][$row['product_id']]['quantity']*$row['price']?>€</td>
                                            
                                            </tr>
                                            
                                            <?php $Allprice[$i]=$_SESSION['cart'][$row['product_id']]['quantity']*$row['price'];?>
                                            <?php $Productname[$i]=$row['name'];?>
                                            <?php $Quantity[$i]=$_SESSION['cart'][$row['product_id']]['quantity']?>
                                            <?php
                                         
    											$i++;			
                                            
								}

but now i have another problem i would like to send them to my email.

 

so i made a form

<form method="post" action="checkout.php">
                  	 
                             <?php echo print_r($Productname,true) ?>
                                <?php echo print_r($Quantity,true) ?>
                                <?php echo print_r($Allprice,true) ?>
                                <input type="submit" value="Buy">
                                </form>

and checkout looks like that

<?php
mail('my_mail@mail.com',$_POST['$Productname'], $_POST['$Quantity'], $_POST['$Allprice']);
?>
<p>Your products where bought.</p>

but it does not work,

 

i tried it with :

<textarea name="message"><?php echo print_r($Productname,true) ?></textarea>

and then i was calling message in my chechout.php,

but the problem is it displays the input window with my array inside it, on my cart.php,

but i don't want that i want,

when user presses buy, it would send me the arrays  without displaying them again on my website. Thank you in advance

Edited by Dorkmind

go back and re-read what Barand first suggested -

 

While you are constructing the table for your page, store the HTML in a variable that can be added to your email message.

 

what this means is to construct the HTML in a php variable (i.e. replace the echo statements with code that adds the html to a php variable.) when you are done constructing the HTML in the php variable, you can do whatever you want with that php variable. you can echo it on your page to display the html to the visitor or you can use it in the email message body to send the html in the email.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.