Simultaneous ajax requests - ajax

I am trying to run the remote function in the code below every 5 seconds. It runs only once if I remove the frequency option. I tried remoteTimer function, but when I use remoteTimer function, some code of the script goes outside the script tags and I see that in the webpage.
echo $ajax->form(array('type' => 'post', 'options' => array('model' => 'Thing',
'url' => array('controller' => 'things', 'action' => 'xyz'),'update' => 'dy4', 'indicator' => 'ldng', 'loading' => ( $ajax->
remoteFunction(array('url' => array('controller' => 'stories', 'action' =>
'keep'), 'update' => 'dy3', 'frequency' => 5))))));echo $form->input('a', array('type' => 'checkbox'));echo $form->input('b', array('type' => 'checkbox')); echo $form->end('RUN');
If in case, this cannot be done using CakePHP helpers, how can I do it with JavaScript?

Use remoteTimer in a very simple controller. See the page source and between <script> tags you'll get code that does what you want.You can use that code as value for "loading" option.

Related

CakePHP adding image to echo $this->Html->link

Hi I have tried various things but not sure if I am trying to over complicate things. Quite simply I want to add a small image country flag for the word 'Spanish' and 'English' and have an 'Alt' tag with those words instead. The following works fine with just text and as is but it would be cool to have the image either to replace or sit side by side with the word. Any help available?
echo $this->Html->link('Spanish', array('controller'=>'settings', 'action'=>'lang', 'spa'),
array('rel'=>'nofollow'));
}
else {
echo $this->Html->link('English', array('controller'=>'settings', 'action'=>'lang', 'eng'),
array('rel'=>'nofollow'));
}
You can either output just a linked image using the image method with a url attribute:-
<?php
echo $this->Html->image(
'spain.jpg',
array(
'alt' => 'Spanish',
'url' => array('controller' => 'settings', 'action' => 'lang', 'spa')
)
);
Or you can include an image with a text link by combining the normal CakePHP link method and the image method:-
<?php
echo $this->Html->link(
h('Spanish') . $this->Html->image('spain.jpg', array('alt' => 'Spanish')),
array('controller' => 'settings', 'action' => 'lang', 'spa'),
array('escape' => false, 'rel' => 'nofollow')
);
With the link method you need to remember to prevent Cake from escaping the img tag using 'escape' => false. If you disable escaping like this it is work remembering to escape any user provided text by using the h() method to prevent any HTML injection (I've shown this in my example wrapping the word 'Spanish' but this is only necessary if this is coming from a variable).
if you are using cake2 see this link to the cookbook
echo $this->Html->image("spain.jpg", array(
"alt" => "Spanish language",
'url' => array('controller' => 'settings', 'action' => 'lang', 'spa')
));

How to echo config item from config file for view in Codeigniter?

How to call config item from config file for view in codeigniter.
here is my config file
$config['user'] = array(
'email_validation' => 'email validation',///^([\w-]+(?:\.[\w-]+)*)#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
'no_permission' => '/',
'admin_group' => 'admin',
'default_group' => 'default',
'public_group' => 'public',
'users' => 'aauth_users',
'groups' => 'aauth_groups',
'user_to_group' => 'aauth_user_to_group',
'perms' => 'aauth_perms',
'perm_to_group' => 'aauth_perm_to_group',
'perm_to_user' => 'aauth_perm_to_user',
'pms' => 'aauth_pms',
'system_variables' => 'aauth_system_variables',
'user_variables' => 'aauth_user_variables',
'remember' => ' +3 days',
'max' => 13,
'valid_chars' => array(' ', '\''),
'ddos_protection' => true,
'recaptcha_active' => false,
'recaptcha_login_attempts' => 4,
'recaptcha_siteKey' => '',
'recaptcha_secret' => '',
'max_login_attempt' => 10,
'verification' => false,
'email' => 'admin#admin.com',
'name' => 'Emre Akay'
);
Here is my load config
$this->config->load('user');
And I will view its item for view as below
$site_name = $this->config->item('email_validation');
But is don't show any thing
This is because your config array is two dimensional array. So, you can't access directly email_validation without getting user first. Moreover,
$this->config->load('user'); just means loading user.php from application/config/ directory. Doesn't mean loading user index from $config array. You can do it like that.
$userConfig = $this->config->item('user');
echo $userConfig["email_validation"];
Edit
Please make sure you config file is under application/config/ and loaded.
$this->config->load('user');
You can check which config is loaded by doing like this.
echo "<pre>";
print_r($this->config);
echo "</pre>";
Hope it will be useful for you.
if your php version>=5.4 you can use this
$site_name = $this->config->item('user')['email_validation']

CakePHP: How to define multiple success parameters when creating an AJAX-link?

I have currently the problem with CakePHP that I don't know how to define multiple success parameters in an AJAX/JS-link.
Currently, that's my code I have:
<?php echo $this->Js->link('Firma entknüpfen', array(
'controller' => 'contacts',
'action' => 'unbindCompany',
$contact['Contact']['id']
),
array(
'update' => '#success',
'success' => $this->Js->get('#current_company')->effect('fadeOut')
)); ?>
Now, I need a second 'success'-parameter/action to fade in another element, like this:
<?php echo $this->Js->link('Firma entknüpfen', array(
'controller' => 'contacts',
'action' => 'unbindCompany',
$contact['Contact']['id']
),
array(
'update' => '#success',
'success' => $this->Js->get('#current_company')->effect('fadeOut')
'success' => $this->Js->get('#assign_company')->effect('fadeIn')
)); ?>
I tried to pass an array to success but this didn't work, too.
How can I realise this correctly?
JsHelper::effect() returns a string, so you could simply concat the return values:
'success' => $this->Js->get('#current_company')->effect('fadeOut') .
$this->Js->get('#assign_company')->effect('fadeIn')

Is there a way to speed up ajax calls in Drupal forms

I've built a form in Drupal 7 using form API and ajax calls. A typical form item looks like this:
$form['wrapper']['step1']['currency'] = array(
'#type' => 'radios',
'#options' => array(
'USD' => t('USD'),
'GBP' => t('GBP'),
'EUR' => t('EUR'),
),
'#default_value' => (!empty($form_state['values']['currency'])) ? $form_state['values']['currency'] : 'USD',
'#title' => t('Choose Currency'),
'#required' => TRUE,
'#ajax' => array(
'callback' => 'ajax_step1',
'wrapper' => 'step1-wrapper',
'method' => 'replace',
'effect' => 'fade',
'speed' => 'fast',
),
);
Everything is working as should but even when the ajax call just rebuilds a small part of the form it takes couple of seconds (the throbber is working overtime :).
Is this normal?
Is there a way to speed this (keeping things the Drupal way)?
When the ajax call is made, it literally rebuilds the entire form and returns only an aspect of it. For instance, in your function ajax_step1, you are probably calling to return a certain element from the form, and display it in the step1-wrapper div/wrapper element.
If you want to speed up the form return, you need to optimize the form builder itself. Which means, you might need to rewrite the form.

Data validation with custom route issue (default url when errors occur)

I'm building a user panel, and having some problems with data validation. As an example, the page where you change your password (custom validation rule comparing string from two fields (password, confirm password)):
Route:
Router::connect('/profile/password', array('controller' => 'users', 'action' => 'profile_password'));
Controller:
function profile_password()
{
$this->User->setValidation('password'); // using the Multivalidatable behaviour
$this->User->id = $this->Session->read('Auth.User.id');
if (empty($this->data))
{
$this->data = $this->User->read();
} else {
$this->data['User']['password'] = $this->Auth->password($this->data['User']['password_change']);
if ($this->User->save($this->data))
{
$this->Session->setFlash('Edytowano hasło.', 'default', array('class' => 'success'));
$this->redirect(array('action' => 'profile'));
}
}
}
The problem is, that when I get to http://website.com/profile/password and mistype in one of the fields, the script goes back to http://website.com/users/profile_password/5 (5 being current logged users' id). When I type it correctly then it works, but I don't really want the address to change.
It seems that routes aren't supported by validation... (?) I'm using Cake 1.3 by the way.
Any help would be appreciated,
Paul
EDIT 1:
Changing the view from:
echo $form->create(
'User',
array(
'url' => array('controller' => 'users', 'action' => 'profile_password'),
'inputDefaults' => array('autocomplete' => 'off')
)
);
to:
echo $form->create(
'User',
array(
'url' => '/profile/password',
'inputDefaults' => array('autocomplete' => 'off')
)
);
does seem to do the trick, but that's not ideal.
Check the URL of the form in the profile_password.ctp view file.
Try the following code:
echo $this->Form->create('User', array('url' => array('controller' => 'users', 'action' => 'profile_password')));
Also, I think your form might be a little vulnerable. Try using Firebug or something similar to POST a data[User][id] to your action. If I'm right, you should be setting:
$this->data['User']['id'] = $this->Auth->user('id');
instead of:
$this->User->id = $this->Session->read('Auth.User.id');
because your id field is set in $this->data.
HTH.

Resources