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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 谁有RH342练习环境
    • ¥15 STM32F407 DMA中断问题
    • ¥15 uniapp连接阿里云无法发布消息和订阅
    • ¥25 麦当劳点餐系统代码纠错
    • ¥15 轮班监督委员会问题。
    • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
    • ¥20 关于变压器的具体案例分析
    • ¥15 生成的QRCode圖片加上下載按鈕
    • ¥15 板材切割优化算法,数学建模,python,lingo
    • ¥15 科来模拟ARP欺骗困惑求解