I create form using the following code:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'contacts-form',
'enableAjaxValidation'=>false,
)); ?>
Is there any way to submit the form by AJAX? Remember not I am not talking about AJAX validation.
One way to do this is to use the built in ajaxSubmitButton helper, like so:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'contacts-form',
'enableAjaxValidation'=>false,
)); ?>
<!-- your form elements here -->
<?php echo CHtml::ajaxSubmitButton(Yii::t('app', 'Submit')); ?>
<?php $this->endWidget(); ?>
This automatically binds a jQuery AJAX call to the SUbmit button which will POST the form values to your from's action URL.
You could also write the AJAX code yourself, of course, but Yii has this helper function too.
This helper ajaxSubmitButton function isn't terribly useful, especially since it uses jquery.ajax() without using the "Promise interface" in jquery 1.5+, so you have to process the response through the success callback. This would have been cleaner if they just used jquery.submit(). You're better off just rolling your own, honestly.
I'm disappointed that none of the (relatively popular) Bootstrap integrations like Yii-Bootstrap or YiiBooster offer much of anything in terms of generating forms that refresh themselves with ajax response data either (I'm not talking validation). I go through all the trouble of learning & adopting a framework, only to end up html/js/css-coding my own frontend presentation and logic anyway... Oh well.
I'm still a Yii enthusiast, mostly due to their gii generator and support for quasi-mixin patterns with "behaviors". It leads to clean code on the backend, but the framework has some growing to do on the frontend & view rendering. Their "CHtml" library really isn't cutting it right now.
Related
I have app in Yii framework and i want to add Captcha in it and i don't want use Yii defaults .. long story short , i found one which works fine without framework so i created an action and i put image creation function in it, but when i call that action i get broken link.
yii/mycontroller/createcaptchaimage
What is wrong in my solution ?
the captha code is in this link .
There is nothing wrong in your solution. You Obviously have some Routing issue . Did you checked your main.php or .htaccess ?
I found this captcha-extended extension more beautiful than CCapthcha. Here is the link:
http://www.yiiframework.com/extension/captcha-extended/
You call it just like normal CCaptcha widget from any view/layout file:
<?php $this->widget('CCaptcha'); ?>
This will look for controller action "captcha" which is defined in array of actions.
public function actions(){
return array(
'captcha'=>array(
'class'=>'CaptchaExtendedAction',
// if needed, modify settings
'mode'=>CaptchaExtendedAction::MODE_MATH,
),
);
}
if append to page the CGridView ('myGridViewID') by ajax, I can't reload it.
$.fn.yiiGridView.update('myGridViewID');
TypeError: settings is undefined
$grid.addClass(settings.loadingClass);
Use renderPartial in controller action:
$cs = Yii::app()->clientScript;
$cs->reset();
$cs->scriptMap = array(
'jquery.js' => false, // prevent produce jquery.js in additional javascript data
);
// Look at 4th parameter: with TRUE value, your view will have additional javascript data.
$this->renderPartial('_partialViewWithGrid', array(), false, true);
Here is a wiki for dynamic CgridViews in the same view. That should work.
The problem with dynamically loaded CGridViews (and everything containing ajax) is that CController::renderPartial() does not render the required javascript code for them to work properly, unlike CController::render(), which includes the required layout and JS.
There is an extension called ZController which offers a workaround for this problem, but due to the way CGridviews are reloaded (by making an ajax call to the same URL)... when you try to filter/sort/page a CGridView loaded via AJAX, the subsequent Ajax call will replace the whole contents of your browser window, but I honestly think that maybe (only maybe) this workaround could help, but I haven't had the time to try it out.
That is why I currently avoid loading CGridViews using AJAX.
i am using a third party plugin with yii, it provides chat functionality, it has its own DB and php files that provide the functionality,
now i am want to use it in the view, but the simple include statements are not working, do i need to convert it to yii or can i use it as is?
<?php
session_start();
// Load MySQL DB settings
include_once('config.inc.php');
$_SESSION['username'] = 'Currently logged in users's username from database';
$_SESSION['user_id'] = 'Currently logged in user's id';
?>
//That's it! To print online users, you need to do it like this:
<?php
$users = mysql_query("SELECT id,username FROM ".$sql_table_users." WHERE chat_status='online' AND id!='".$_SESSION['user_id']."'");
if(mysql_num_rows($users) > 0){
while($user = mysql_fetch_assoc($users)){
print ''.$user['username'].'<br />';
}
}
?>
this is the interface plugin has provided for me.
plugin location is /assets/plugin.
i cant use direct php query commands to another Database, which i want to keep seperate from mine, plus the js file that comes with the plugin calls the script with wrong URL parameters, so what is the best method to incorperate this into my yii app. thanks
You should create a Yii extension that'll wrap your plugin.
Then in your view you'll have to call a widget that'll display your chat.
I think this is the best way to do it because using this all your call to the plugin will be performed with the yii strucutre and philosophy. Only your extension will be structured using the chat philosophy.
Source about creating widgets
I have tried asking this question in a number of ways, and I still can't get an answer. In Asp.net MVC4, is there a way I can just add jquery code to my views and not have to add any kind of annotation to a model to validate my form input? I just realized that I am using Ajax.BeginForm... I am betting that I cannot using regular Jquery Ajax calls with that on my form. I bet if I use HTML.beginForm, regular jquery will work. But now that will break my ajax calls... Which were failing for some reason. Well, I am about to find out why. Hopefully I can figure out how to just avoid using all Asp.net Ajax crap. It has given me nothing but a massive headache. Oh wait, you know what, I just looked at another view, and there I am using Html.BeginForm and I still can't use plain jquery code in my views to validate my form. Is this even possible in MVC4?
of course this is possible - ASP.NET MVC just emits HTML yes? so just add some jquery validate code to the document ready.
$(function(){
$('#myform').validate(/* options here */);
});
This is my scenario. A home page with a upload form, user upload the images and submit, then the controller will catch the data from user, add it in the Model. Finally the image info will display in the another page.
I put the actionIndex() in the siteController to render the home page, the actionUpload() in the same file to handle the data user. So in the view, what should I put in the form action to invoke the actionUpload(). I think the flow is quite weird in Yii when I read the blog demo code, I just follow same way with the ASP.NET MVC. Suggest me the right way, plz. Thanks
Depends on how you build your form. If using CHtml, do the following:
<?= CHtml::beginForm($this->createUrl('site/upload'))?>
If you have a model that sits behind it:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'action' => $this->createUrl('site/upload'),
)); ?>
Please check the Yii documentation and examples on how to properly set up forms. You have several choices here.