doulu1968 2015-10-29 20:07
浏览 44

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

I have built out a fairly simple html form that, once submitted, will email the contents to an email address.

I am have setup jQuery to take the form data and send it to a php code file thrugh an ajax request:

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

I am lost as to what to do in the SilverStripe code file, though, to process the data (I don't have much experience setting up forms like this using ajax, and I normally use a form plugin in SilverStripe). Any help or even some hints would be great so I can figure out what step to take next.

I have the function "shell" ready to go in HomePage.php (under mysite/code):

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

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥35 平滑拟合曲线该如何生成
    • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
    • ¥15 名为“Product”的列已属于此 DataTable
    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 自己瞎改改,结果现在又运行不了了
    • ¥15 链式存储应该如何解决
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站