【问题标题】:syntax error, unexpected T_FUNCTION, expecting ')'语法错误,意外的 T_FUNCTION,需要 ')'
【发布时间】:2014-02-09 04:54:55
【问题描述】:

您好,我已经购买了主题,但我认为联系表单提交中存在错误。主题使用ajax提交联系表单。

联系表格链接http://192.185.83.133/~lbrisas/TRY/ibrisas/contact.html

当您填写完整的表单并提交时,我将使用 jquery ajax 将表单提交到contact.php 文件,这里是contact.php 的代码

代码

<?php

    spl_autoload_register(function($class_name){
        $file_name = str_replace('\\', '/', $class_name);
        $file_name = str_replace('_', '/', $file_name);
        $file = dirname(__FILE__) . "/lib/$file_name.php";
        if(is_file($file)) include $file;
    });

    use HybridLogic\Validation\Validator;
    use HybridLogic\Validation\Rule;

    $CONFIG = array(
        /* Mail Options */
        'mail_send_to'      =>  'vishalnthoriya@gmail.com', 
        'mail_contents'     =>  'mail-content.php', 
        'mail_failed_msg'   =>  'An unknown error has occured', 

        /* Notification Messages */
        'form_errors_msg'   =>  '<h4>The following errors were encountered</h4><ul><li>%s</li></ul>', 
        'form_invalid_msg'  =>  'The form is invalid', 
        'form_success_msg'  =>  '<h4>Thank you</h4>Your message has been sent, we\'ll get back to you shortly :)'
    );

    function createFormMessage( $formdata )
    {
        global $CONFIG;

        ob_start();

        extract($formdata);
        include $CONFIG['mail_contents'];

        return ob_get_clean();
    }

    function cleanInput($input) {
        $search = array(
            '@<script[^>]*?>.*?</script>@si',   // Strip out javascript
            '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
            '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
            '@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments
        );

        $output = preg_replace($search, '', $input);
        return $output;
    }

    function sanitize($input) {
        if (is_array($input)) {
            foreach($input as $var=>$val) {
                $output[$var] = sanitize($val);
            }
        }
        else {
            if (get_magic_quotes_gpc()) {
                $input = stripslashes($input);
            }
            $input  = cleanInput($input);
            $output = $input;
        }
        return $output;
    }

    $formdata = sanitize( $_POST['ContactForm'] );
    $response = array();
    $validator = new Validator();
    $validator
        ->set_label('name', 'Name')
        ->set_label('email', 'Email')
        ->set_label('subject', 'Subject')
        ->set_label('comment', 'Comment')
        ->add_filter('name', 'trim')
        ->add_filter('email', 'trim')
        ->add_filter('email', 'strtolower')
        ->add_filter('subject', 'trim')
        ->add_rule('name', new Rule\NotEmpty())
        ->add_rule('email', new Rule\NotEmpty())
        ->add_rule('email', new Rule\Email())
        ->add_rule('subject', new Rule\NotEmpty())
        ->add_rule('comment', new Rule\NotEmpty());

    if( $validator->is_valid( $formdata ) )
    {
        include 'lib/swiftmail/swift_required.php';

        $transport = Swift_MailTransport::newInstance();
        $mailer = Swift_Mailer::newInstance($transport);

        $body = createFormMessage($formdata);

        $message = Swift_Message::newInstance();
        $message
            ->setSubject($formdata['subject'])
            ->setFrom($formdata['email'])
            ->setTo($CONFIG['mail_send_to'])
            ->setBody($body, 'text/html');

        if( !$mailer->send($message) ) {
            $response['success'] = false;
            $response['message'] = $CONFIG['mail_failed_msg'];
        } else {
            $response['success'] = true;
            $response['message'] = $CONFIG['form_success_msg'];
        }
    } else {
        $response = array( 'success'=>false, 'message'=>sprintf($CONFIG['form_errors_msg'], implode('</li><li>', $validator->get_errors() ) ) );
    }

    echo json_encode($response);
?>

这里有错误 :解析错误:语法错误,意外的 T_FUNCTION,在第 3 行的 /home/lbrisas/public_html/TRY/ibrisas/php/contact.php 中期待 ')'

提前致谢

【问题讨论】:

标签: php ajax


【解决方案1】:

而不是使用

spl_autoload_register($class_name) {
// your code
}

试试这个

function __autoload($class_name) {
// your code
}

【讨论】:

  • 您好,感谢重播,但问题出在 PHP 版本上,正如上面两个回复中提到的那样。谢谢
【解决方案2】:

您需要更改调用函数的方式,因为您的 PHP 版本不支持匿名函数:

function stack_2145($class_name)
{
    $file_name = str_replace('\\', '/', $class_name);
    $file_name = str_replace('_', '/', $file_name);
    $file = dirname(__FILE__) . "/lib/$file_name.php";
    if(is_file($file)) include $file;
}
spl_autoload_register("stack_2145");

【讨论】:

  • 这还不够,只需查看该行正下方的行。该代码还使用名称空间和可能的其他现代 PHP 功能。
【解决方案3】:

您需要升级到 PHP 5.3 以获得匿名函数支持。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    • 2012-05-11
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多