Load Specific Form Fields Upon Radio Button Selection - ajax

I am a fairly new user of Yii.. Have been using it for less than 2 months..
I am trying to do a registration form that upon loading, it only shows 2 radio buttons(say choice 1 is Person, choice 2 is Organization), when the user selects a choice i want to dynamically display the fields corresponding to his selection using ajax.
Note: I am using the yii-user extension, and I am intending to use the same table to store both Person & Organization users, since they only differ in 3 attributes.
I have been stuck with this problem for 2 days and am not able to solve it..
This is a snippet of my code:
registration.php (view)
<?php $this->pageTitle=Yii::app()->name . ' - '.UserModule::t("Registration");
$this->breadcrumbs=array(
UserModule::t("Registration"),
);
?>
<h1><?php echo UserModule::t("Registration"); ?></h1>
<?php if(Yii::app()->user->hasFlash('registration')): ?>
<div class="success">
<?php echo Yii::app()->user->getFlash('registration'); ?>
</div>
<?php else: ?>
<div class="form">
<?php $form=$this->beginWidget('UActiveForm', array(
'id'=>'registration-form',
'enableAjaxValidation'=>true,
'disableAjaxValidationAttributes'=>array('RegistrationForm_verifyCode'),
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
'htmlOptions' => array('enctype'=>'multipart/form-data'),
)); ?>
<p class="note"><?php echo UserModule::t('Fields with <span class="required">*</span> are required.'); ?></p>
<?php echo $form->errorSummary(array($model,$profile)); ?>
<div class="row">
<?php echo $form->radioButtonList($model, 'profile_type', array(0=>'Person',1=>'Company'),array('separator'=>'', 'labelOptions'=>array('style'=>'display:inline'), 'onclick' =>CHtml::ajax(array("type"=>"GET", "url"=>array("registration/add"), "data"=>array('test'=>'js:this.value'), "update"=>"#test")))); ?>
</div>
RegistrationController.php
public function actionAdd() {
if(Yii::app()->request->isAjaxRequest){
$this->renderPartial("/user/test",array('model'=>$model),false,true);
Yii::app()->end();
}
}
test.php
<?php
$val = $_GET['test'];
if($val == 1){
echo "yes";
//nothing added yet
}
else{
echo "no";
?>
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username'); ?>
<?php echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'email'); ?>
<?php echo $form->textField($model,'email'); ?>
<?php echo $form->error($model,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password'); ?>
<?php echo $form->error($model,'password'); ?>
<p class="hint">
<?php echo UserModule::t("Minimal password length 4 symbols."); ?>
</p>
</div>
<div class="row">
<?php echo $form->labelEx($model,'verifyPassword'); ?>
<?php echo $form->passwordField($model,'verifyPassword'); ?>
<?php echo $form->error($model,'verifyPassword'); ?>
</div>
<?php
}
?>
The erros am recieving when I select Person (choice 0) is the following:
Fatal error: Call to a member function labelEx() on a non-object in test.php
Any help is much appreciated!

The smoking gun is the error Fatal error: Call to a member function labelEx() on a non-object in test.php - it's telling you that labelEx() is not a method. Looking at your view.php, you can see labelEx() is called as a method of $form.
The issue is that when the view test.php is being rendered, it has no idea about the $form you've created in registration.php. test.php only knows about parameters you've passed to it (in this case, only $model).
Instead of using $form->method(), just use CActiveForm::method in your view, e.g.:
<div class="row">
<?php
echo CActiveForm::labelEx($model,'username')
echo CActiveForm::textField($model,'username');
echo CActiveForm::error($model,'username');
?>
</div>
I also combined your consecutive PHP blocks into a single block for easier reading/parsing/etc.

Related

Check duplicate data in CodeIgniter result

I have a small forum and want to show the last 10 reply's.
My problem:
I get some duplicate entries from the database because there have some replies in one post. How I can disable all duplicate posts and show only one from all posts. I try to say, if more then 1 replys with the same forum_post_id > show only the last.
My View:
<?php foreach ($forum_reply as $item): ?>
<?php $standing = $item->forum_post_id ?>
<div class="col-lg-12 robie" onClick="location.href='<?= site_url('viewtopic/index/' . $item->forum_post_id) ?>'">
<div class="catcenter55 row">
<div class="col-md-3 deta grew">
<?php echo $this->db->where('forum_post_id', $item->forum_post_id)->get('forum_post')->row()->post_head; ?>
</div>
<div class="col-md-2 deta">
<?php $topcat = $this->db->where('forum_post_id', $item->forum_post_id)->get('forum_post')->row()->forum_cat_id ;?>
<?php echo $this->db->where('forum_cats2_id', $topcat)->get('forum_cats2')->row()->forum_name; ?>
</div>
<div class="col-md-2 deta">
<?php echo $this->db->where('forum_post_id', $item->forum_post_id)->get('forum_post')->row()->views; ?>
</div>
<div class="col-md-2 deta">
<div class="likebg2">+<?php echo $this->db->where('forum_post_id', $item->forum_post_id)->get('forum_post')->row()->likes; ?></div>
</div>
<div class="col-md-3 deta">
<?php foreach ($this->db->order_by('timestamp', 'DESC')->get('forum_reply')->result() as $stan): ?>
<?php if($standing == $stan->forum_post_id): ?>
<?php echo $this->db->where('user_id', $stan->user_id)->get('users')->row()->username; ?> | <?php echo $this->db->order_by('timestamp', 'DESC')->where('forum_reply_id', $stan->forum_reply_id)->get('forum_reply')->row()->timestamp; ?>
<?php break; ?>
<?php endif; ?>
<?php endforeach; ?>
</div>
</div>
</div>
<?php endforeach; ?>
My Controller
public function index() {
$data['forum_reply'] = $this->db->get('forum_reply')->result();
}
My result
Managed to get what I think you are trying to achieve by using a group by i.e.
SELECT * FROM posts GROUP BY forumId;
However this makes things more complicated when ordering the posts, this is a bit hacky but have a look at this:
SELECT *
FROM (
SELECT *
FROM posts
ORDER BY date DESC
LIMIT 18446744073709551615
) AS sub
GROUP BY sub.forumId
ORDER BY id DESC
For reference regarding the sub query look at this previous question and this post
Thank you Jake,
I was change
$data['forum_reply'] = $this->db->get('forum_reply')->result();
to
$data['forum_reply'] = $this->db->group_by('forum_post_id')->order_by('timestamp', 'DESC')->get('forum_reply')->result();
and it´s working.

Magento: Show messages inside the content area in checkout page

I have a problem showing error messages in the content section of the checkout page.
I want to load the messages directly from the checkout/cart.phtml page instead of page/1column.phtml so that I can put it directly near the checkout options
this is the code in my checkout/cart.phtml
<div class="cart-collaterals">
<div class="col2-set">
<div class="col-1">
<?php echo $this->getMessagesBlock()->toHtml() ?>
<?php echo $this->getChildHtml('crosssell') ?>
</div>
<div class="col-2">
<?php /* Extensions placeholder */ ?>
<?php echo $this->getChildHtml('checkout.cart.extra') ?>
<?php echo $this->getChildHtml('coupon') ?>
<?php if (!$this->getIsVirtual()): echo $this->getChildHtml('shipping'); endif; ?>
</div>
</div>
Using this I can display how I want the success text (such as "Your product has been added to the cart") but when I have an error that tells me that I have to change quantities in the cart it doesn't load the template from checkout/cart.phtml but from page/1column.phtml
<div class="main">
<?php echo $this->getChildHtml('breadcrumbs') ?>
<div class="col-main">
<?php echo $this->getChildHtml('global_messages') ?>
<?php echo $this->getChildHtml('content') ?>
</div>
</div>
What I need to do is to load the <?php echo $this->getMessagesBlock()->toHtml() ?> directly from checkout/cart.phtml

ajax calling another ajax in yii

I can access the registration form which appears in a model window via ajax on any page as coded in the menu layout .
I need some thing on click of submit button of registration form it should get display a thank you message which is also via ajaxin the same div as that of wherein the registration form appears
registratiincontrooler
if ($model->save()) {echo "thank you for registration";return; }
registration view
<?php
/* #var $this UserProfileController */
/* #var $model UserProfile */
$this->breadcrumbs=array(
'User Profiles'=>array('index'),
'Create',
);
?>
<div id="ajax101" >
<div class="form" >
<?php $form=$this->beginWidget('UActiveForm', array(
'id'=>'registration-form',
'enableAjaxValidation'=>true,
'disableAjaxValidationAttributes'=>array('RegistrationForm_verifyCode'),
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
'htmlOptions' => array('enctype'=>'multipart/form-data'),
)); ?>
<p class="note"><?php echo UserModule::t('Fields with <span class="required">*</span> are required.'); ?></p>
<?php echo $form->errorSummary(array($model/*,$profile*/)); ?>
<div class="row">
<div class="rlabel">
<?php echo $form->labelEx($model,'username', array("style"=>"display:inline")); ?>
</div>
<div class="rtextfield">
<?php echo $form->textField($model,'username', array("style"=>"margin-left:43px")); ?>
</div>
<div class="rerror">
<?php echo $form->error($model,'username', array("style"=>"margin-left:113px")); ?>
</div>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password', array("style"=>"display:inline")); ?>
<?php echo $form->passwordField($model,'password', array("style"=>"margin-left:43px")); ?>
<?php echo $form->error($model,'password', array("style"=>"margin-left:113px")); ?>
<p class="hint">
<?php echo UserModule::t("Minimal password length 4 symbols."); ?>
</p>
</div>
<div class="row">
<?php echo $form->labelEx($model,'verifyPassword', array("style"=>"display:inline")); ?>
<?php echo $form->passwordField($model,'verifyPassword'); ?>
<?php echo $form->error($model,'verifyPassword', array("style"=>"margin-left:113px")); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'email', array("style"=>"display:inline")); ?>
<?php echo $form->textField($model,'email', array("style"=>"margin-left:65px")); ?>
<?php echo $form->error($model,'email', array("style"=>"margin-left:113px")); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'user_type', array("style"=>"display:inline")); ?>
<?php echo $form->dropDownList($model,'user_type',$model->getUType()); ?>
<?php echo $form->error($model,'user_type', array("style"=>"margin-left:113px")); ?>
</div>
<?php if (UserModule::doCaptcha('registration')): ?>
<div class="row">
<?php echo $form->labelEx($model,'verifyCode'); ?>
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->textField($model,'verifyCode'); ?>
<?php echo $form->error($model,'verifyCode'); ?>
<p class="hint"><?php echo UserModule::t("Please enter the letters as they are shown in the image above."); ?>
<br/><?php echo UserModule::t("Letters are not case-sensitive."); ?></p>
</div>
<?php endif; ?>
<div class="row submit">
<?php echo CHtml::ajaxSubmitButton(UserModule::t("Register"),array('/user/register'),array('update'=>'#ajax101')); ?>//div for form
</div>
<?php $this->endWidget(); ?>
</div>
</div><!-- form -->
<?php endif; ?>
I think the problem exists here
<?php echo CHtml::ajaxSubmitButton(UserModule::t("Register"),array('/user/register'),array('update'=>'form')); ?>//div for form
</div>
example if I have 3 menu items
home
aboutus
contact
suppose visited home when I click on register a model window with registration form appears and when i click on submit button model->save as well make another call using ajax to update that model window with thank you message
similarly with other items in menu
whats happening is it saves and redirects to /user/registration with thank you but i need same model window and not to be updated with thank message and not redirected thank message
I did try this but it did not save
<?php echo CHtml::ajaxSubmitButton(UserModule::t("Register"),'',array('update'=>'form-content')); ?>
I am calling ajax within ajax .
try with
<?php
echo CHtml::ajaxSubmitButton(signup , CHtml::normalizeUrl(array('<controller>/<action>', 'render' => true)), array(
'dataType' => 'json',
'type' => 'post',
'success' => 'function(data) {
}',
'beforeSend' => 'function(){
}','complete' => 'function(){
}',
), array('id' => 'signup', 'class' => ''));
?>
The problem is in your controller, you have to stop Yii, return; alone won't cut it.
if ($model->save()) {
if($this->isAjaxRequest)
{
ehco 'thank you for registration';
Yii::app()->end(); // to stop yii
}
else
{
// probably redirect
}
}

Display specific category products on no results search page

When we search for a wrong keyword like "sdfsdf" in magento site, it displays that "your search returns no results". Here I want to display any category products like "similar products" category as we display "best sellers" on home page. I have tried by calling the block in the catalogsearch.xml. BUt catalogsearch.xml doent contain any block for no results. So how can I display any category products on no results page.
I have an idea like can we display a specific category products on .phtml page? If we can display specific category of products then we can call that category from "result.phtml". Any help?
my result.phtml
<?php if($this->getResultCount()): ?>
<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
<div class="page-title category-title">
<?php if ($this->helper('rss/catalog')->getTagFeedUrl()): ?>
<?php echo $this->__('Subscribe to Feed') ?>
<?php endif; ?>
<h1><?php echo ($this->getHeaderText() || $this->getHeaderText() === false) ? $this->getHeaderText() : $this->__("Search results for '%s'", $this->helper('catalogsearch')->getEscapedQueryText()) ?></h1>
</div>
<?php if ($messages = $this->getNoteMessages()):?>
<p class="note-msg">
<?php foreach ($messages as $message):?>
<?php echo $message?><br />
<?php endforeach;?>
</p>
<?php endif; ?>
<?php echo $this->getProductListHtml() ?>
<?php else: ?>
<div class="page-title category-title">
<h1><?php echo ($this->getHeaderText() || $this->getHeaderText() === false) ? $this->getHeaderText() : $this->__("Search results for '%s'", $this->helper('catalogsearch')->getEscapedQueryText()) ?></h1>
</div>
<p class="note-msg">
<?php echo ($this->getNoResultText()) ? $this->getNoResultText() : $this->__('Your search returns no results.') ?>
<?php if ($messages = $this->getNoteMessages()):?>
<?php foreach ($messages as $message):?>
<br /><?php echo $message?>
<?php endforeach;?>
<?php endif; ?>
</p>
<div class="search-noresults">
<h1>Meanwhile, You may go through our featured categories:</h1>
</div>
<?php echo $this->getLayout()->CreateBlock('catalog/product_list')->setCategoryId(18)->setTemplate('catalog/product/list.phtml')->toHtml();?>
<?php endif; ?>
A simple solution is within catalogsearch/result.phtml template you would need to setup what happens when there are no results
<?php if($this->getResultCount()): ?>
SHOW RESULTS
LEAVE DEFAULT
<?php else: ?>
NO RESULTS
<?php echo $this->getLayout()->createBlock('catalog/product_list')->setCategoryId(4)->setTemplate('catalog/product/list.phtml')->toHtml() ?>
<?php endif; ?>

Extrafields Visibility. K2 Extension

So i have this code changed in ../html/com_k2/template/default/item.php:
<?php if($this->item->params->get('itemExtraFields') && count($this->item->extra_fields)): ?>
<!-- Item extra fields -->
<div class="itemExtraFields">
<h3><?php echo JText::_('Additional Info:'); ?></h3>
<ul>
<?php foreach ($this->item->extra_fields as $key=>$extraField):?>
<?php $user =& JFactory::getUser(); ?>
<?php if($extraField->name == "Price" && $user->get('Guest') ==1): ?>
<?php else: ?>
<li class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
<span class="itemExtraFieldsLabel"><?php echo $extraField->name; ?>:</span>
<span class="itemExtraFieldsValue"><?php echo ($extraField->type=='date')?JHTML::_('date', $extraField->value, JText::_('K2_DATE_FORMAT_LC')):$extraField->value; ?></span>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<div class="clr"></div>
</div>
<?php endif; ?>
What I'm trying to achieve is to hide the extrafield on Guest Viewer on the front-page, and that extrafield will only be visible to certain User groups. I already tried changing this line three times:
name == "Price" && $user->get('Guest')==1): ?>
name == "itemExtraFields" &&$user->get('Guest')
==1): ?>
name == "itemExtraFieldsValue"
&&$user->get('Guest') ==1): ?>
(I named my custom field as Price)
So i don't know if I'm missing something on the code or i got the itemFields name wrong. Any help would be appreciated a ton. I already asked in the k2 forum and joomla forum but no one is answering.
Try this code. It is working fine.
<?php foreach ($this->item->extra_fields as $key=>$extraField):?>
<?php if($extraField->name === "Price" && $this->user->guest){}
else{ ?>
<li class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
<span class="itemExtraFieldsLabel"><?php echo $extraField->name; ?>:</span>
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
</li>
<?php }?>
<?php endforeach; ?>
I just added a strict comparison to the name and brackets for the if else condition. Also altered the way of checking guest user.

Resources