cakephp ajax not working properly - ajax

I am new in cakephp and I want to implement Ajax on my home page.
I have three modules in my page (client, developer and project). I want to add an ajax link. It's working perfectly only in the index page.
My code:
<h2>Projects</h2>
<div class="clear"></div>
<ul>
<li title="Project List">
<?php echo $ajax->link('Projects List', array("controller" => "projects", "action" => "index"), array( 'update' => 'main_page' ));?>
</li><br />
<li title="Add New Project">
<?php echo $ajax->link('Add New Project', array("controller" => "projects", "action" => "add"), array( 'update' => 'main_page' ));?>
</li>
</ul>
Now, my first issue is that in the add form, the validation with js is not working.
Second is: if I use cakephp inbuilt validation then it validates my form but redirects the page to "admin/projects/add" if no data is inserted.
Third problem is that when the above case happens and I want to redirect to listing page through my ajax link, at that time its also not working.

The best thing to do is stop using the ajax helper. Its been depreciated and will not be available in the 3.x branch.
This was done because it was not a good idea to start with, very limiting and buggy.
ajax with something like jQuery is not very difficult and you should rather look into using that or a similar tool.

Related

vue vf-form can't get ajax data

I have a problem with ajax from vue vf-form.
I have wizard in this project as registration process.
So every step I'have a different view.
Code:
<wizard-hidden-step
title="About your company"
v-ref:head2
:valid="valid.step2"
:locked="isLocked"
:loading="isSending"
>
#include('auth.steps.step2', ['action' => "/company/signup/step2"])
</wizard-hidden-step>
{{-- ====== STEP 3 - ADDITIONAL INFO ========= --}}
<wizard-hidden-step
title="Additional information"
description="Add company additional info"
v-ref:head3
:valid="valid.step3"
:locked="isLocked"
:loading="isSending"
>
#include('auth.steps.step3', ['action' => '/company/signup/step3'])
</wizard-hidden-step>
And on all view I have a similar data but I will insert the code where i can't get ajax data:
<vf-form ajax
action="{{ $action }}"
method="POST"
:validation="validation"
v-ref:step2
>
PHP Laravel Controller:
return json_encode([ 'shared' => [ 'status' => 'ok', 'postcodes' => $postcodes ] ]);
After that I have ajax data in console (see on picture) but I can't set that data on view in vf-select:
<vf-select
:items="shared.postcodes"
multiple
select2
err-msg="Postcodes"
name="postcodes"
:html="true"
:value=""
v-ref:postcodes
:options="{showDropdowns: true, width: '100%'}"
>
</vf-select>
I have just No results found in vf-select.
Please help me, I don't have idea what to do.
I've found solution.
I was send shared object from backend and when I edit that in share, and on frontend read that as a shared this was work. I don't know how but it work...

Joomla 2.5 frontend pagination clicking on next button not working

I am using joomala 2.5 and developed my own component for showing table data in front end and added pagination. I'm getting pagination links, after clicking on the links 'next', 'prev' nothing happens.
What may be the problem?
In view.html.php I've added
$this -> pagination = $this->get('Pagination');
In default.php I've added
<div class="pagination">
<?php echo $this->pagination->getListFooter(); ?>
</div>
You haven't mentioned what you have done in you components model file. My advoice to you is just read this document carefully http://docs.joomla.org/J1.5:Using_JPagination_in_your_component & you will be easily apply pagination. The doc is perfect & its very simple to use pagination in Joomla.
Thank you.
Put your pagination buttons inside a tag and make sure the action url points to the same view (e.g. action = 'index.php?option=com_component&view=listview')
<form action=""...>
<div class="pagination">
<?php echo $this->pagination->getListFooter(); ?>
</div>
</form>

Yii, Selenium and multiple submit buttons

I've got a form with two submit buttons which I want to test using Selenium.
View:
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'profile-form',
'enableAjaxValidation' => true,
'action' => '',
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange' => true,
'validateOnType' => false,
),
));
?>
<input name="cancel" type="submit" value="Cancel" />
<input name="submit" type="submit" value="Save changes" />
<?php $this->endWidget(); ?>
Controller is nothing special, you can assume it just prints "Your profile has been saved" or "Your profile was not saved" depending on what $_POST['cancel'] it gets
Test code:
<?php
$this->open('/profile_form_url');
$submit_button_selector = 'css=#profile-form input[name="submit"]';
$cancel_button_selector = 'css=#profile-form input[name="cancel"]';
$this->clickAndWait($cancel_button_selector);
$this->assertTextPresent('Your profile was not saved');
$this->open('/profile_form_url');
$this->clickAndWait($submit_button_selector);
$this->assertTextPresent('Your profile has been saved');
The problem is that code works great in browser but not when running tests in Selenium/Firefox. When running tests, it "sees" the first button only (Cancel), clicking "Save changes" has the same effect. If you place Save changes button first, it will not "see" Cancel button.
If you turn enableAjaxValidation off, it works both in browser and Selenium, but I'd like to have a more elegant solution of course. Like for example turning off the AJAX validation on clicking on Cancel.
No, the problem doesn't depend on which locator you use for buttons (xpath, css, id).
clickAndWait() calls waitForPageToLoad() - ajax form validation will generally not trigger this event (unless a page loads), so your test will never complete; this is probably why if you turn ajax validation off it works.
You might want to look at the other options selenium provides (this is an old link to the free phpunit pocket guide that describes some other options - it's based on phpunit 3.1 though) such as using click() and then using waitForCondition() with some javascript to run and return true if the new text is displayed.

cakephp ajax remote function select input parameter

I try to do two inputs in cakePHP (category, subcategory).
And if i change category input i want ajax to load values to subcategories.
What i can do it?
Im using remote function like this:
$ajax->remoteFunction(
array(
'url' => array( 'controller' => 'categories', 'action' => 'loadSubcategories', AND NOW I WANT PUT HERE CATEGORY ID FROM MY INPUT ),
'update' => 'subcategories'
)
);
<select name="categories" id="categories" onhange="MY REMOTE FUNCTION">
CATEGORIES
</select>
<select name="subcategories" id="categories" onhange="MY REMOTE FUNCTION">
LOAD SUBCATEGORIES BY CATEGORY ID WITH AJAX
</select>
I hope you can understand me :)
All you need is:
a Cake action that outputs a list of items based on the data it receives (shouldn't be hard)
a bit of JS that figures out which categories are selected (which you already have)
another bit of JS that packages that data and sends it to the Cake action
another bit of JS that updates the site with the list of items you received back
I'd take a look at jQuery's AJAX functions to accomplish this task. If you POST the data in a format like this, it's very easily accessible in $this->data in Cake:
{
'data[ModelName][categories]' : $("#category").val()
}

cakephp ajax pagination works only once - scripts are not evaluatedd?

I followed tutorial from cakephp site but pagination with ajax works only once - the content is updated and its ok. But for the second time I click some page-link the whole page is reloaded - I think that click() event handlers are not binded again when content is refreshed by ajax - I don't know why... I am using this:
$this->Paginator->options(array(
'update' => '#content',
'evalScripts' => true
));
When I load page in the source code there is:
« Previous
$(document).ready(function (){
$("#link-925538478").bind("click", function (event) {
$.ajax({dataType:"html", success:function (data, textStatus){
$("#content").html(data);}, url:"\/final\/books\/index\/page:10\/sort:id\/direction:desc"});
return false;});
...
When I click for example next page (for the first time), everything is refreshed (the link hrefs also so it works) but the scripts are not reloaded so no click events are binded I think and clicking next page again just uses the link.
This is strange because I added this just after the pagination links:
<script>
$(document).ready(function (){
alert('yes');
});
</script>
And the alert is shown after first ajax refresh...
And I set up this thing ofc. <?php echo $this->Js->writeBuffer(); ?> at the end...
-------------------edit
I recognised that its not the paginator - for the following 2 links:
<?php echo $this->Js->link('link1', array('author' => 'abc'), array('update' => '#content')); ?>
<?php echo $this->Js->link('link2', array('author' => '123'), array('update' => '#content')); ?>
Its the same - when I click link1 its ajax, then when I click link2 there is normal reload - so it's somthing with script evaluation after ajax refresh... What that might be?
I am setting up JSHelper like this:
var $helpers = array('Js' => array('Jquery'));
I figured it out!!!
It's because ajax request sets the layout to app/View/Layouts/ajax.ctp and ajax.ctp is:
<?php echo $this->fetch('content'); ?>
I had to add this line
<?php echo $this->Js->writeBuffer(); ?>
To ajax.ctp to write java scripts (so the ajax links work after ajax request).
And now pagination works perfect!!!
Cake php Ajax Paginator not seems to be working fine. I had similar issues also.
I would recommend you to use the cakephp plugin Cakephp-DataTable
This plugin has implemented the pagination and it has most of the features by default. They have also provided documentation and if you find any difficulty in implementing please go throught the issues section
Also the developer is very responsive and can get clarifications for that plugin if you have any.

Resources