js删除提交的表单数据


js deleting submitted form data

问题:Javascript函数(由模板预制)清除php 发送给我的表单数据

php代码:

<?php
    header('Content-type: application/json');
    $status = array(
        'type'=>'success',
        'message'=>'GJ '
    );
    $name = @trim(stripslashes($_POST['name'])); 
    $phone = @trim(stripslashes($_POST['phone'])); 
    $subject = @trim(stripslashes($_POST['subject'])); 
    $message = @trim(stripslashes($_POST['message'])); 
    $email_to = 'email@email.com';//replace with your email
    $body = 'Name: ' . $name . "'n'n" . 'Телефон: ' . $phone . "'n'n" . 'Subject: ' . $subject . "'n'n" . 'Message: ' . $message;
    $success = @mail($email_to, $subject, $body);
    echo json_encode($status);
    die;

javascript函数代码:

var form = $('.contact-form');
form.submit(function () {'use strict',
    $this = $(this);
    $.post($(this).attr('action'), function(data) {
        $this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
    },'json');
    return false;
});

当没有js代码时,php返回一行白色屏幕,但@mail发送正常消息,但当有此代码时,淡入淡出效果良好,但数据到达时为空。消息和名称中的消息是cyrylic,如果这很重要的话。

请帮我修复它(即,我需要数据到达和成功淡出出现,或者至少需要没有.php白屏的数据到达),或者只是解释js代码做了什么我没有得到。感谢

您尚未为$.post 提供data参数

尝试

var form = $('.contact-form');
form.submit(function () {'use strict',
    var $this = $(this);    
    $.post($(this).attr('action'), $this.serialize(), function(data) {
        $this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
    },'json');
    return false;
});

参考文献:

jQuery.post()文档

jQuery.serialize()文档