weixin_33713707 2017-02-22 11:16 采纳率: 0%
浏览 6

jQuery ajax表单

I have contact form and I want to use ajax for this form.

files structure:

-contact.html

-email.php

jQuery version - 1.12.4

So I add script for my form (#send_message)

var send_message = $('#send_message');
send_message.on('click', function(e){
    e.preventDefault();
    var error = false;
    var name = $('#name').val();
    var email = $('#email').val();
    var message = $('#message').val();

    if(name.length === 0){
        var error = true;
    }
    if(email.length === 0 || email.indexOf('@') === -1){
        var error = true;

    }
    if(message.length === 0){
        var error = true;
    }

    if(error == false){

        var formData = {
            'name'              : name,
            'email'             : email,
            'message'           : message
        };

        $.ajax({
            type        : 'POST',
            url         : 'email.php',
            data        :  formData,
            success: function(){
                $('#send_message').remove();
                $('#mail_success').fadeIn(500);             }
        })

    }
});

I get users name, email and message. But it seems ajax can't send data to my email.php. I can't see ajax message in console log

  • 写回答

1条回答 默认 最新

  • weixin_33676492 2017-02-22 11:34
    关注

    Data might be corrupt. Serialze formData to JSON -

    data: {
      data: JSON.stringify(formData)
    }

    At the server end deserialize -

    $dataJson=json_decode($_POST["data"]);
    $name= $dataJson->{'name'};
    $email= $dataJson->{'email'};
    $message= $dataJson->{'message'};

    return result from php - echo result

    To see what has been returned by server, replace success with complete -

    complete: function(data) {
    // keep break point in body and inspect data.ResponseText
    //check http request status
    }

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 u盘问题:盘符不显示 无媒体
  • ¥50 R语言读取nc按月均值转为tif
  • ¥30 智能车串级pid调参
  • ¥15 visual studio code翻译老是错误
  • ¥20 卫星测高数据的高程转换
  • ¥15 爬取招聘网站数据信息
  • ¥15 安装完tensorflow,import tensorflow as tf后报错,如何解决?
  • ¥15 ultralytics库导出onnx模型,模型失去预测能力
  • ¥15 linux下点对点协议连接2个USB串口的硬件流量控制问题
  • ¥15 SQL数据自动生成问题