0

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"];
    }
}
?>
Kizangila
  • 11
  • 3
  • possible duplicate of [jQuery Ajax POST example with PHP](http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php) – krishna Apr 22 '15 at 10:27
  • Krishna, that post is just receiving data in a php file but in my question I want the form data to be received in a php class and processed by a function in that class – Kizangila Apr 22 '15 at 10:36
  • so what you have to do is, in that file called by ajax, call the php class and use the function in that class – krishna Apr 22 '15 at 10:38
  • My question is on how to do that Krishna – Kizangila Apr 22 '15 at 10:45
  • pls do some google, and you can easily get it. check [this](http://stackoverflow.com/questions/14350803/how-to-use-a-php-class-from-another-file) post for how to do it. you can use statements like include,include_once,require or require_once – krishna Apr 22 '15 at 11:00

1 Answers1

0
<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

$(function(){
    $("#registration-form").submit(function(){
        var name = $("#name").val();
        var email = $("#email").val();
        var data = {"name":name, "email":email};
        $.ajax({
            type: "POST",
            url: "http://localhost/mySite/controllers/process-registration.php",
            data: data,
            dataType: "json",
            success:  function(data){
                if(data.status == "OK"){
                    alert(data.message)//it will give you the response message from server
                    alert("User registered successfully.");
                    window.location.reload(true);
                }else{
                    //if error
                    alert("Error - "+data.message);
                }
            },
            error: function(data){
                alert("Internal server error");
            }
        }); 
    });    
});

php code (process-registration.php)

<?php 
require_once("../models/registrationModel.php")
class Process-registration(){
    function addUser(){
       //Where should I grab these values? "Thats upto you"
       $name = $_POST["name"];
       $email = $_POST["email"];
       $response = array("status"=>"OK","message"=>"Got name($name) and email($email)");
       return json_encode($response);
    }
}

$process-registration=new Process-registration();
echo $process-registration->addUser();
?>
Aneesh R S
  • 3,807
  • 4
  • 23
  • 35