简体   繁体   中英

js deleting submitted form data

The problem: Javascript function( which was premade by template) cleans the form data sent to me by php

The php code:

<?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;

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;
});

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.

Please help me fix it(ie 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

You haven't provided the data argument for $.post

Try

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;
});

References:

jQuery.post() Docs

jQuery.serialize() Docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM