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 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug