Jump to content
Larry Ullman's Book Forums

Updating Textfield Value From Custom Method Call Through Ajaxbutton


microsoftx
 Share

Recommended Posts

Hello nice people, I'm pretty new to the 'Yii' platform, however, I have searched all over the internet for a solution to my problem as I have had a roadblock on this for 3 days (believe it). I have a created form to receive employee data from user input. I have a 'textField' for password but my intention is to auto-generate this password using a custom method call. This call will initially fill the 'textField' automatically upon page load and subsequently require the use of 'ajaxButton' to re-generate a new password.

 

As it stands, the method is located in my Model, while my controller is set-up to manage the request and 'renderPartial' the targeted view upon the ajax request. Issue is, I have successfully achieved the effect because I can see the auto-generated value appear on a targeted 'div' in my form. However I want this value to appear within the 'textField' marked with a "password" attribute. Any ideas how I can achieve this, a clear and concise implementation will be appreciated as am still new to this MVC approach and not the best with javaScript either. 

 

the code below

 



 

----Model----
---- Employee.php ---
//Method for generating password in Employee Model 
 
...
// for simple character display (less secure default  password)
    function simpleRandomizer($length){
        $randstr = "";
        for($i=0; $i<$length; $i++){
            $randnum = mt_rand(0,61);
            if($randnum < 10){
                $randstr .= chr($randnum+48);
            }else if($randnum < 36){
                $randstr .= chr($randnum+55);
            }else{
                $randstr .= chr($randnum+61);
            }
        }
        return $randstr;
    }    // simpleRandomizer
 
 
--- Controller----
--- EmployeeController.php
 
 
      public function actionRandomize(){
          $model = new Employee();
       // if(isset($_GET['ajax']))
          $randomPassword = $model->simpleRandomizer(8);
 
 
          $this->renderPartial('randomize',array('random'=>$randomPassword));
      }
 
 
--- View -----
--- randomize.php ---
 
 
/* @var $random EmployeeController */
 
 echo CHtml::encode($random);
 
 
--- form ----
--- _form ---
//this code actually gets the value and updates the targeted div ('pass') as desired
<div class="row" >
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->textField($model,'password',array( 'id'=>"passwd", 'name'=>'passwd')); ?>
<?php echo $form->error($model,'password'); ?>
        <?php echo CHtml::ajaxButton('Generate',array('randomize'),array( 'update'=>'#pass', )); ?>
</div>                         <div id='pass'></div>
 
 
// this is the alternate code i was hoping will actually target the textField ('password') and change its value
// as desired
  <div class="row" >
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->textField($model,'password',array( 'id'=>"passwd", 'name'=>'passwd')); ?>
<?php echo $form->error($model,'password'); ?>
        <?php echo CHtml::ajaxButton('Generate',array('randomize'),array( 'update'=>'#pass',              
                   'data'=>array('password'=>'js: { $(\'#passwd\').val()}'))); ?>
</div>                         <div id='pass'></div>

 


Link to comment
Share on other sites

Well why don't you view the page source and get the id of the text field you wish to add the value to, id's are usually preset automatically with Yii for forum elements. Or if you wished you could just use html options array and add it to the textfield and add an id in separately. You will need this Id so you can reference the input and add the value in with JQuery, you could use raw javascript if you wished (There are 1000's of scenario's if you search on google for every situation you require). This seems to be fairly simple to me unless there is something i am misunderstanding. I have loads of ajax calls in my scripts in Yii and they all work successfully, sometimes i work the Yii way, sometimes i don't it really depends on the situation.

Link to comment
Share on other sites

I have seen some generated password programs before but usually they display a random password in a HTML division block, then if you wish to select the password you can copy and paste it to somewhere safe followed by clicking on a button to fill in a password input (not a text). But from own experience a feature like this only appeared on the admin side of the website. Nice comment about becoming a painter, you have a sense of humor. Seriously though about my CGridView thread there must be something behind that error if i do figure it out i will update the thread.

Link to comment
Share on other sites

 Share

×
×
  • Create New...