hello im so confused with my problem
the problem is the ajax didnt update the value
screenshot
while i choose kegiatan ajax didnt react/update.
idk were the error
please help me
this my controler code
public function actionDynamiclocation()
{
$data=Kegiatan::model()->findAll('nid=:nid',
array(':nid'=>(int) $_POST['nid']));
$data=CHtml::listData($data,'nid','lokasi','pengajuan','realisasi');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
public function actionAjaxCreate() {$model=new Reportbean;
$outlets = Kegiatan::model()->findAll('nid=:nid',
array(':nid'=>(int) $_POST['nid']));
$model->lokasi=0;
$model->pengajuan=1;
$model->realisasi=2;
$this->renderPartial('_keg ', array('model'=>$model),false,true);
}
and view code
<div class="row">
<?php echo $form->labelEx($model,'kegiatan'); ?>
<?php echo $form->DropDownList($model,'kegiatan',
array(),
array(
'empty'=>'--pilih--',
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('Reportbean/ajaxCreate'), //url to call.
'data'=>array('nid'=>'js:this.value'),
'update'=>'.outlet',
//'update'=>'#'.CHtml::activeId($model,'lokasi'), //selector to update
//'data'=>array('nid'=>'js:this.value'),
))); ?>
<div class="outlet">
<?php
$this->renderPartial('_keg', array('model'=>$model),false,true);
?>
</div>
<?php
$this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'id'=>'keg',
'attributes'=>array(
'lokasi',
'pengajuan',
'realisasi',
),
));
?>
Related
I am trying to get users to fill in much information so I need more than one form and page. I made a main_view.php which has a side bar on the left with links to sub1.php, sub2.php, sub3.php. On the right half of main_view.php, it displays the sub pages with corresponding forms. Part of the main_view.php looks like this:
<?php $view_path = "../../application/views/"?>
<span id="theFormChanger" >
<?php
?>
</span>
var currentPage = 0;
var subviews = ['sub1.php', 'sub2.php','sub3.php'];
$('#sub1').click(function(){
currentPage = 1;
$('#theFormChanger').load(viewpath + subviews[currentPage]);
});
Part of the code of sub view pages:
<?php echo form_open('v_controller'); ?>
<?php echo form_input(array( 'type' => 'text', 'id' => 'demail', 'name' =>'demail')); ?>
<?php echo form_input(array( 'type' => 'text', 'id' => 'dname', 'name' => 'dname')); ?>
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
<?php echo form_close(); ?>
For ../application/controllers/,there is a v_controller.php:
function __construct() {
parent::__construct();
}
public function index()
{
$this->load->helper('form');
$this->load->view('sub1');
$data = array(
'User_Name' => $this->input->post('dname'),
'User_Email' => $this->input->post('demail'));
}?>
Every time when I go to localhost:8000/main/main_view, the left part is fine but the right part says "Fatal error: Call to undefined function form_open() in main_view.php"
I searched around but couldn't find answers. I made sure everything is loaded in autoload.php.
Is this a routing problem? I can't directly go to view files? Please help me. Thank you!
You can load views on to a view file like so
application > views > default.php
<?php $this->load->view('template/common/header');?>
<?php $this->load->view('template/common/navbar');?>
<?php $this->load->view('template/' . $page);?>
<?php $this->load->view('template/common/footer');?>
And then on controller
<?php
class Example extends CI_Controller {
public function index() {
$data['page'] = 'common/example';
$this->load->view('default', $data);
}
}
you need to create NameOfController/NameOfMethod in the form open method in your view page.
replace our code by this one , it will be work for you.
<?php echo form_open('v_controller/index'); ?>
I have the following view named 'findreseller.php':
<?php
$countries = CHtml::listData(Country::model()->findAll(), 'id', 'name');
echo CHtml::dropdownlist('find_reseller', '', $countries,
array('ajax'=>array(
'type'=>'POST',
'url'=>Yii::app()->getController()->createAbsoluteUrl('/webcare/reseller/loadAjaxData'),
'update' =>'#data',
)
)
);
?>
<div id="data">
<?php $this->renderPartial('_ajaxContent', array('dataProvider'=>$dataProvider))?>
</div>
_ajaxContent just echoes the result, nothing special there...
The dropdown, as you can see is generated with CHtml because I dont't need a form. I just need an onChange event to do something...
As per the code that follows, in '/webcare/reseller/loadAjaxData' I have:
public function actionLoadAjaxData() {
$country = $_POST['find_reseller'];
//do something...
$dataProvider=new CArrayDataProvider($country_reseller);
$this->render('findreseller', array('dataProvider' => $dataProvider));
}
I can tell that I am missing something, but I am not sure what exactly.
Edit
Modified like this:
<?php
//CHtml::form();
$countries = CHtml::listData(Country::model()->findAll(), 'id', 'name');
echo CHtml::dropdownlist('find_reseller', '', $countries,
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('/webcare/reseller/loadAjaxData'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#city_id', //selector to update
'data'=>'js: $(this).val()',
//leave out the data key to pass all form values through
)
)
);
//empty since it will be filled by the other dropdown
echo CHtml::dropDownList('city_id','', array());
//CHtml::endForm();
?>
<div id="data">
<?php $this->renderPartial('_ajaxContent', array('dataProvider'=>$dataProvider))?>
</div>
And now I get:
http://prntscr.com/42wwx6
And I have the following controller action:
public function actionLoadAjaxData() {
$country = $_POST['country_id'];
...
$dataProvider=new CArrayDataProvider($country_reseller);
$data = User::model()->findAll('country_id=:country_id AND reseller=:reseller',
array(':country_id'=>(int) $_POST['country_id'], ':reseller'=>1));
$data=CHtml::listData($data,'city','city');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
$this->render('action_name', array('dataProvider' => $dataProvider));
}
Edit 2
If I write a die in actionLoadAjaxData(), right at the beginning, the method is loaded fine, the action is ok and the server answers 200.
I am trying to setup a new application using the Yii framework that requires a dependent dropdown, so that when a user selects the jobSkillArea, the options for the next dropdown, jobSkillSpecialty, gets loaded using the built-in jQuery methods. I have copied and modified the code from things I found here and the Yii forums, but I am getting nothing, not even in Chrome's javascript console. Can anyone look at this and see where I've gone wrong? Thanks.
Here is the code in my view for the two dropdowns:
<div class="row">
<?php echo $form->labelEx($model,'jobSkillArea'); ?>
<?php
$list = array();
$list = CHtml::listData(validJobSkillAreas::model()->findAll(), 'JobSkillArea', 'JobSkillArea');
echo $form->dropDownList($model, 'jobSkillArea', $list,
array('prompt'=>'--Select Skill Area--'),
array(
'ajax'=>array(
'type'=>'POST',
'data'=>array('jobSkillArea'=>'js:this.value'),
'url'=>CController::createUrl('NewConsFormController/getSkillSpecialty'),
'update'=>'#'.CHtml::activeId($model,'jobSkillSpecialty')
)
)
);
?>
<?php echo $form->error($model,'jobSkillArea'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'jobSkillSpecialty'); ?>
<?php
$list = array();
$list = CHtml::listData(validJobSkillSpecialties::model()->findAll(),'jobSkillSpecialty','jobSkillSpecialty');
echo $form->dropDownList($model, 'jobSkillSpecialty', array(), array('prompt'=>'--Select Skill Specialty--'));
?>
<?php echo $form->error($model,'jobSkillSpecialty'); ?>
</div>
Then below is the code called by the first dropdown from my controller. The first find is to get the ID that links the parent with the child since I am not storing the KeyValue in the end product. The rest is as it came from the forums.
public function actionGetSkillSpecialty() {
$areaID = ValidJobSkillAreas::model()->find('JobSkillArea=:SkillArea',
array(':SkillArea'=>'$_POST[$jobSkillArea]'));
$data=ValidJobSkillSpecialties::model()->findAll('SkillAreaId=:SkillAreaId',
array(':SkillAreaId'=>$areaID->ID));
$list=array();
$list=CHtml::listData($data,'jobSkillSpecialty','jobSkillSpecialty');
echo "<option value=''>--Select Skill Specialty--</option>";
foreach($list as $value=>$jobSkillSpecialty) {
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($jobSkillSpecialty),true);
}
}
The view is a partial render within the _form view because that was the only way I could get the accordion widget to work with the fields I have. This is the accordion code that calls the jobDetails section that contains the two dropdown selection boxes.
<div id="accordion">
<?php
$this->widget('zii.widgets.jui.CJuiAccordion', array(
'panels'=>array(
'Job Details'=>$this->renderPartial('_partial_jobdetails',array('model'=>$model,'this'=>$this,'form'=>$form),true,false),
'Consultant Details'=>$this->renderPartial('_partial_consdetails',array('model'=>$model,'this'=>$this,'form'=>$form),true,false),
'Client Details'=>$this->renderPartial('_partial_clientdetails',array('model'=>$model,'this'=>$this,'form'=>$form),true,false),
'Internal Info'=>$this->renderPartial('_partial_internaldetails',array('model'=>$model,'this'=>$this,'form'=>$form),true,false),
'Form Requirements'=>$this->renderPartial('_partial_formsdetails',array('model'=>$model,'this'=>$this,'form'=>$form),true,false),
'JPMC Details'=>$this->renderPartial('_partial_jpmcdetails',array('model'=>$model,'this'=>$this,'form'=>$form),true,false),
),
// additional javascript options for the accordion plugin
'options'=>array(
'collapsible'=>true,
'active'=>false,
'autoHeight'=>false,
'heightStyle'=>'content',
),
'htmlOptions'=>array(
// HTML options you may need
),
));
?>
</div>
Please try the following code
View
<?php
echo CHtml::dropDownList('region_id','',
CHtml::listData($courses, 'course_id', 'course_name'),
array(
'prompt'=>'Select Region',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('loadcities'),
'update'=>'#city_name',
'data'=>array('region'=>'js:this.value'),
)));
echo CHtml::dropDownList('city_name','', array(), array('prompt'=>'Select City'),
);
?>
=================================================
controller function
public function actionLoadcities()
{
$data=City::model()->findAll('course='.$_POST['region'],
array(':region'=>(int) $_POST['region']));
$data=CHtml::listData($data,'city_id','city_name');
echo "<option value=''>Select City</option>";
foreach($data as $value=>$city_name)
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($city_name),true);
}
I'm facing validation problems integrating my custom module in zfcAdmin and BjyAuthorize.
My form class:
...
$formOptions = $this->settings->getFormSettings();
foreach ($formOptions as $field){
if (isset($field['field']))
$this->add($field['field']);
}
...
My filter class:
$formOptions = $this->settings->getFormSettings();
foreach ($formOptions as $filter){
if (isset($filter['filter']))
$this->add($filter['filter']);
}
...
Fields, filters and other options are retrieved from config file.
Basically everything works fine: form data can be added, edited or deleted from db.
Also after the zfcAdmin module installation no problem rose. Everything works fine using both 'site/mymodule' route and 'site/admin/mymodule' route: i can still add, edit and delete items from db.
Here the problem: I need some form elements (a Select in this particular case) editable/viewable only by administrator. (I can write a new controller/entity class 'ad hoc' for admin but i would like to use the same code for the whole site.)
I installed and configured bjyoungblood/BjyAuthorize module: it allowed me to display some form elements/fields only to admin but when i'm in edit mode a form validation error is displayed: "Value is required and can't be empty"
Here the code:
//view/mymodule/mymodule/update.phtml
<div id="page" style="margin-top: 50px;">
<?php if (isset($this->messages) && count($this->messages) > 0 ): ?>
<?php foreach ($this->messages as $msg): ?>
<div class="alert alert-<?php echo $this->escapeHtmlAttr($msg['type']); ?>">
<?php if (isset($msg['icon'])) echo '<i class="'.$this->escapeHtmlAttr($msg['icon']).'"></i> '; ?><?php echo $this->escapeHtml($msg['message']); ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php
$title = 'Edit Item';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<?php
$form = $this->form;
$form->setAttribute('action', $this->url($this->route . 'mymodule/update', array('action' => 'update', 'id' => $this->id )));
$form->prepare();
$form->setAttribute('method', 'post');
$input = $form->getInputFilter();
?>
<?php echo $this->form()->openTag($form) ?>
<dl class="zend_form">
<?php foreach ($form as $element): ?>
<?php
//CHECK USER PRIVILEDGES
$elName = $element->getName();
$elResource = isset($this->form_options[$elName]['auth']) ? $this->form_options[$elName]['auth']['resource'] : "userresource";
$elPrivilege = isset($this->form_options[$elName]['auth']) ? $this->form_options[$elName]['auth']['privilege'] : "view";
//SHOW THE ELEMENT IF ALLOWED
if($this->isAllowed($elResource, $elPrivilege)):
?>
<?php if ($element->getLabel() != null): ?>
<dt><?php echo $this->formLabel($element) ?></dt>
<?php endif ?>
<?php if ($element instanceof Zend\Form\Element\Button): ?>
<dd><?php echo $this->formButton($element) ?></dd>
<?php elseif ($element instanceof Zend\Form\Element\Select): ?>
<dd><?php echo $this->formSelect($element) . $this->formElementErrors($element) ?></dd>
<?php else: ?>
<dd><?php echo $this->formInput($element) . $this->formElementErrors($element) ?></dd>
<?php endif ?>
<?php else: ?>
<?php
?>
<?php endif ?>
<?php endforeach ?>
</dl>
<?php echo $this->form()->closeTag() ?>
</div>
<div class="clear-both"></div>
My controller action
//controller
public function updateAction(){
$messages = array();
$id = (int)$this->getEvent()->getRouteMatch()->getParam('id');
$form = $this->getServiceLocator()->get('FormItemService');
$itemMapper = $this->getItemMapper();
$item = $itemMapper->findById($id);
$form->bind($item);
$request = $this->getRequest();
if($request->isPost()){
$form->setData($request->getPost());
if ($form->isValid()) {
die('c');//never here
$service = $this->getServiceLocator()->get('mymodule\Service\Item');
if ( $service->save($form->getData()) )
{
$messages[] = array(
'type' => 'success',
'icon' => 'icon-ok-sign',
'message' => 'Your profile has been updated successfully!',
);
}
else
{
$messages[] = array(
'type' => 'error',
'icon' => 'icon-remove-sign',
'message' => 'Profile update failed! See error messages below for more details.',
);
}
}else{
var_dump($form->getMessages());//Value is required and can't be empty
}
}
return array(
'messages' => $messages,
'form' => $form,
'id' => $id,
'form_options' => $this->getServiceLocator()->get('mymodule_module_options')->getFormSettings(),
'route' => $this->checkRoute($this->getEvent()->getRouteMatch()->getmatchedRouteName())
);
}
If user is not allowed to view the resource, the element is not echoed. So $request->getPost() has no value for that form element and an error is returned by isValid().
Has anyone solved a similar problem or can anyone point me to the right direction?
Thanks
The problem is that you don't do any security check in your FormFilter class, where you define your required fields.
The $form->isValid() function checks the posted data against those filter elements. So it's not enough to prevent the 'echo field' in your view, you still need to apply the security check to the filter element.
One other approach would be to make two forms one for the front end and one for the admin. Since the one for the admin will have the same fields plus one extra select field you can make the admin form extends the front end one. E.g.
class myForm
{
public function __construct(...)
{
// add fields and set validators
}
}
and the admin form could be:
class MyAdminForm extends myForm
{
public function __construct(...)
{
parent::__construct(...);
// add the extra field and extra validator
}
}
In that way even if you edit the front end form (or validators) the back end will always be up to date.
Hope this helps :),
Stoyan
I need to understand how to build Ajax request in Yii. I searched on the Yii website and found the following article :
http://www.yiiframework.com/wiki/24/
I wrote the code and I tested it on my localhost ? but for some reason it did not work.
For a first attempt I only wanted to do something simple. I wanted to print the result of another action on my page by using Ajax. The text that I want to be displayed is 'Hi'.
This is how mu code looks like for that action:
view/index
<?php
/* #var $this CurrentController */
$this->breadcrumbs=array(
'Current'=>array('/current'),
'index',
);
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'users-index-form',
'enableAjaxValidation'=>true,
)); ?>
<?php
echo CHtml::dropDownList('country_id','', array(1=>'USA',2=>'France',3=>'Japan'),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('currentController/dynamiccities'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#city_id', //selector to update
//'data'=>'js:javascript statement'
//leave out the data key to pass all form values through
)));
//empty since it will be filled by the other dropdown
echo CHtml::dropDownList('city_id','', array());
?>
<?php $this->endWidget(); ?>
</div><!-- form -->
Controller
<?php
class CurrentController extends Controller
{
public function accessRules()
{
return array(
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update','dynamiccities'),
'users'=>array('#'),
),
);
}
public $country_id;
public function actionIndex()
{
$this->render('index');
}
public function actionDynamiccities() /// Called Ajax
{
echo CHtml::tag('option',
array('value'=>'2'),CHtml::encode('Text'),true);
}
}
Unfortunately I'm not getting the desired result. What I get is:
drowpdown list contains country array.
another drowpdown list but empty ?!
How should I fix my example code so it would work? Can anyone see what I am doing wrong?
echo CHtml::dropDownList('city_id','', array());
use id as
echo CHtml::dropDownList('city_id','', array('id'=>'city_id'));