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