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.

Nixxx
  • 133
  • 2
  • 10
  • This looks like it is setup correctly. Perhaps you're not getting anything back because the fName function is returning an empty value? – mjohns Jun 25 '15 at 01: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? – Nixxx Jun 25 '15 at 01:18

2 Answers2

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;
}
Drakes
  • 23,254
  • 3
  • 51
  • 94
  • 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 – Nixxx Jun 25 '15 at 01:24
  • 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 – Drakes Jun 25 '15 at 01:25
  • 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. – Nixxx Jun 25 '15 at 01:31
  • 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: http://stackoverflow.com/questions/132194/php-storing-objects-inside-the-session – Drakes Jun 25 '15 at 01:46
  • 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! – Nixxx Jun 25 '15 at 01:59
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();
Scalable
  • 1,550
  • 4
  • 16
  • 29
  • yeap but i have two functions and both has an echo. how can i retrieve only the data from my fname function? – Nixxx Jun 25 '15 at 01:13
  • 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? – Nixxx Jun 25 '15 at 01:25