sonoton345 Posted August 17, 2009 Share Posted August 17, 2009 Can someone help me with this code please? I'm trying to setup a page to change password, first by entering the old password then the new password(twice). What my script is doing is putting a new entry into the database. My Controller code: function admin_changepassword() { if($this->Session->check('userid')) { if($this->Session->read('usertype')=='admin') { if (!empty($this->data)) { $this->data['Admin']['id']=$_SESSION['id']; $uid = $this->Admin->findById($this->data['Admin']['id']); if($this->data['Admin']['current']!= $uid['Admin']['password']) { $this->Session->setFlash("Your old Password field didn't match"); } else if($this->data['Admin']['new_password'] != $this->data['Admin']['confirm_password'] ) { $this->Session_setFlash("New password and Confirm password field do not match"); } else { $this->data['Admin']['password'] = $this->data['Admin']['new_password']; $this->data['Admin']['id'] = $this->Admin->id; if($this->Admin->save($this->data)) { $this->Session->setFlash("Password updated"); $this->redirect(array('controller'=>'jobseekers','action'=>'welcome/')); } else { $this->Session->setFlash('Please try again.', true); } } } } } else { $this->Session->setFlash('You are not logged in.', true); $this->redirect(array('action'=>'index/')); } $this->layout = 'admin'; } My view code: <table border="0" width="100%" id="table7" cellspacing="0" cellpadding="0"> <tr> <td> <img border="0" src="<?= $html->url('/img/adm/changepass.gif'); ?>" width="235" height="38"></td> </tr> <tr> <td> </td> </tr> <tr> <td> <?php echo $form->create('Employer',array('action'=>'changepassword')); ?> <table border="0" width="753" id="table9" cellspacing="5" cellpadding="0"> <tr> <td width="140" align="left"> <p style="margin-left: 10px">Password: </td> <td width="598"> <p style="margin-left: 10px"> <?php echo $form->input('Admin.current',array('type'=>'password','label'=>false,'class'=>'input','id'=>'textfield','style'=>'font-size: 8pt; border-style: solid; border-width: 1px; padding-left: 4px; padding-right: 4px; padding-top: 1px; padding-bottom: 1px; float:left'))?> <!--input name="textfield29" size="34" style="font-size: 8pt; border-style: solid; border-width: 1px; padding-left: 4px; padding-right: 4px; padding-top: 1px; padding-bottom: 1px; float:left" /--></td> </tr> <tr> <td width="140" align="left"> <p style="margin-left: 10px">New Password:</td> <td width="598"> <p style="margin-left: 10px"> <?php echo $form->input('Admin.new_password',array('type'=>'password','label'=>false,'class'=>'input','id'=>'textfield','style'=>'font-size: 8pt; border-style: solid; border-width: 1px; padding-left: 4px; padding-right: 4px; padding-top: 1px; padding-bottom: 1px; float:left'))?> <!--input name="textfield27" size="34" style="font-size: 8pt; border-style: solid; border-width: 1px; padding-left: 4px; padding-right: 4px; padding-top: 1px; padding-bottom: 1px; float:left" /--></td> </tr> <tr> <td width="140" align="left"> <p style="margin-left: 10px">Confirm New Password:</td> <td width="598"> <p style="margin-left: 10px"> <?php echo $form->input('Admin.confirm_password',array('type'=>'password','label'=>false,'class'=>'input','id'=>'textfield','style'=>'font-size: 8pt; border-style: solid; border-width: 1px; padding-left: 4px; padding-right: 4px; padding-top: 1px; padding-bottom: 1px; float:left'))?> <!--input name="textfield28" size="34" style="font-size: 8pt; border-style: solid; border-width: 1px; padding-left: 4px; padding-right: 4px; padding-top: 1px; padding-bottom: 1px; float:left" /--></td> </tr> <?php echo $form->input('id',array('type'=>'hidden','value'=>$_SESSION['userid']));?> <tr> <td width="140"> </td> <td width="598"> <p style="margin-left: 10px; margin-top: 5px; margin-bottom: 5px" align="left"> <?php echo $form->submit('/img/adm/submit.gif'); ?> <!--img border="0" src="../ima/tit/submit.gif" width="58" height="15"--></td> </tr> </table> <?php echo $form->end();?> </td> </tr> </table> [/code] My admin table has 3 fields: ID,USERNAME AND PASSWORD Quote Link to comment https://forums.phpfreaks.com/topic/170633-change-password-error-in-cakephp/ Share on other sites More sharing options...
jcombs_31 Posted August 17, 2009 Share Posted August 17, 2009 From what I can see, you are doing a few things wrong. First, you are creating a form with the 'Employer' model but then using an Admin model data. I suspect this is your problem right there. I would use saveField rather than the save function. That form is a mess and hard to read with all that inline styles and tables. You should really clean up your code and create a stylesheet. You should be doing your validation in the model, not the controller. You can create a custom function to see if 2 fields match. Quote Link to comment https://forums.phpfreaks.com/topic/170633-change-password-error-in-cakephp/#findComment-900426 Share on other sites More sharing options...
sonoton345 Posted August 17, 2009 Author Share Posted August 17, 2009 hmm..ok..I can do everything else with admin (add employer,delete employer etc) except changing admin password. I kind of "inherited" the code from a previous programmer working on the site but all else seem to work in admin even though there are some functions in Employer Controller and some functions in Jobseeker Controller. So are you suggesting creating an admin controller? Quote Link to comment https://forums.phpfreaks.com/topic/170633-change-password-error-in-cakephp/#findComment-900436 Share on other sites More sharing options...
jcombs_31 Posted August 17, 2009 Share Posted August 17, 2009 I would re-read what I wrote. <?php echo $form->create('Employer',array('action'=>'changepassword')); ?> Is changepassword a function of Employer or Admin Quote Link to comment https://forums.phpfreaks.com/topic/170633-change-password-error-in-cakephp/#findComment-900449 Share on other sites More sharing options...
sonoton345 Posted August 17, 2009 Author Share Posted August 17, 2009 It's a function for admin to change his/her password. Quote Link to comment https://forums.phpfreaks.com/topic/170633-change-password-error-in-cakephp/#findComment-900450 Share on other sites More sharing options...
jcombs_31 Posted August 19, 2009 Share Posted August 19, 2009 It's a function for admin to change his/her password. I understand what it does. But when you call the form help you are calling it with the wrong model. You should be using $form->create('Admin', array(...)); ?> While I suppose you could do it by specifying Admin.variable for the form fields, it doesn't make sense, and if for some reason an associate is not defined correctly it is not going to work correctly. Since all your data belongs to the Admin model, that is what you should be specifying for the form creation. Second, if debug is set to 2, you should be able to see the queries that cake is running at the bottom of your screen. I would take a look there to see if that leads to the answer. Quote Link to comment https://forums.phpfreaks.com/topic/170633-change-password-error-in-cakephp/#findComment-901748 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.