使用ajax调用php方法

Hi i would like to use ajax in my website where all requests pass are loaded in index.php (i use htacess for rewriting urls) so when i use ajax i always reload the current page and then use if(expressio) to check if the user has called a function with ajax but this cause that all the page is reloaded each time i use ajax.

I was wondering if there is a method to call a specific php method/function or property in the current page with ajax without reloading all the page.

I'm using the jquery library for ajax

If someone knows some other ways is ok!

A main use of ajax is that you call asynchronously something. Most often functions-methods that are called with ajax do rely on the same php file (if not then it's okay) with other stuff that you do not need to call asynchronously.

For example you have a method that is called via ajax to autocomplete a text field (like google search) in a file that there is other stuff you don't want to execute too.

If you are under some mvc then you have controller check this out and make sure that only the requested method is called (I've done it successfully). So it is easier controlled under an mvc, all things are in classes...

If not under mvc then I guess you have to implement something like controller in order to call only the methods you like. However there is a conition that should be espected, no code should be found out of classes cause it would be executed on "include", it would go like this: file ajax handler

1.Check if an ajax call
2.if check false return;
3.if true then get the name of the class file
4. call the desired method (or methods, you have an execution flow predefined during to your needs)

So it can be done. It is important not to execute code that is not supposed to be executed since then undesired results (or errors) would occur.

in ajax i use the current url as the action of the request but this cause the re-load of the whole page.

Since you have your own mvc it could go like this index.php/rt=request/action where request=the name of the controller (which in the end is a class), and action is the name of the action (which in the end is a method inside the class-controller you are requesting) for example mysite.com/rt=user/create My point is that you don't care what the current url is, all you care is to call the right method(=action) of the right class(=controller).

The login class is istantiater in all pages because it check if a user is logged;

I don't really understand this (when you say pages you mean php files?), whatever it is I suggest (if haven't done already) to follow the single entry point (no need to do this for ajax calls) where every request hits only to index.php like I showed you above. Then a few lines of code on the very top can do the login check.

And a last thing what kind of ajax is this that reloads all the page, this an example how I do ajax for autocomplete in a text field:

put this as it is in head-script-javascript to make the connection

var request = false;
try { 
  request = new XMLHttpRequest(); 
} catch (trymicrosoft) {                         
  try { 
    request = new ActiveXObject("Msxml2.XMLHTTP"); 
  } catch (othermicrosoft) {
    try {
      request = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (failed) {                  
      request = false;       
    }
  }
}

if (!request) 
  alert("Error initializing XMLHttpRequest!"); 

or even better you can have it in a separate file and include it wherever you need it

then I use a function to do the asunchronous call, what I am doing is similar to your needs because I too get data from html and pass it to php (read the comments) and then execute sql.

   function getMatch() 
   {
        //get the form values, I have one value here, you get as need as many as you need
        var str = document.getElementById("categ_name").value ;
        var url = "internal.php";//php file to execute
        //now pass the html form parameters to php
        //do you see here controller is defined, it is named asynch
        //then see the action (method of controller class) it is called getMatch
        //then see how I pass the html form data with str, it is the string to match
        //comcateg is the mysql table to get the match
        var params = "rt=asynch/getMatch&data=" +  (str)+"&from=comcateg";

        request.open("POST", url, true);//use post for connect harder to attack, lots of data transfer 

        //Some http headers must be set along with any POST request.
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
        request.setRequestHeader("Content-length", params.length);
        request.setRequestHeader("Connection", "close");

        request.onreadystatechange = updatePage;
        request.send(params);

   }////////////////////

Then when the answear will be back this function will be called because we defined so above in the code

   function updatePage() {
     if (request.readyState == 4) {
       if (request.status == 200) 
       { 
                    //test
             var r=request.responseText.split("$$");   

             alert(r[1]);
       } 
       else{
         //alert("status is " + request.status);
         }
     }
   }

I think now you can do it without problem, remeber whatever would be echoed by php even errors all of them will come back in the request.responseText. This is a tutorial among others about mvc that I like, https://sites.google.com/site/mhabipi/php-reading-material/superMVC.htm?attredirects=0&d=1 it's written by Kevin Waterson but sadly phpro.org does not work anymore.