doutuoji8418 2014-01-21 08:46
浏览 57
已采纳

jQuery Ajax表单发布到PHP空白信息

I'm using the following jQuery and PHP where people fill in a form, jQuery then goes through the fields, validates there is content and if everything is filled in correctly it will send to PHP which then formats the email and sends.

The problem is when I am trialing it, it works fine but I have received emails with the line headings such as Name: and Email: etc but the user input content is blank.

Could it be possible that search engines are crawling the site and come across the PHP file and this is why it sends blank data?

jQuery

$('body').on('click','.sendmail',function(){
    var arr = [];
    $('.form input').each(function(index, element) {
        var i = $(this).prop('id');
        var v = $(this).val();
        var obj = {key: i, val: v};
        arr.push(obj);
    });
    arr.push({key:"subject", val: $('.subject .active').text()});
    arr.push({key:"comments", val: $('#comments').val()});
    var param = {};
    $.each(arr, function() {
        param[this.key] = this.val;
    });
    var sValid;
    var isValid = !$('#name, #company, #tel, #comments').filter(function() {return !this.value;}).length;
    var eValid = validateEmail($('#email').val());
    if ($('.subject span').hasClass("active")) {
        sValid = true;
    } else {
        sValid = false;
    }
    if (isValid && sValid && eValid){
        $.ajax({
            type: "POST",
            url: "/assets/inc/contact.php",
            data: param,
            dataType: "json",
            success: function(data) {
                $('.form span').removeClass('active');
                    $('.form-response').removeClass('green');
                    $('.form-response').removeClass('red');
                if (data.sent == 1){
                    $('.form-response h1').text('Form is sent! We\'ll be in touch soon');
                    $('.form-response').addClass('green');
                } else {
                    $('.form-response h1').text('Form not sent! Please check all fields are filled in.');
                    $('.form-response').addClass('red');
                }
                $('.form-response').slideDown().delay(3000).slideUp();
                $('.form input, .form textarea').val('');
                $('.form label').css({'opacity':1, 'display':'block'});
                $('.form span').removeClass('active');
            }
        });
    } else {
        $('.form-response h1').text('Form not sent! Please check all fields are filled in.');
        $('.form-response').addClass('red');
        $('.form-response').slideDown().delay(3000).slideUp();      
    }
});

PHP

<?php
$name       = $_POST['name'];
$email      = $_POST['email'];
$company    = $_POST['company'];
$tel        = $_POST['tel'];
$subject    = $_POST['subject'];
$comments   = $_POST['comments'];


$to      = 'info@domain.com';
$subject = $subject;
$message = '
Name: '.$name.'<br>
Email: '.$email.'<br>
Company: '.$company.'<br>
Tel: '.$tel.'<br><br><hr><br><br>
Comments: '.$comments.'<br>
';
$headers = 'From: noreply@domain.com' . "
" .
    'X-Mailer: PHP/' . phpversion();
$headers .= 'Content-Type: text/html; charset=utf-8';


// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "
";
$headers .= 'Content-type: text/html; charset=utf-8' . "
";

// Additional headers
$headers .= 'From: '.$name.' <'.$email.'>' . "
";


$sent = mail($to, $subject, $message, $headers);

if($sent){
    $sent = 1;
} else {
    $sent = 0;
}
$post_data = json_encode(array('sent' => $sent));
echo $post_data;
  • 写回答

6条回答 默认 最新

  • douyao2529 2014-01-21 09:43
    关注

    I found that going directly to the .php file which executes the above would send the file. I think that it was crawled or someone looked at my code directly and checked out the PHP file.

    I have taken car of it by wrapping my PHP with the following

    if (!empty($name) and !empty($email) and !empty($company) and !empty($tel) and !empty($subject) and !empty($comments)) {
        // my code to send the form
    } else {
        $post_data = json_encode(array('sent' => 'no'));
        echo $post_data;    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动