douzhang8033 2013-05-01 08:54
浏览 37

如何将表单数据发送到CSV

I have a specific request from a client that a newsletter signup be sent to a CSV file. I am a noob when it comes to anything backend, let alone frontend development.

I have a template that I am working from and can't make sense of the way it delivers the values.

The form code is quite simple

<form action="" method="post" class="signup" id="newsletter-form">
    <p>
        <input type="text" name="signup_name" id="signup_name" class="required" value="Your Name" />
    </p>
    <p>
        <input type="text" name="signup_email" id="signup_email" class="required" value="Your E-mail" />
    </p>
    <input type="submit" value="SEND" class="signupSubmit" id="submitform" />
    <div id="newsletter-msg-wrapper" style="position:relative; clear:both; width:100%;">
        <div id="newsletter-loader"></div> <span id="newsletter-msg"> &nbsp; </span>

    </div>
</form>

Then I have this .js file that seems to be handling the post

$(document).ready(function () {
    var contactFormDefaults = new Array();
    contactFormDefaults['name'] = 'Your Name';
    contactFormDefaults['email'] = 'E-mail';
    contactFormDefaults['subject'] = 'Subject';
    contactFormDefaults['message'] = 'Message';
    contactFormDefaults['msg'] = $('.contactForm span#msg').html();

    $('.contactForm input[type="text"], .contactForm textarea').focus(function () {
        $(this).addClass('inputHighlight').removeClass('errorOutline');
        if ($(this).hasClass('required')) {
            $('.contactForm span#msg').html('This is a required field.').removeClass('errorMsg successMsg');
        } else {
            $('.contactForm span#msg').html(contactFormDefaults['msg']).removeClass('errorMsg successMsg');
        }
        if ($(this).val() == contactFormDefaults[$(this).attr('id')]) {
            $(this).val('');
        }
    });
    $('.contactForm input[type="text"], .contactForm textarea').blur(function () {
        $(this).removeClass('inputHighlight');
        $('.contactForm span#msg').html(contactFormDefaults['msg']).removeClass('errorMsg successMsg');
        if ($(this).val() == '') {
            $(this).val(contactFormDefaults[$(this).attr('id')]);
        }
    });

    $('.contactForm input[type="text"], .contactForm textarea').hover(function () {
        $(this).addClass('inputHighlight');
    }, function () {
        $(this).not(':focus').removeClass('inputHighlight');
    });

    $('.contactForm').submit(function () {
        $('.contactForm .submit').attr("disabled", "disabled");
        $('#msg').html('<img src="images/loader-light.gif" />').removeClass('errorMsg successMsg');
        var isError = false;
        $('.contactForm input, .contactForm textarea').each(function () {
            if ($(this).hasClass('required') && ($.trim($(this).val()) == contactFormDefaults[$(this).attr('id')] || $.trim($(this).val()) == '')) {
                $(this).addClass('errorOutline');
                $('#msg').html('There was an error sending your message. Please try again.').addClass('errorMsg');
                isError = true;
            }
            if ($(this).attr('id') == 'email') {
                var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
                if (reg.test($(this).val()) == false) {
                    $(this).addClass('errorOutline');
                    if (!isError) {
                        $('#msg').html('Please enter a valid e-mail address and try again.').addClass('errorMsg');
                    }
                    isError = true;
                }
            }
        });
        if (isError) {
            $('.contactForm .submit').removeAttr("disabled");
            return false;
        } else {
            var name = $('#name').val(),
                email = $('#email').val(),
                subject = $('#subject').val(),
                message = $('#message').val();
            $.ajaxSetup({
                cache: false
            });
            var dataString = 'name=' + name + '&email=' + email + '&subject=' + subject + '&message=' + message + '&ajax=1';
            $.ajax({
                type: "POST",
                url: "../myform.php",
                data: dataString,
                success: function (msg) {
                    // Check to see if the mail was successfully sent
                    if (msg == 'Mail sent') {
                        // Update the progress bar
                        $('#msg').html('Message sent.').addClass('successMsg');
                        // Reset the subject field and message textbox
                        if (contactFormDefaults['subject']) {
                            $('#subject').val(contactFormDefaults['subject']);
                        } else {
                            $('#subject').val('');
                        }
                        if (contactFormDefaults['message']) {
                            $('#message').val(contactFormDefaults['message']);
                        } else {
                            $('#message').val('');
                        }
                    } else {
                        $('#msg').html('There was an error sending your email. Please try again.').addClass('errorMsg');
                        $('.contactForm .submit').attr("disabled", "");
                    }
                    // Activate the submit button
                    $('.contactForm .submit').removeAttr("disabled");
                },
                error: function (ob, errStr) {
                    $('#msg').html('There was an error sending your email. Please try again.').addClass('errorMsg');
                    //Activate the submit button
                    $('.contactForm .submit').removeAttr("disabled");
                }
            });
            return false;
        }
    });
});

If possible I'd love to know how to make this all function and what I am not seeing here and how this all can be written into a CSV file.

A full view of the site and code can be viewed here:

www.cndnsd.com/ClientAccess/Newmarket/FinalSite/index.html

The form is at the bottom of the page.

  • 写回答

1条回答 默认 最新

  • douzhao7445 2013-05-01 09:06
    关注

    in your myform.php , receive all the data (email, name etc) and start creating your CSV file. It is a simple operation and not that complicated.

    For CSV file writing, please check out these posts on stack overflow:

    write into csv file in php

    Creating csv file with php

    Also do some googling.

    Hopefully this will help you.

    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像