tags:

views:

359

answers:

1

hai guys am new to this cakephp stuff and is having problem using cakephp with jquery...

my problem is that am not able to get the values posted from jquery to a controller in php...

my controller file is simple and is given below...

users_controller.php

<?php 

class UsersController extends AppController

{
       var $name = "Users";
       var $uses = null;           //if no DB connection is reuired……….



       function index()
       {

       }

       function login()
       {
                       print_r($this->params['form']);
                       echo "<br/>";
                       echo "<br/>";
                       echo "<br/>";
                       print_r($_REQUEST);
                       echo "<br/>";
                       echo "<br/>";
                       echo "<br/>";
                       print_r($_POST['username']);
                       echo "<br/>";
                       echo "<br/>";
                       echo "<br/>";
                       echo $_POST['data'];
                       echo "<br/>";
                       echo "<br/>";
                       echo "<br/>";
                       print_r($this->data);
                       echo "<br/>";
                       echo "<br/>";
                       echo "<br/>";

         }

}

?>

===========================

-> what am doing in this controller is just trying to output the value posted from jquery,But none

of them seems to work....

->my view files for login is given below

login.ctp

<html>
<head>
<?=$html->css('general');?>
<?=$javascript->link('jquery'); ?>
<?=$javascript->link('jquery.form'); ?>
<?=$javascript->link('LoginValidation'); ?>
</head>
<body>
<h1 align="center"><u>Login Page</u></h1>
<form method="post" action="" id="customForm" name="customForm">
<table cellpadding="0" cellspacing="10">
<tr><td>User Name :</td><td><input id="username" name="username"
type="text" /></td><td><span id="usernameInfo"></span></td></tr>
<tr><td>Password :</td><td><input id="password" name="password"
type="password" /></td><td><span id="passwordInfo"></span>
</td></tr>
<tr><td colspan="2" align="right"><input id="submit" name="submit"
type="submit" value="Log In" /></td></tr>
</table>
</form>
</body>
</html>

=============================

->the login view is just a simple one which uses plain php coding...i havent used any helpers, sorry for that.....but am comfortable using this....

->finally my javascript code LoginValidation.js where the view specific validation is given below

LoginValidation.js

// JavaScript Document
$(document).ready(function(){

//global vars
var form = $("#customForm");
var username = $("#username");
var password = $("#password");
var usernameInfo = $("#usernameInfo");
var passwordInfo = $("#passwordInfo");
var site_url = "http://localhost/jeffery/cake_jquery/";
//http://localhost/jeffery/cake_jquery/users/login  var str = $
("form").serialize();

//On blur
username.blur(validateUserName);
password.blur(validatePassword);

//On key press
username.keyup(validateUserName);
password.keyup(validatePassword);

//On Submitting
form.submit(function(){
       if(validateUserName()  &  validatePassword() )
               {

                       $.ajax({
                                       type: "POST",
                                       url: "http://localhost/jeffery/cake_jquery/users/login",
                                       data: "username="+ username.val() + "password=" + password.val(),
                                       success: function(){
                                               form.hide();
                                               $('div.success').fadeIn();
                                       }
                               });
                               return false;
               }
               return false;

});



//validation functions
function validateUserName(){
               //if it's NOT valid
               if(username.val() == ""){
                       username.addClass("error");
                       usernameInfo.addClass("error");
                       usernameInfo.text("User Name is required !!!");
                       return false;
               }
               else
               {
                       username.removeClass();
                       usernameInfo.removeClass();
                       usernameInfo.text("");
                       return true;
               }

}

function validatePassword(){

               //it's NOT valid
               if(password.val() == ""){
                       password.addClass("error");
                       passwordInfo.addClass("error");
                       passwordInfo.text("Password is required !!!");
                       return false;
               }
               //it's valid
               else
               {
                       password.removeClass();
                       passwordInfo.removeClass();
                       passwordInfo.text("");
                       return true;
               }
       }


});             //      end of $(document).ready(function()

============================================

=> so what i intend to do here is use a login form, validate it and submit it via ajax, so that page is not refreshed......

=>and am just trying to view the submiited data via ajax in the login controller just to make sure

that the controller is receiving the value.....

=> THE PROBLEM IS AM NOT GETTING THE VALUES TO BE VIEWD......

=> ANY HELP FROM U GUYS WILL BE VERY GREATFULLL

=>THANKS IN ADVANCE..........................................

A: 

In your ajax code there is

data: "username="+ username.val() + "password=" + password.val()

You are missing the '&', should be:

data: "username="+ username.val() + "&password=" + password.val()
Daniel Moura
Thanz for the reply Daniel.....ya i did what u told but still no go....its not showing any data.......but if i submit the page i gets the data ,But i need these data without submitting and so am usingthis jquery.................but with cakephp this stuff isnt going well.............However if i used the same code in normal php coding, its working perfectly fine...cuz there am able to get the values to a php page using $_POST[]; method....The problem is when using cakephp frame work ....Is there something missing or am using a wrong method to get the data???HELP