I have a html file which has a form. I want to send the form data to a function in a php class in another php file (process-registration.php) via jQuery AJAX. My question is i) How do I set the url variable in the AJAX request? Should I include the function name handling the request in the php class? and ii) How do I receive the form data sent via AJAx in the php class function? Here is the html code
<form id = "registration form">
<input type = "text" id = "name" placeholder = "Name" />
<input type = "text" id = "email" placeholder = "Email" />
<input type = "submit" id = "register" value "Register" />
</form>
//Jquery Ajax
var name = $("#name").val();
var email = $("#email").val();
var datastring = 'name='+name+'&mail='+email;
$.ajax({
//Should I add the function name to the url to look like url: "http://localhost/mySite/controllers/process-registration.php/addUser()"
type: "POST",
url: "http://localhost/mySite/controllers/process-registration.php",
data: datastring,
cache: false,
contentType: false,
processData: false,
success: function(data){
alert("User registered successfully.");
window.location.reload(true);
}
});
//php code (process-registration.php)
<?php
require_once("../models/registrationModel.php")
class Process-registration(){
function addUser(){
//Where should I grab these values? Within the class? Outside the class? Within the function?
$name = $_POST["name"];
$email = $_POST["email"];
}
}
?>