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.