Olá, Queria meter no meu site um carrinho para produtos, mas só para pedidos de orçamento, e não para venda online. Criei o carro e está a funcionar direito, porém, quando faço o envio de orçamento só me envia o ultimo produto adicionado. Será que alguém me pode dar umas luzes? Obrigado. <?php session_start();
$servidor = 'localhost';
$usuario = 'user';
$senha = 'pass';
$db = 'db';
$con = mysql_connect($servidor, $usuario, $senha) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
?>
<?php
$product_id = $_GET[id];
$action = $_GET[action];
if($product_id && !productExists($product_id)) {
die("Error. O Produto Não Existe!");
}
switch($action) {
case "add":
$_SESSION['cart'][$product_id]++;
break;
case "remove":
$_SESSION['cart'][$product_id]--;
if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]);
break;
case "empty":
unset($_SESSION['cart']);
break;
}
?>
<div id="form_contact">
<form method="post" action="orcamento.php" border="0">
<?php
$ipi = getenv("REMOTE_ADDR");
$httprefi = getenv ("HTTP_REFERER");
$httpagenti = getenv ("HTTP_USER_AGENT");
?>
<fieldset>
<legend class="texte_legende">as suas informações </legend>
<input type="hidden" name="ip" value="<?php echo $ipi ?>" />
<input type="hidden" name="httpref" value="<?php echo $httprefi ?>"
/>
<input type="hidden" name="httpagent" value="<?php echo $httpagenti
?>" />
<table cellpadding=5 cellspacing=0 border="0">
<tr>
<td class="texte">Nome completo :</td>
<td><input class="champ" type="text" name="name" id="name"></td>
</tr>
<tr>
<td class="texte">Email :</td>
<td><input class="champ" type="text" name="email" id="email" ><
/td>
</tr>
<tr>
<td class="texte">Telefone :</td>
<td><input class="champ" type="text" name="tlf" id="tlf" ></td>
</tr>
</table>
</fieldset>
<br>
<table border="0" padding="0" width="100%" id="mytable">
<caption>Pedido de Orçamento</caption>
<tr>
<th width="10" class="nobg" scope="col" abbr="
Configurations"></th>
<th width="100" scope="col" abbr="Referência">Referência</
th>
<th width="202" scope="col" abbr="Descrição">Descrição</th>
<th width="15" scope="col" abbr="Qtd">Qtd</th>
</tr>
<?php
if($_SESSION['cart']) {
foreach($_SESSION['cart'] as $product_id => $quantity) {
$sql = sprintf("SELECT ref, descricao FROM produtos WHERE id = %d;",
$product_id);
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
list($ref, $descricao) = mysql_fetch_row($result);
$line_cost = $quantity;
$total = $total + $line_cost;
echo "<tr>";
echo "<th scope=\"row\" abbr=\"Apagar\" class=\"spec\"><a href=\"
$_SERVER[PHP_SELF]?action=remove&id=$product_id\">x</th>";
echo "<td>$ref</td>";
echo "<td>$descricao</td>";
echo "<td class=\"input\" align=\"center\"><input type=\"text\" id=\"qtd\"
name=\"qtd\" maxlength=\"3\" value=\"$line_cost\" size=\"1\" /></td>";
echo "</tr>";
}
}
echo "<tr>";
echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\"
onclick=\"return confirm('Tem certeza?');\">Esvaziar Carrinho</a></td>";
echo "<td align=\"right\"><input class=\"button_send\" name=\"enviar\" type=\"
submit\" value=\"Enviar\"></td>";
echo "</tr>";
}else{
echo "<script>
alert('não existem produtos no carrinho.');</script>";
}
function productExists($product_id) {
$sql = sprintf("SELECT * FROM produtos WHERE id = %d;", $product_id);
return mysql_num_rows(mysql_query($sql)) > 0;
}
?>
</table>
<input type="hidden" id="qtd" name="qtd" value="<?php echo $qtd ?>" />
<input type="hidden" id="rf" name="rf" value="<?php echo $ref ?>" />
<input type="hidden" id="des" name="des" value="<?php echo $descricao ?>" />
</form>
</div>
<div id="footer">
<strong><em><font size="-2">Copyright © 2011</font></em></strong>
</div>
<?
$product_ref = $_POST['rf'];
$product_descricao = $_POST['des'];
$qtd = $_POST['qtd'];
$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];
$name = ($_POST['name']);
$email = ($_POST['email']);
$tlf = ($_POST['tlf']);
$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
$erro .= (empty($name)) ? 'Nome não inserido!\n' : NULL;
$erro .= (empty($email)) ? 'Email não inserido!\n' : NULL;
$erro .= (empty($tlf)) ? 'Contacto não inserido!\n' : NULL;
$erro .= (!eregi($email_exp,$email)) ? 'Email Inválido!\n' : NULL;
$msg = 'Ocorreram os seguintes erros:\n\n';
if(empty($erro)) {
$your_email= "dominio@dominio.pt";
$subject= "Pedido de Orcamento";
$headers= "From: ".$_POST['name']." <".$_POST['email'].">\r\n";
$headers.='Content-type: text/html; charset=iso-8859-1';
mail($your_email, $subject, "
<html>
<head>
<title>Pedido de Orçamento</title>
</head>
<body>
<br>
Nome : ".$_POST['name']."<br>
Email : ".$_POST['email']."<br>
Telefone : ".$_POST['tlf']."<br><br>
Assunto : ".$subject."<br><br>
IP:".$_POST['ip']."<br>
Browser:".$_POST['httpagent']."<br>
Reffer:".$_POST['httpref']."<br><br /><br/><br />
".$product_ref."
".$product_descricao."
".$qtd."
</body>
</html>", $headers);
echo "<script>alert('Mensagem enviada com sucesso!\\nAgradecemos desde já o seu contacto.\
\nResponderemos tão brevemente quanto possivel.');</script>";
echo "<script>history.back();</script>";
}else{
echo "<script>alert('".$msg.$erro."');
history.back();</script>";
}
?>