js删除提交的表单数据

问题描述:

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

The problem: Javascript function( which was premade by template) cleans the form data sent to me by 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函数代码:

The javascript function code:

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.

When there's no js code php returns white screen with a line but the @mail sends normal message, but when there is this code fade in and out works good, but the data arrives empty. The message in message and name is cyrylic if that is important.

请帮助我解决该问题(即我需要同时显示数据到达和成功淡入,或者至少需要数据到达而没有.php白屏),或者只是解释一下我没有得到js代码是什么. 谢谢

Please help me fix it(i.e. i need both data arriving and success fade-in appearing or at least data arriving with no .php white screen) or just explain what the js code does i don't get it. Thanks

您尚未为$.post

尝试

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()文档

jQuery.serialize() Docs