Concrete5 Securimage - image not refreshing on click - captcha

I'm trying to build my own contact form with Securimage. In my view.php, I have this:
<div>
<img id="captcha_img" src="<?php echo $this->action('securimage'); ?>" />
<script>
$(function() {
$('#captcha_img').click(function(){
$(this).attr('src', '<?php echo $this->action('securimage'); ?>&time=' + (new Date).getTime());
});
});
</script>
</div>
but when I click on it on the image it disappears instead of being reloaded.
My controller.php has this code for the securimage:
public function action_securimage() {
Loader::library('3rdparty/securimage/securimage');
$this->securimage = new Securimage();
$this->securimage->show();
}
What am I doing wrong? Will really appreciate your help. Thank you.

It looks good you just need to make a minor change from:
$(this).attr('src', '<?php echo $this->action('securimage'); ?>&time=' + (new Date).getTime());
to
$(this).attr('src', '<?php echo $this->action('securimage'); ?>?time=' + (new Date).getTime());
You need to change the & to ? to properly add the random value to the query string and not part of the URL.

Related

ClientValidation In Form not showing When I Send Id Variable To Jquery in YII

I Need Help in Yii, i get problem in my form, the problem is if i put id in textfield then client validation not showing for that field but another field no problem. This is my code in form
<div class="row">
<?php echo $form->labelEx($model,'latar_belakang'); ?>
<?php echo $form->textField($model,'latar_belakang',array('id' => 'latarbelakang','class' => 'input-block-level')); ?>
<?php echo $form->error($model,'latar_belakang'); ?>
<?php echo $form->labelEx($model,'rumusan_masalah'); ?>
<?php echo $form->textField($model,'rumusan_masalah',array('id' => 'rumusanmasalah','class' => 'input-block-level')); ?>
<?php echo $form->error($model,'rumusan_masalah'); ?>
</div>
for your knows i use id in textfield for calculaten field with jquery which i put in form . this is my jquery code in form
Yii::app()->clientScript->registerCoreScript('jquery');
Yii::app()->clientScript->registerScript('totalValues',"
function totalValues()
{
var lb = $('#latarbelakang').val() ;
var rm = $('#rumusanmasalah').val();
var tot = parseInt(lb) + parseInt(rm);
$('#nilaiakhir').val( tot );
}
", CClientScript::POS_HEAD);
Yii::app()->clientScript->registerScript('changeTotals', "
$('#latarbelakang').change( function(){ totalValues(); } );
$('#rumusanmasalah').change( function(){ totalValues();} );
", CClientScript::POS_END);
What i must do ? for solving this problem ?.
Thanks B4..
By default Yii generates ids of fields dynamically using model name and field name.
For example for 'password' field of 'User' model the id would be 'User_password'
You can inspect a field(to which you have not given your custom id) with firebug and see the auto generated id. also yii maps error messages with those auto generated ids in response. that is why your error messages are not showing.
what you can do is to try to call that js function with yii generated id.

how to add input value from session using ajaxlink in yii

I am kind of lost on how to do this in my view.
I want to add the quantity. Right now, it adds but how do I get the input value into my ajaxlink?
My controller is using session to add.
<input id="quantity" type="text" value="1" class="span1">
<div id="cart-text">
<?php echo CHtml::ajaxLink(' Add ',
Yii::app()->createUrl('controller/basketAjax',array('id'=>$_GET['id'])),
array('success'=>'function(data){...
controller:
$session=new CHttpSession;
$session->open();
if (!isset(Yii::app()->session['cart']))
{
$quantity = 1;
$session->add('cart',array(
'product_id.'.$id=>array("id"=>$id,
'quantity'=>$quantity)
));
}
else
{
$cart = Yii::app()->session['cart'];
$array = $cart['product_id.'.$id];
if (isset($array)){
$array['quantity']=$array['quantity']+1;
} else {
$t = array('product_id.'.$id=>array("id"=>$id,'quantity'=>$quantity));
array_push($cart,$t);
}
$session->add('cart', $products);
}
you can do this by using keyup function of jquery. First set the id of link as "mylink". Compute the url by using createUrl.
<script type="text/javascript" >
$("#quantity").keyup(function(){
var url=<?php echo Yii::app()->createUrl('controller/basketAjax') ?>;
$("#mylink").attr("href","");
$("mylink").attr("href",url +$(this).val());
});
</script>
Now i explain whats happening above. first i catch the event keyup on input you are using. It will be called each time you press a key on input. Now url=<?php echo Yii::app()->createUrl('controller/basketAjax') ?>; this code is returning you the base url to your action without any parameters being passed.this line $("#mylink").attr("href",""); will set the href of your link to " "( means nothing). Now this line $("mylink").attr("href",url +$(this).val()); is appending the value of the input you are getting from the input.
Remember you have to put the separator in between like
$("mylink").attr("href",url+"/" +"id"+"/"+$(this).val());
Above i have assumed that the href in your case looks like "projectname/index.php/controller/action/id/something". Thats y i have used separators in between but you can customize it according to your needs.
ajaxLink will not work (from what I know), just build it the old fashion way with jquery. Your best solution is actually to put it in a form and submit the form. Something like
<?php
Yii::app()->clientScript->registerCoreScript('yiiactiveform');
$form=$this->beginWidget('CActiveForm', array('action'=>Yii::app()->createUrl('cart/addProduct')));?>
<input type="text" name="quantity" value="1">
<input type="hidden" name="Product_id" value="<?php echo $model->id;?>">
<?php echo CHtml::ajaxSubmitButton('Save',CHtml::normalizeUrl(array('cart/addProduct','render'=>true)),
array(
'dataType'=>'json',
'type'=>'post',
'success'=>'function(data) {
if(data.status=="success"){
$("#formResult").html("form submitted successfully.");
}
else{
$.each(data, function(key, val) {
$("#user-form #"+key+"_em_").text(val);
$("#user-form #"+key+"_em_").show();
});
}
}',
)); ?>
<?php $this->endWidget(); ?><!-- .contact -->
Sorry for the indenting, hard to indent properly here.

i want to show textarea after a specific value chosen from DropDownList

i have an issue .. i have a form that contain a drop down list that have a lot of values
i want when the user choose specific value from drop down a textarea i shown immediately .. i know i will have to use javascript but i dont know how
this is my Code :
<div class="row">
<?php echo $form->labelEx($model,'type'); ?>
<?php echo $form->dropDownList($model,'type',$model->getTypeOptions()); ?>
<?php echo $form->error($model,'type'); ?>
</div>
<div class="row">
<?php echo "<b>Data</b> : <i> Use WhiteSpaces or , to enter values</i> "; ?><br/>
<?php echo $form->textArea($model, 'data', array('rows'=>10, 'cols'=>50)); ?>
<?php echo $form->error($model, 'data'); ?>
</div>
i want this textarea only shown when the user choose value (ex: checkboxtlist from dropdown)
i searched and found that in
$form->dropDownList($model,'type',$model->getTypeOptions());
we can but array parameter inside dropDownList .. but i dont really know what excatly to write in this array
any help ?
Do like below:
<?php echo $form->dropDownList($model,'type',$model->getTypeOptions(),array('id'=>'myDropDown')); ?>
<?php echo $form->textArea($model, 'data', array('rows'=>10, 'cols'=>50,'id'=>'myTextArea')); ?>
Now, What you need is to create an Ajax request.
<script>
$('#myDropDown').on('change', function() {
$.ajax({
url: "<?php echo $this->createUrl('controller/test'); ?>",
dataType: 'html',
type: 'POST',
data: {dropDownValue: $(this).val()},
success: function(data, textStatus, jqXHR) {
$('#myTextArea').val(data);
}‌
});
});
Note that test is a action in your controller.(replace with your own names).
Then, Create the action:
public function actionTest() {
if (Yii::app()->request->isAjaxRequest) {
$dropDownValue = Yii::app()->request->getPost('dropDownValue');
if ($dropDownValue === 'A VALUE YOU WANT TO COMPARE WITH') {
echo 'SOME THING YOU WANT';
}
Yii::app()->end();
}
}
To know more, we say that if value of dropdown changed, call an ajax request in order to send dropdown value to server. Then if our value was what we want, we would do something. Finally in success function which means that ajax successfully returned response we change our textArea's value with what server(test action) sent to us.

Dynamically show required textfield depending on dropdownlist value

Good day. My problem is I have a dropDownlist with three options: A, B, C. What I need to do is to show a textfield depending on what the user chose.
Example: user chose A => it will show textfield AA (which should be required and not empty)
user chose B => it will show textfield BB (which should be required and not empty)
user chose C => it will show textfield CC (which should be required and not empty)
Can anyone please help me? Thank you.
EDIT: It's working now. The only problem now is when an I leave the chosen textfield as blank and it shows the error, the textfield that was shown (the one which is dependent on the value of dropdownlist) disappears or goes back to state display:hidden. This happens right after the 'textfield cannot be blank' error message. The initial chosen value of dropdownList is still there and is still on focus but the textfield disappears.
HERE ARE MY UPDATED CODES:
VIEW:
<div class="row">
<?php echo $form->labelEx($model,'org_type'); ?>
<?php echo $form->dropDownList($model,'org_type', $model::getOrgType(), array('prompt'=>'', 'id'=>'orgType')); ?>
<?php echo $form->error($model,'org_type'); ?>
</div>
<div class="row" style="display:none" id="sec">
<?php echo $form->labelEx($model,'sec_ref'); ?>
<?php echo $form->textField($model,'sec_ref', array('id'=>'secField')); ?>
<?php echo $form->error($model,'sec_ref'); ?>
</div>
<div class="row" style="display:none" id="dti">
<?php echo $form->labelEx($model,'dti_ref'); ?>
<?php echo $form->textField($model,'dti_ref', array('id'=>'dtiField')); ?>
<?php echo $form->error($model,'dti_ref'); ?>
</div>
<div class="row" style="display:none" id="cda">
<?php echo $form->labelEx($model,'cda_ref'); ?>
<?php echo $form->textField($model,'cda_ref', array('id'=>'cdaField')); ?>
<?php echo $form->error($model,'cda_ref'); ?>
</div>
MODEL:
public function addCustomError($attribute, $error) {
$this->customErrors[] = array($attribute, $error);
}
/**
*/
protected function beforeValidate() {
$r = parent::beforeValidate();
if ($this->org_type == 'Single') {//this is the checkbox
$this->validatorList->add(CValidator::createValidator('required',$this,'dti_ref',array()));
}
if ($this->org_type == 'Partnership') {//this is the checkbox
$this->validatorList->add(CValidator::createValidator('required',$this,'sec_ref',array()));
}
if ($this->org_type == 'Corporation') {//this is the checkbox
$this->validatorList->add(CValidator::createValidator('required',$this,'sec_ref',array()));
}
if ($this->org_type == 'Cooperative') {//this is the checkbox
$this->validatorList->add(CValidator::createValidator('required',$this,'cda_ref',array()));
}
foreach ($this->customErrors as $param) {
$this->addError($param[0], $param[1]);
}
return $r;
}
Javascript on View:
<script>
"$('#orgType').change(function(){
if($('#orgType').val() == 'Single')
{
$('#dti').show();
$('#dtiField').addClass('required');
$('#cda').hide();
$('#sec').hide();
}
if(($('#orgType').val() == 'Partnership') || ($('#orgType').val() == 'Corporation'))
{
$('#sec').show();
$('#secField').addClass('required');
$('#dti').hide();
$('#cda').hide();
}
if($('#orgType').val() == 'Cooperative')
{
$('#cda').show();
$('#cdaField').addClass('required');
$('#dti').hide();
$('#sec').hide();
}
return false;
})";
You can try this:
After Every change event of DropDown You can set value to the text Filed ..

validation_errors(), variable,

I want to save errors from validation_errors() in variable, and dispay it in view.
I know how I can pass variable to view pass another view:
in controller:
$data['date'] = 'some_data';
$this->load->view('register/register_open',$data);
in view_open
<?php
$this->load->view('mains/header');
$this->load->view('login/loggin');
$this->load->view('mains/menu');
$this->load->view('left_column/left_column_before');
$this->load->view('left_column/menu_left');
$this->load->view('left_column/left_column');
$this->load->view('center/center_column_before');
$this->load->view('register/register_view');
$this->load->view('center/center_column');
$this->load->view('right_column/right_column_before');
$this->load->view('right_column/right_column');
$this->load->view('mains/footer');
?>
in view:
<?php echo $date; ?>
But I don`t know how can I save validation_errors() in array or variable, and pass then to view page.
Could you help me?
See this manual: http://ellislab.com/codeigniter/user_guide/general/views.html
Assigning variable:
<?php
class Blog extends CI_Controller {
function index()
{
$data['title'] = "My Real Title";
$data['heading'] = "My Real Heading";
$this->load->view('blogview', $data);
}
}
?>
Now using it in view as:
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<h1><?php echo $heading;?></h1>
</body>
</html>
In your case, try this in view:
var_dump($todo_list);
You will understand the data structure and how to process it.