简体   繁体   中英

Calling a function using zend framework

I am currently using zend framework and working with jquery and php. Trying to implement an autocheck username availability.

For the following code, I need to call my user_availability.php which is in my controller folder. I did /controllers/user_availability, ../user_availability but it doesnt call that page. Can anyone help?

$.post("user_availability.php",{ user_name:$(this).val() } ,function(data)

如果您确实想使用zend框架调用纯文本php文件,则需要将其放在公用文件夹中,并将其命名为/user_availability.php,但几乎可以肯定,使用户可用性成为您的一项行为是更好的选择控制器

Look into Action and View Helpers (depending on where you want to call the script from). Action Helpers can be called from controllers and may be kept in a .../project/library.

From a view you can call a custom View Helper you've written and stored in .../projectname/application/view/helpers.

You may want to restructure your code to fit better within the framework but simply creating a helper that requires (includes) your .php script should work.

View helpers (Survive the Deep End)

Action helpers (from the manual):

It appears you're making a page request to a non-controller item. Anytime you make a request in Zend Framework, it will attempt to match the URL of the request to a route in the system, which corresponds to a controller action. All of this is handled via your application's index.php file - the only way to serve a different php file directly would be through your public folder.

My suggestion is to create a controller action that checks for an Ajax request and returns something in your format of choice (XML, json) (rather than rendering an HTML template).

public function userAvailableAction() 
{
   if ($this->getRequest()->isXmlHttpRequest()) { 
      // check user availability
      $this->_helper->json($data);
   }

}

Thanks everyone. I found the solution to it.

You just need to put the function in the controller page and call the function name.

eg:

$.post("check",{ user_name:$(this).val() } ,function(data)

Then in your controller file checkusername availability make sure you add the check function.

my controller page is as follows the code:

class RegisterController extends Zend_Controller_Action {
    public function checkAction(){
        $users = new Users();
        $username = $_POST['username'];
        if($users->checkUnique($_POST['username'])){
            echo "fail";
        }           
}

In this case, the checkUnique is just an sql statement in my model controller to check if the username exist.

For my jquery code it is:

$("#username").blur(function(){
    //remove all the class add the messagebox classes and start fading
    $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
    //check the username exists or not from ajax
    $.post("check",{ user_name:$(this).val() } ,function(data){
        if(data=='no'){ //if username not avaiable
            $("#msgbox").fadeTo(200,0.1,function(){ //start fading the messagebox
                //add message and change the class of the box and start fading
                $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
           });
        }else {
         $("#msgbox").fadeTo(200,0.1,function(){  //start fading the messagebox
                //add message and change the class of the box and start fading
                $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);
            });
        } 
    });
});

I got this example from this link:

http://roshanbh.com.np/2008/04/check-username-available-ajax-php-jquery.html . Do take a look at it. I hope it helps. =)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM