SilverStripe 自定义电子邮件表单 - jQuery.ajax 未将变量传递到服务器

SilverStripe Custom Email Form - jQuery.ajax not passing variables to server

我正在尝试为 SilverStripe 网站构建自定义电子邮件表单,该表单混合了 jQuery、HTML、PHP 和 SilverStripe API,而且我一直坚持尝试将表单字段转移到服务器端。请不要对表单插件提出任何建议——表单是按原样设置的,我不会回去花几个小时重做它。

这是获取表单数据并将其发送到服务器的函数:

function ajax_script() {
  var form_data = {
    firstname: SchFirstName.val(),
    lastname: SchLastName.val(),
    useremail: SchEmail.val(),
    birthday: SchBirthday.val(),
    phone: SchPhone.val(),
    servicereq: SchService.val(),
    location: SchLocation.val(),
    doctor: SchDoctor.val(),
    calltime: SchTime.val()
  };
  $.ajax({
    type: "POST",
    url: "/home/ScheduleAppointment",
    data: form_data,
    success: function(response) {
      console.log(response);
    }
  });
}

这里是 Page.php 控制器中的 ScheduleAppointment 函数:

public function ScheduleAppointment() {
     $fname = $this->getRequest()->param('firstname');
     $lname = $this->getRequest()->param('lastname');
     $birthday= $this->getRequest()->param('birthday');
     $useremail= $this->getRequest()->param('useremail');
     $phone = $this->getRequest()->param('phone');
     $servicereq = $this->getRequest()->param('servicereq');
     $location = $this->getRequest()->param('location');
     $doctor = $this->getRequest()->param('doctor');
     $calltime = $this->getRequest()->param('calltime');

     $from = '[hidden]';
     $to = '[hidden]';
     $subject = 'Appointment Request Submission';

     $body = "First Name: ".$fname."\r\n"."Last Name: ".$lname."\r\n"."Date of Birth: ".$birthday."\r\n"."Phone Number: ".$phone."\r\n"."Email: ".$useremail."\r\n"."Best Time to Call: ".$calltime."\r\n"."Location/Hospital: ".$location."\r\n"."Physician: ".$doctor."\r\n"."Service Requested: ".$servicereq."\r\n" ";

     $email = new Email($from, $to, $subject, $body);
     $email->replyTo($useremail);

     $email->send();
}

我试过调试表格,我可以看到变量都是null$fname$lname$birthday等),所以有点是不对的。 jQuery.ajax 功能有缺陷或 this->getRequest()->param() 设置正确。

如果有人对如何解决此问题有任何建议,我将不胜感激。几个小时以来,我一直在努力解决这个问题,现在我几乎被难住了。

正如史蒂夫在评论中指出的那样:

$this->request->param('foo'); 

错了。它用于从 URL 参数中获取内容,如 $Action、$ID 或您在路由或 $url_handlers (see docs)

中定义的任何内容

要获取 POST 变量,您需要调用:

$this->request->postVar('foo');

要获得两者,POST 或 GET 变量,您可以调用:

$this->request->requestVar('foo');

See docs