2

hi is it possible to retrieve data from a PHP function using Ajax?.

i want my url in ajax to point in one of my functions. please see my code below.

like this:

    <?php
     class employee{

        public function __construct(){
        }

        public function fName($fName){
         echo $fName;
        }

        public function lName($lName){
         echo $lName;
        }
     }

        ?>


  <div id="fName"></div>

        <script type="text/javascript">
         $.ajax({
                type: 'POST',
                url: "classes.php", <!-- HERE I want to target the fname function-->
                success: function(result){
                 fname.html(result);}
                        });

        </script>

what im doing so far is create a new php page which contain code like this.

<?php
$employee = new employee();
$employee->fName();

then ill point the ajax url to that new php page.

2
  • This looks like it is setup correctly. Perhaps you're not getting anything back because the fName function is returning an empty value? Commented Jun 25, 2015 at 1:13
  • yeap ,but what i want is i want to directly communicate with my class and point the url to its function. instead of creating new object in other page. is that possible? Commented Jun 25, 2015 at 1:18

2 Answers 2

1

Assuming this code works as written in your question

<?php
$employee = new employee();
$employee->fName();

you can pass a parameter to your PHP script then decided which function is to be called like so:

jQuery:

$.ajax({
  type: 'GET',
  url: "classes.php?func=fname",
  success: function(result) {
    $("fname").html(result);
  }
});

PHP - classes.php:

<?php
$employee = new employee();

$func = @$_GET["func"];
switch($func) {
    case "fname":
       echo $employee->fName(); 
       break;
    case "lname":
       echo $employee->lName(); 
       break;
    default:
       break;
}
Sign up to request clarification or add additional context in comments.

5 Comments

what i want is some shortcut that instead of creating new php page for every functions i want to directly call that functions inside the class. so far i believe this is the most near to what i want
That is impossible as you've described. PHP runs on the server before any HTML/JS is run. Once the PHP is executed, the results are sent to the browser which then executes the JS. There is no real-time communication between the two on the same page. This answer is a typical pattern of communicating with PHP
i understand, thank you for the insight and giving me the idea. i really want to avoid the creation of numerous page. i want my ajax url to point to only one page. thanks your code it help me.
There is one thing you can do to prevent recreating an object over and over again, but it has pros and cons. This is an entirely new topic, so if you wish to peruse it, please feel free to ask new questions anytime. However, the above pattern is straightforward. You might find this interesting reading: stackoverflow.com/questions/132194/…
i just realize ill put the selected function in data: {func: fname}, instead of using url: "classes.php?func=fname", hmm cool. thanks for the idea!
0

The url property has to be a url .. in your case it perhaps will point to "/classes.php" , assuming the file is on document root of the webserver

The function fName needs to be called by the code present in that url.

so your file "/classes.php" should look like this

<?php 
class Employee {  
  protected fName;
  protected lName;
  function __construct($f, $l) {
      $this->fName = $f;
      $this->lName = $l;
  }
  public function getFName(){
     return $this->fName ;
  }
  public function getLName(){
     return $this->lName ;
  }

}
$employee = new Employee("John" , "Doe");
echo $employee ->fName();

2 Comments

yeap but i have two functions and both has an echo. how can i retrieve only the data from my fname function?
i want to communicate to only one page. the code seems retrieving only the fname. what in a case i want to retrieve the lname?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.