Add attribute attributes while appending a js file using headscript() - javascript

This is my first time working with php. I was trying to optimize js in a website where I saw headscript() to append js file.
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js');
In this I am trying to add an attribute the script tag appended
$this->headScript()->setAllowArbitraryAttributes(true);
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js',
I also tried
$this->headScript()->setAllowArbitraryAttributes(true);
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js',$attrs = array("async" => "true"))
array("async" => "true"));
php portion in the file
<?php
$this->headScript()->setAllowArbitraryAttributes(true);
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js', $attrs = array("async" => "true"));
?>
I expected output to be
<script async="true" src="/js/image-gallery/jquery.simplemodal.1.4.4.min.js"></script>
Instead I got
<script type="Array" src="/js/image-gallery/jquery.simplemodal.1.4.4.min.js"></script>
How to solve it? I was not able to find any samples where attributes are added via headscript();

You need to pass the attributes as the third argument
$this->headScript()->appendFile(
'/js/image-gallery/jquery.simplemodal.1.4.4.min.js',
null,
array('async' => 'true', 'foo' => 'bar')
);
null here is the 'type' attribute, which will default to text/javascript

Looks like you are using Zend framework. If so please refer HeadScript Helper
You need to pass the attributes as $attrs = array()
appendFile($src, $type = 'text/javascript', $attrs = array())
So your code should be
UPDATED CODE
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js', $type = 'text/javascript', $attrs = array("async" => "true"));
instead of
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js', array("async" => "true"));

Arbitrary attributes are disabled by default. To allow such attributes, you can enable them via the setAllowArbitraryAttributes() method:
$this->headScript()->setAllowArbitraryAttributes(true);
Then
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js', null, array("async" => "true"));

Related

Dynamically add Yii form elements with jQuery

I have a problem integrating Yii and jQuery when trying to dynamically add fields generated by Yii to my form.
Namely, statically I use this code to have a dropdownlist on my form:
echo $form->label($model,Yii::t('candidates', 'Contact type'));
$options = CHtml::listData(HrpContactTypes::model()->findAll(),'id','type');
$ct = array();
$b=1;
for($i=0;$i<=count($options)-1;$i++) {
$ct[$b] = Yii::t('candidates', $options[$b]);
$b++;
}
echo $form->dropDownList($model,'contact_type_id',$ct,array('class'=>'form-control'));
and I use this code to have a text-input on my form:
echo $form->label($model,Yii::t('candidates', 'Contact')); ?>
echo $form->textField($model,'contact', array('required'=>'required', 'class'=>'form-control' ));
Then, I use this code to dynamically add elements to my form:
$('#addContact').click(function() {
$('#row').append(
'<br><div class="col-md-6">contact type</div><div class="col-md-6">contact</div>'
);
return false;
});
MY QUESTION IS: HOW TO DYNAMICALLY ADD YII DROPDOWNLIST AND YII TEXT-INPUT WITH THE HELP OF JQUERY?
At first please pay attention on coding.
Second parameter of CActiveForm::label() must be attribute, and translate it you must in your model class (overriding method attributeLabels()), not in form:
public functions attributeLabels() {
return array(
'candidates' => Yii::t('candidates', 'Contact type'),
ATTRUBUTE_NAME => ATTRIBUTE_LABEL
);
}
Read about loops in php. Instead of declaring additional varaible $b you can use loop varaible $i, also you get values from $optios starting from 1, so your last element of $ct always null, I think you want to write this:
$options = CHtml::listData(HrpContactTypes::model()->findAll(),'id','type');
$ct = array();
for($i=0; $i<count($options); $i++) {
$ct[$i+1] = Yii::t('candidates', $options[$i]);
}
Thanks.

How to pass extra data content on yii activeform's ajax parameters?

I am trying to set up a ajax request when radio button is clicked. Here is my code that I am trying for.
<?php echo $form->dropDownListRow($model, 'page', $pages, array('class' => 'span5', 'prompt' => '-- page --')); ?>
<?php echo $form->radioButtonListInlineRow($model, 'for', array('desktop'=>'Desktop', 'mobile'=>'Mobile'), array('ajax' => array(
'type'=>'POST',
'url'=>Yii::app()->createUrl('/admin/adv/loadpositions'), //or $this->createUrl('loadcities') if '$this' extends CController
'update'=>'#Adv_position', //or 'success' => 'function(data){...handle the data in the way you want...}',
'data'=>array('for'=>'js:this.value', 'page' => 'XXXXXXXX'),
)));
?>
I just want to pass its's value as well as it's upper field's value to the ajax action. How can I make it come true. 'data'=>array('for'=>'js:this.value', 'page' => 'XXXXXXXX'),.
Thanks in advance.
$(this.form).serialize()
Will give you the form data. Try something like:
<?php
echo $form->radioButtonList($model, 'for', array('desktop'=>'Desktop', 'mobile'=>'Mobile'), array('ajax' => array(
'type'=>'POST',
'url'=>Yii::app()->createUrl('/admin/adv/loadpositions'),
'data'=>array('data'=>'js:$(this.form).serialize()'),
)));
?>
Update:
You can easily parse this serialized string using PHP function parse_str
<?php
$data = 'str1=value1&str2=value2';
parse_str($data);
echo $str1; //value1
echo $str2; //value2
Just change your data attribute to something like this:
'data'=>array('for'=>'js:this.value', 'page' => 'js:$("#someId").val()'),

form method post and jquery

I'm doing a cakephp form in my view and clicking on the button, I would like to evaluate values before making a certain action, the problem is not me, in another view I have exactly the same code and if it works, but here , do not. However, I do not have any error console.
echo $this->Form->create('Detalle', array('class'=>'form_center', 'type'=>'file','id'=>'detalle_form'));
echo $this->Form->input('FechaEvento', array('type'=>'hidden','value'=>$fecha_evento));
if($SuperAdmin and !$evento['Evento']['gratuito']){
echo $this->Form->input('Inscripcion.cortesia', array('type'=>'checkbox', 'value'=>1, 'label' => utf8_encode('Cortesía'), 'div' => 'input checkbox inputRutPasaporte'));
echo $this->Form->input('Inscripcion.boleta_no_generar', array('type' => 'hidden', 'value' => 0));
}
$options_btn = array('label' => __('Pagar'), 'class' => 'btn_blue', 'div' => false);
echo $this->Form->end($options_btn);
javascript :
<script type="text/javascript">
$(document).ready(function(){
<? if($evento['Evento']['generar_boleta']){ ?>
<? if($evento['Evento']['habilitar_compra_multiple']){ ?>
$('#detalle_form').data('callback', function(form){
alert("TEST");
});
<? } ?>
<? } ?>
});
</script>
The code looks fine but the point is we need information on your control flow. I assume there are missing variables due to it being different.
I advise you to
combine the code into a CakePHP element that is called in the view so you write the code only once (keeping your code DRY)
set DebugLevel to 2
install CakePHP DebugKit DebugKit on Github
check for missing variables and errors
if the problem persists, give us more information accordingly

jQuery autocomplete from php-json without database

I want to make autocomplete on my form with jQuery. The source taken from json data from php, but i don't use database.
Here is my code :
facebookfriends.php
<?php
include('facebookdata.php');
$user_friend = $user_friends['data'];
$json_friends = json_encode($user_friend);
echo $json_friends;
?>
Script
$(function() {
$( "#search" ).autocomplete(
{
source:'facebookfriends.php',
});
});
JSON DATA
[{"name":"Indiarto Priadi","id":"502163984"},
{"name":"Agustin Kertawijaya","id":"511990261"},
{"name":"Jecklyne Christin L","id":"528197912"},
{"name":"Jazi Eko Istiyanto","id":"531149275"},
{"name":"Esritha Situmorang","id":"535864892"},
{"name":"Christy Margaretha Siregar","id":"543468540"},
{"name":"Daniel Oscar Baskoro","id":"549332828"},
........]
I just want to display the name in autocomplete to ensure that the autocomplete works well. But it doesn't. Please help. Thank you!
1.You have to parse json in php
2.make an array for dropdown list
Following steps to be followed:
$data ='[{"name":"Indiarto Priadi","id":"502163984"},
{"name":"Agustin Kertawijaya","id":"511990261"},
{"name":"Jecklyne Christin L","id":"528197912"},
{"name":"Jazi Eko Istiyanto","id":"531149275"},
{"name":"Esritha Situmorang","id":"535864892"},
{"name":"Christy Margaretha Siregar","id":"543468540"},
{"name":"Daniel Oscar Baskoro","id":"549332828"}]';
$user_friend = json_decode($data, true );
$data=array();
foreach($user_friend as $key=>$val)
$data[]=$val['name'];
$json_friends =json_encode($data);
echo $json_friends;
you need to pass label and value into you php file
For example
$data[] = array(
'label' => $row['city_name'].','.$state['state_name'].','.$c_name['country_name'],
'value' => $row['city_name'].','.$state['state_name'].','.$c_name['country_name']
);
echo json_encode($data);
flush();

image declaration in external js file in cakephp

I am declaring an image as
<?php echo $this->Html->image('buttonaft.png', array('alt'=> __('img', true), 'border' => '0')); ?>
inside a javascript file that would be read by default.ctp in cakephp
The code is not able to read the image.
try to give like this..
but make sure you are giving correct path.
echo $html->image('image.png', array('url' => '/' . IMAGES_URL . 'image.png'));
you can also do this in 1.2
echo $html->link(
$html->image('img.png'),
'img.png',
array(),
null,
false
);
or in 1.3
echo $html->link(
$html->image('img.png'),
'img.png',
array(),
array( 'escape' => false ),
);

Categories

Resources