views:

145

answers:

6

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)
A: 

if you truely want to call a plain php file with zend framework, then you need to put it inside your public folder and call it as /user_availability.php but it should almost certainly be better to make user-availability an action of one of your controllers

roman
So if let say I have a checkcontroller.php located in my controller and my useravailability is a function in that page. How do i call it?$.post("checkcontroller.php", {user_name:$(this).val()} , function(data) ? Because this doesnt work either..
Ryan
could you post a code snippet of your controllers and/or your zend app layout?
roman
class UsercheckController extends Zend_Controller_Action { public function indexAction() { $view = Zend_Registry::get('view'); $view->render('register2.tpl'); $users = new Users(); $username = $_POST['username']; require_once 'Zend/Db.php'; $db2 = Zend_Db::factory('Pdo_Mysql', array( 'host' => '127.0.0.1', 'username' => 'roseadmin', 'password' => 'test123', 'dbname' => 'roseuser', 'port' => '3306' )); if($users->checkUnique($_POST['username'])){ echo "fail"; } }}
Ryan
The layout of my zend app is 1) /application, 1.1) /default, 1.1.1) /config 1.1.2) /controllers 1.1.3) /models 1.1.4) /views
Ryan
my code which is trying to call the controller is in views folder. thanks =)
Ryan
A: 

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):

I dont quite understand. My code that is trying to call the controller is in the view folder.
Ryan
A: 

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);
   }

}

Bryan M.
I did create the controller action. But it's just not calling it. I'm calling from the view folder and that template is using .tpl. Does it matters?
Ryan
A: 

Thanks everyone. I found the solution to it.

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

e.g:

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

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

Ryan
A: 

hi rayan

i put a function with check name but it is not called by javascript, i dont know why

i am intersted to see your complete code about this example,

i need the same code can you post your code?

thanks

ulduz114
hey, look at my example below. I hope it helps.
Ryan
A: 

Hi ulduz

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. =)

Ryan