函数不在循环内调用

时间:2014-06-18 11:06:17

标签: php x-cart

我已经创建了一个PHP脚本,它循环来自ajax请求的POST数据,并将电子邮件发送给相应的收件人。在大多数情况下,脚本可以工作,但是我有两个函数来验证和清理POST数据。

问题是这两个函数没有被调用,实际上当电子邮件实际发送出去时,它们被评估为字符串。我已经尝试了几种不同的方法来使其相应地起作用,包括通过引用传递变量,但无济于事。这里有人可以发现这个问题吗?

        <?php

    error_reporting(E_ALL);

    function validate($value){

        $value = strip_tags($value);
        $value = htmlspecialchars($value);
        $value = stripcslashes($value);

        return $value;
    }

    function detectUrl($string){
        $pattern = "/https?\:\/\/[^\" ]+/i";
        $val = preg_match_all($pattern, $string);

        if($val > 0){
            $replace = preg_replace($pattern, '', $string);
            return $replace;
        } else {
            return $string;
        }
    }

    if(isset($_POST) === true){
        # Include our DB connection
        if (!empty($include_func)) {
            require_once $_module_dir . XC_DS . 'func.php';
        }

        // Current date
        $date = date("d/m/y");

        // Get Form content
        $content = $_POST;

        // Begin message body
        #
        # Prepare message body
        ##
        $message   = array();
        $message[] = '<!DOCTYPE html>';
        $message[] = '<html>';
        $message[] = '<head>
                      <title>Sunglasses.ie Prescription form</title>
                      <style>
                        #wrapper {
                          width: 960px;
                          margin: 0 auto;
                          padding: 0 20px;
                        }
                        h1 {
                          text-align: center;
                        }
                        h3 {
                          text-align: center;
                          background: #eee;
                          border-top: 1px solid #000;
                          border-bottom: 1px solid #000;
                          padding: 5px 0;
                        }
                        table {
                          width: 100%;
                          margin-bottom: 20px;
                        }
                        table thead th {
                          border-bottom: 2px solid #ddd;
                          text-align: left;
                          vertical-align: bottom;
                        }
                        table tbody td {
                          border-bottom: 1px solid #ddd;
                          vertical-align: top;
                        }
                        table tfoot td {
                          border-top: 2px solid #000;
                          vertical-align: bottom;
                        }
                      </style>
                      </head>';
        $message[] = '<body>';
        $message[] = '<div id="wrapper">';
        $message[] = '<img src="test.jpg" alt="Sunglasses.ie">';
        $message[] = "<h1>A new Prescription has been generated: {$date}</h1>";
        $message[] = '<table>';
        $message[] = '<tbody>';


    foreach($content as $key => $value){
        $message[] = '<tr>';
        $message[] = "<td style='padding:8px;font-weight:bold;text-transform:uppercase;'>{$key}</td>";
        if(empty($value) || $value === null){
            $message[] = "<td style='text-transform:uppercase;'>0</td>";
        } else {
            $message[] = "<td style='text-transform:uppercase;'>{$value}</td>";
        }
        $message[] = '</tr>';
    }



    $stop = 5*ceil(((count($content)-3)/2)/5); # get array length to nearest multiple of 5


    /** Single POST Array **/
    for($row=3; $row<$stop+3; $row+=5){ // count $_POST entries
        $message[] = '<tr>';
        for($col=0; $col<5; $col++){ // simple column count
            if($col==2) {
                $message[] = "<td>&nbsp;</td>";
            } else if($col==0){
                if(!empty($_POST[$row])){
                    $message[] = "<td style='padding:8px;font-weight:bold;text-transform:uppercase;'>".$_POST[$row]."</td>";
                } else {
                    $message[] = "<td style='padding:8px;font-weight:bold;text-transform:uppercase;'>&nbsp;</td>";
                }
            } else if($col==3){
                if(!empty($_POST[$row+3])){
                    $message[] = "<td style='padding:8px;font-weight:bold;text-transform:uppercase;'>".$_POST[$row+3]."</td>";
                } else {
                    $message[] = "<td style='padding:8px;font-weight:bold;text-transform:uppercase;'>&nbsp;</td>";
                }
            } else if($j==1){
                if (!empty($_POST[$row+1])){
                    $message[] = "<td style='text-transform:uppercase;'>".detectUrl(validate($_POST[$row+1]))."</td>";
                } else {
                    $message[] = "<td style='text-transform:uppercase;'>&nbsp;</td>";
                }
            } else if($j==4){
                if (!empty($_POST[$row+4])){
                    $message[] = "<td style='text-transform:uppercase;'>".detectUrl(validate($_POST[$row+3]))."</td>";
                } else {
                    $message[] = "<td style='text-transform:uppercase;'>&nbsp;</td>";
                }
            }
        }
        $message[] = '</tr>';
    }


        $message[] = '</tbody>';
        $message[] = '</table>';

        #
        # Prepare email headers
        ##
        $to        = 'me@me.ie';
        $subject   = 'A new Prescription form has been generated';
        $headers   = array();
        $headers[] = "To: {$to}";
        $headers[] = 'From: Prescriptions ';
        $headers[] = 'Reply-To: Prescriptions - ';
        $headers[] =  "Subject: {$subject}";
        $headers[] = 'X-Mailer: PHP/'.phpversion();
        $headers[] = 'MIME-Version: 1.0';
        $headers[] = 'Content-type: text/html; charset=iso-8859-1';

        if(!empty($content)){
            #
            # Print message on the screen
            ##
            print implode("\r\n", $message);


            #
            # Send email to intended recipients
            ##
            mail('', $subject, implode("\r\n", $message), implode("\r\n", $headers));
            echo 'Email Sent!';
            echo '<pre>';
            print_r($_POST);
            echo '</pre>';
        } else {
            echo 'Mail not sent';
        }

        } else {        

            echo '<br>';
            echo 'Nah';
    }

此脚本的结果产生:

enter image description here

0 个答案:

没有答案