PHP购物车邮件功能

时间:2013-03-06 01:33:04

标签: php

我做网上商店,当我需要通过邮件购买我的购物车时,我不知道如何调用功能推车来获取邮件或我做错了功能以便通过邮件获取

function cart1(){
    foreach($_SESSION as $name => $value){
        if ($value>0){
            if (substr($name, 0,5)=='cart_'){
                $id =substr($name, 5, (strlen($name)-5)); 
                $get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string($id));
                //racuna kolko kosta
                while($get_row = mysql_fetch_assoc($get)){
                    $sub = $get_row['price']*$value;
                    echo $get_row{'name'}.' x '.$value.' Kom'.' = '.number_format($sub,2). ' kn';
                }
            }
            //zbraje 1 i 2i ... proizvod
            echo "<br />";


            $total += $sub;
            //echo "<hr width='150'>";
        }
    }
    if ($total==0){
    echo "Cart is empty.";
    }
    else{
    echo 'Total: '.number_format($total, 2).' kn';
    }
}

和邮件功能,我收集客户信息,但我不知道如何调用功能

<?php
require 'cart.php';
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "aaaa@hotmail.com";
    $email_subject = "Your email subject line";


    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Molimo Vas da upisete sva polja koja su oznacena *.<br /><br />";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['name']) ||
        !isset($_POST['adress']) ||
        !isset($_POST['post']) ||
        !isset($_POST['city']) ||
        !isset($_POST['email']) ||
        !isset($_POST['tel']) ||
        !isset($_POST['comments']))  {
        died('We are sorry, but there appears to be a problem with the form you submitted.');      
    }

    $name = $_POST['name']; // required
    $adress = $_POST['adress']; // required
    $post = $_POST['post']; // not required
    $city = $_POST['city']; // required
    $email = $_POST['email'];
    $tel = $_POST['tel'];
    $comments = $_POST['comments'];
    $cart1 .= cart1();


    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email)) {
    $error_message .= 'U didnt write email..<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$name)) {
    $error_message .= 'Niste upisali Ime i Prezime.<br />';
  }
  if(!preg_match($string_exp,$oib)) {
    $error_message .= 'Niste upisali OIB.<br />';
  }
  if(!preg_match($string_exp,$adress)) {
    $error_message .= 'U didnt write adress.<br />';
  }
  if(!preg_match($string_exp,$post)) {
    $error_message .= 'U didnt write Post number.<br />';
  }
  if(!preg_match($string_exp,$city)) {
    $error_message .= 'U didnt write city.<br />';
  }
  if(!preg_match($string_exp,$tel)) {
    $error_message .= 'U didnt write Tel.<br />';
  }

  if(strlen($comments) < 2) {
    $error_message .= 'U didnt write comment.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "First name/lastname: ".clean_string($name)."\n";
    $email_message .= "Adres: ".clean_string($adress)."\n";
    $email_message .= "City: ".clean_string($city)."\n";
    $email_message .= "Post: ".clean_string($post)."\n";
    $email_message .= "Email: ".clean_string($email)."\n";
    $email_message .= "Tel: ".clean_string($tel)."\n";
    $email_message .= "Fax: ".clean_string($fax)."\n";
    $email_message .= "Comment: ".clean_string($comments)."\n";
    $email_message =  cart1();




// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
?>

<!-- include your own success html here -->

Tenx to buying here!<br />
<?php
}
?>

1 个答案:

答案 0 :(得分:0)

[1] cart1()不是返回任何值。而不是echo,创建一个变量来保存/连接返回的字符串,使用:return $ str_out;

[2]建议您使用新的mysqli协议进行php(使用预准备语句)http://php.net/manual/en/mysqli.prepare.php

[3]建议您在代码中加入错误处理程序以捕获异常。