js删除提交的表单数据 [英] js deleting submitted form data

查看:197
本文介绍了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

这篇关于js删除提交的表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆