簡體   English   中英

jQuery表單發布問題

[英]jQuery Form Posting Issue

我正在使用來自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;
    }
}

確保您的服務器支持簡短的PHP開放標記<?

如果不是,請執行以下操作:更改php.ini文件中的short_open_tag值或使用<?php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM