将表单数据从jQuery.ajax()传递到SilverStripe php文件以进行验证和发送电子邮件

时间:2015-10-29 20:07:45

标签: php jquery ajax

我已经构建了一个相当简单的html表单,一旦提交,就会将内容通过电子邮件发送到电子邮件地址。

我已经设置了jQuery来获取表单数据并通过ajax请求将其发送到php代码文件:

(function($) {
      $(document).ready(function() {
        var SubmitBtn = $('#Schedule-Button');
        var SchFirstName = $('#FName');
        var SchLastName = $('#LName');
        var SchAddress = $('#Address');
        var SchCity = $('#City');
        var SchZip = $('#Zip');
        var SchEmail = $('#Email');
        var SchPhone = $('#Phone');
        var SchService = $('#ServiceReq');
        var SchLocation = $('#Location');
        var SchDoctor = $('#PhysicianName');
        var SchOther = $('#OtherService');
        var SchTime = $('#BestTime');
        var SchInterpret = $('#InterpreterNeeded');
        var SchedulePopup = $('[data-remodal-id=sch-modal]').remodal();

        SubmitBtn.on('click', function(e) {
          var required = [SchFirstName, SchLastName, SchAddress, SchCity, SchZip, SchEmail, SchPhone, SchService];
          var containsError = false;
          e.preventDefault();
          for (i = 0; i < required.length; i++) {
            var input = required[i];
            if ((input.val() == "")) {
              containsError = true;
              input.addClass('error-field');
              $('.Sch-Error-Msg').show();
            } else {
              input.removeClass('error-field');
              $('.Sch-Error-Msg').hide();
            }
          }
          if (containsError == false) {
            ajax_script();
            $('#appt-form')[0].reset();
            SchedulePopup.close();
          }
        });

        function ajax_script() {
          var form_data = {
            firstname: SchFirstName.val(),
            lastname: SchLastName.val(),
            useraddress: SchAddress.val(),
            city: SchCity.val(),
            zipcode: SchZip.val(),
            email: SchEmail.val(),
            phone: SchPhone.val(),
            servicereq: SchService.val(),
            location: SchLocation.val(),
            doctor: SchDoctor.val(),
            other: SchOther.val(),
            calltime: SchTime.val(),
            interpreter: SchInterpret.prop('checked')
          };
          $.ajax({
            type: "POST",
            url: "/home/ScheduleAppointment",
            data: form_data,
            success: function() {
              alert("Form submitted! Thank you.")
            }
          });
        }
  });
}(jQuery));

我很遗憾在SilverStripe代码文件中要做什么来处理数据(我没有太多经验使用ajax设置这样的表单,我通常在SilverStripe中使用表单插件)。任何帮助甚至一些提示都会很棒,所以我可以弄清楚下一步要做什么。

我已准备好在HomePage.php(在mysite / code下)的函数“shell”:

public function ScheduleAppointment() {
   //here is where I process the form data from the form_data variable
    return "test";
}

0 个答案:

没有答案