2013-03-06 96 views
0

我做網上商店和我卡住了,當我需要讓我的購物車上的郵件,我不知道如何調用函數購物車上得到它的郵件或我做錯了函數得到它郵件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 = "[email protected]"; 
    $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._%-][email protected][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 
} 
?> 
+0

刪除'@ mail'上的'@',因爲它消除了您將看到的錯誤。另外,在附註中,你應該拼出單詞'你'而不是'U'。 – SeanWM 2013-03-06 01:36:11

+0

在'cart1'函數中,而不是'echo「Total ...」',你可以'返回「Total ...」'而不是? – Th3Cuber 2013-03-06 01:36:15

回答

0

[1] cart1()不是荷蘭國際集團的任何值。除了echo之外,還要創建一個變量來保存/連接返回的字符串,使用:return $ str_out;

[2]推薦您使用PHP的新的mysqli協議(預處理語句)http://php.net/manual/en/mysqli.prepare.php

[3]建議你結合的錯誤處理代碼以捕獲異常。