dsljpwi494719
2015-11-23 17:13
浏览 48
已采纳

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

I'm trying to build out a custom email form for a SilverStripe site that's a mix of jQuery, HTML, PHP, and SilverStripe API, and I've been stuck on trying to get the form fields over to the server side. And please, please NO suggestions for form plugins--the form is setup as it is and I am not going to go back and spend hours redoing it.

Here is the function that is getting and supposedly sending the form data to the server:

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

And here is the ScheduleAppointment function in the Page.php controller:

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."
"."Last Name: ".$lname."
"."Date of Birth: ".$birthday."
"."Phone Number: ".$phone."
"."Email: ".$useremail."
"."Best Time to Call: ".$calltime."
"."Location/Hospital: ".$location."
"."Physician: ".$doctor."
"."Service Requested: ".$servicereq."
" ";

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

     $email->send();
}

I have tried debugging the form, and I can see that the variables are all null ($fname, $lname, $birthday, etc), so something isn't right. Either the jQuery.ajax function is flawed or the this->getRequest()->param() setup is correct.

If anyone has any suggestions on how to fix the issue, I would be very grateful. I've been trying to solve the problem for hours and I am pretty much stumped right now.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dongzice4895 2015-11-23 17:45
    最佳回答

    As Steve noted in the comment:

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

    is wrong. It's for getting stuff out of URL parameters, as $Action, $ID or whatever you define in your routes or $url_handlers (see docs)

    To get POST vars, you need to call:

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

    To get both, POST or GET vars you can call:

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

    See docs

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题