jQuery表单发布问题

时间:2014-12-08 17:01:33

标签: javascript php jquery ajax

我正在使用malsup的jQuery Form Post插件,代码如下:

//Post a form
function PostForm(FormID, Target) {
var $t = Math.round(new Date().getTime() / 1000);
try{
    var options = {
        target: Target,
        beforeSubmit: function () {
            jQuery(Target).html('<div id="frmLoadingImageWrapper"><img src="/assets/images/ajax-loader.gif" alt="loading..." height="11" width="16" /></div>');
            jQuery(Target).slideDown('slow');
        },
        success: function (html) {
            setTimeout(function () {
                        jQuery(Target).html(html);
                        jQuery(FormID)[0].reset();
                        if($('#captcha-gen').length){
                            $.get('/inc/captcha.php?_=' + $t, function(data){
                                $('#captcha-gen').html(data);
                            });
                        }
                     }, 100);
        },
        error: function(e){
            var $html = e.responseText;
            jQuery(Target).html($html);
            jQuery(Target).slideDown('fast');
            if($('#captcha-gen').length){
                $.get('/inc/captcha.php?_=' + $t, function(data){
                    $('#captcha-gen').html(data);
                });
            }
            setTimeout(function() {
                            jQuery(Target).slideUp('fast'); 
                         }, 3500);
        }
    };
    jQuery(FormID).ajaxSubmit(options);
}catch(err){
    alert(err.message);
}
}

当我将表单提交至/inc/mail.php时,我的PHP中显示的是Target代码,而不是处理。

如何解决此问题?所有其他PHP脚本都可以正常工作,包括其他ajax提取的php脚本。

这是邮件代码,它使用的是PHP SMTP

<?

require("/inc/class.phpmailer.php");

//form validation vars
$formok = true;
$errors = array();

//sumbission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('m/d/Y');
$time = date('H:i:s');

//form data
$name = $_POST['name']; 
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$captcha = $_POST['secAnswer'];

$valid = true;

if(!is_string($email) || !(strlen($email)>0) || !ValidateEmail($email)){
    $valid = false;
}
if(!is_string($name) || !(strlen($name)>0) || !ValidateText($name)){
    $valid = false;
}
if(!is_string($message) || !(strlen($message)>0) || !ValidateText($message)){
    $valid = false;
}
if(!CheckCAPTCHA($captcha)){
    $valid = false;
}

sleep(1.5);

if($valid){

    $mail = new PHPMailer();

    $mail->IsMail();                                         // send via SMTP
    $mail->From     = $email;                // SMTP username again
    $mail->AddAddress("kevin@pirnie.us");                // Your Adress
    $mail->Subject  =  "New mail your site!";
    $mail->IsHTML(true);  
    $mail->CharSet = 'UTF-8';
    $mail->Body     =  "<p>You have recieved a new message from the enquiries form on your website.</p>
                          <p><strong>Name: </strong> {$name} </p>
                          <p><strong>Email Address: </strong> {$email} </p>
                          <p><strong>Phone: </strong> {$phone} </p>
                          <p><strong>Message: </strong> {$message} </p>
                          <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";

    if(!$mail->Send())
    {
       echo "Mail Not Sent <p>";
       echo "Mailer Error: " . $mail->ErrorInfo;
       exit;
    }

    echo "Mail Sent";

}else{
    echo "Mail Not Sent.  Please make sure all fields are filled out correctly.";
}

function ValidateEmail($str){
    $atIndex = strrpos($str, "@");
    if (is_bool($atIndex) && !$atIndex){
        return false;
    }else{
        if (filter_var($str, FILTER_VALIDATE_EMAIL)) {
            $domain = substr($str, $atIndex + 1);
            return (checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"));
        }else{
            return false;
        }
    }
}

function ValidateText($str){
    return (bool)preg_match("/^[a-zA-Z0-9 _-]+$/", $str);
}

function CheckCAPTCHA($str){
    require_once($_SERVER['DOCUMENT_ROOT'] . '/inc/captcha.class.php');
    $csc = new ResponsiveCaptcha();
    if($csc->checkAnswer($str)){
        return TRUE;    
    }else{
        return FALSE;
    }
}

1 个答案:

答案 0 :(得分:1)

确保您的服务器支持简短的PHP开放标记<?

如果不是:更改php.ini文件中的short_open_tag值 或使用<?php