Zend MVC - design - onClick, load new view ? A view inside a view? - php

I have a page and, that page will fade out to display a new content (from another view).
In general terms, what should we have into consideration here?
Where should I dig in to make this possible?
Should I call a view inside another view, by using some sort of ajax?
class EquipasController extends OccControllerAction{
public function init(){
if ($this->getRequest()->isXMLHttpRequest()) {
$this->_helper->layout()->setLayout('blank');
//$this->_helper->layout()->disableLayout();
$logger = $this->getInvokeArg('bootstrap')->getResource('Log');
$logger->debug('AJAX Call');
}
}
public function listaAction()
{
echo ("I'm HERE??");
$dados = array('dados1', 'dados2', 'dados3');
}
}
<h1>Tests...</h1>
Mostra Lista Sff
<div id="testresults">
<h1>Esta é uma coisa que aparece sempre.</h1>
</div>
<script type="text/javascript">
$(function() {
$('.ajaxloader').click(function(event) {
var target = $(this).attr('href');
window.location.hash = target;
$.ajax({
url: target,
success: function(data) {
$('#testresults').html(data);
}
});
return false;
});
});
</script>
When I first load the page, all ok.
When I click the link, I actually get an ajax call (because I notice no refresh).
When it displays it displays the all page again.
It doesn't display the echo that i've put on listaAction ! :-(
UPDATE:
When I see my link URL on the browser I see:
http://mypage.org/equipas/registo#/equipas/lista
By on the anchor I have:
http://mypage.org/equipas/lista
Could the issue be here?

I've done this exact thing before. Here's a stripped down version of what I do.
Have a 'blank' layout. This excludes any global header/content/css.
Have a 'smart' controller init() or plugin that will toggle between your default and blank layout if the request is ajax. Here's my (stripped) controller code:
class TestController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
if ($this->getRequest()->isXMLHttpRequest()) {
$this->_helper->layout()->setLayout('blank');
$logger = $this->getInvokeArg('bootstrap')->getResource('Log');
$logger->debug('AJAX Call');
}
}
public function indexAction()
{
// render the default page
}
public function speedAction()
{
// Do stuff render something
}
public function somethingelseAction()
{
// do something else render something.
}
}
Have your initial view render with a target div, as well as some links that you want to go into that target. Here's my index.phtml:
<h1>Tests...</h1>
<a class="ajaxloader"
href="<?php echo $this->url(array('controller'=> 'test', 'action' => 'speed'), null, true);?>">Speed</a>
<a class="ajaxloader"
href="<?php echo $this->url(array('controller'=> 'test', 'action' => 'somethingelse'), null, true);?>">Something Else</a>
<div id="testresults">
<h1>Default stuff to show.</h1>
</div>
Setup some jQuery code (however you like) to attach to these 'ajaxloader' links and target your results to the 'testresults' div. Here's some basic JS:
$(function() {
$('.ajaxloader').click(function(event) {
var target = $(this).attr('href');
window.location.hash = target;
$('#testresults').fadeOut('slow', function() {
// complete fadeout, load new content while it's hiding!
$.ajax( {
url : target,
success : function(data) {
$('#testresults').html(data);
$('#testresults').fadeIn();
}
});
});
return false;
})
});
All links clicked on that have the class 'ajaxloader' will be loaded via ajax and put into the 'testresults' div.

If your page causes a portion to fade out then you must use JavaScript (or CSS animation or IE's old, awful propriety, meta tags).
I would use JavaScript to fetch a view via AJAX, and load it into an element on your page.

Related

Start a function through url

The following function is inside a page (mainpage.php) which I load dynamically into a tab (tabcontent.php) .
$('.editlink').on('click', function() {
var id = $(this).attr('data-ds');
$.ajax({
url: 'noticejob.php?step=get&ds='+id,
method: 'GET'
}).success(function(response,status) {
...
});
});
I call this function by this link, which is inside the tabcontent.php
<i class="fa fa-pencil"></i>
All works fine.
Now I want to start this function over a url call for adding links into other pages to open the mainpage.php and start with this function (should open a modal).
For Example: mainpage.php?start=edit&ds=123
Is it possible and in which way?
You can just print the JS you need to execute in the PHP page.
...
?>
<script>
$(function() {
foo();
});
</script>
<?php
...
To open the modal automatically, either create a JS function that opens the modal and call it (with the previous method), or do
<script>
$(function() { $('.editlink').click(); });
</script>
Ok, now it works,
i've put a new (named "edit_function") js function into the mainpage.php with the "old" code of the "$('.editlink').on('click', function() {..."
after that i call this function from the mainpage if there is a url parameter. I do it into the php file and create a dynamicaly js code
if($_GET['job']=='new')
{
$p['JS']='edit_function("'.$_GET['detail'].'");';
}
at the other hand i call the function for the tabcontent.php page in this way
$('.editlink').on('click', function() {
var id = $(this).attr('data-ds');
edit_function(id);
});
for me it works

Rendering ajax layout from cake's beforeFilter using requestAction

I have a scenario were in the beforeFilter in AppController, I have a session-check method which checks if some session data is set. If not, the user is redirected to the login page.
This works well with server side action call, however when ajax calls are made, the beforeFilter seems to ignore the requestAction.. Here's my code
AppController beforeFilter
public function beforeFilter(){
if($this->RequestHandler->isAjax()) {
echo $this->requestAction("/users/ajax_login", array('return'));
}else{
$this->redirect('/login');
}
}
ajax_login action
function ajax_login() {
$this->render('ajax_login', 'ajax');
}
ajax_login view
<script type="text/javascript">
window.location = '<?php echo 'http://'.$_SERVER['HTTP_HOST'].$html->url('/login').'"'; ?>';
</script>;
Basically this should render an ajax layout. However the view is not being rendered and therefore redirection is not working.
Of course I get an error since the action being called via ajax is still being called which fires an error net::ERR_CONNECTION_RESET due to session expiry.
I found an alternative solution that works. Here's the code :
class AppController extends Controller {
var $components = array('Auth', 'RequestHandler');
function beforeFilter() {
if($this->RequestHandler->isAjax()) {
if(!$this->Session->check('data')) {
header('Requires-Auth: 1'); // set header and make sure to exit
exit;
}
}
}
}
Then just have a jquery (acaxComplete) event listener in place which checks for the header, if it is set to 1, redirect to login page.
<script>
$(document).ready(function() {
$(document).ajaxComplete(function(event, request, settings){
var requiresAuth;
try { requiresAuth = request.getResponseHeader('Requires-Auth') }
catch(e) {}
if (requiresAuth == 1) {
window.location = "<?php echo $this->Html->url(array('controller' => 'users', 'action' => 'login')); ?>";
return false;
}
});
});
</script>
This works fine. However I am still interested as to why my first solution did not work. Any input is welcome.. Cheers

jquery change content in php template from file with "pages" in

I've created a php template with variables library and what not all pulled into an index page I've created a basic script with fades the page in and out on load and have got that working the next thing I wanted to do is to use my navbar links to pull formatted content into the page (as I'm using foundation 4 framework) now the code I tried is as follows
$(document).ready(function() {
var hash = window.location.hash.substr(1);
var href = $('#nav li a').each(function(){
var href = $(this).attr('href');
if(hash==href.substr(0,href.length-5)){
var toLoad = hash+'.html #content';
$('#content').load(toLoad)
}
});
$('#nav li a').click(function(){
var toLoad = $(this).attr('href')+' #content';
$('#content').hide('fast',loadContent);
$('#load').remove();
$('#wrapper').append('<span id="load">LOADING...</span>');
$('#load').fadeIn('normal');
window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
function loadContent() {
$('#content').load(toLoad,'',showNewContent())
}
function showNewContent() {
$('#content').show('normal',hideLoader());
}
function hideLoader() {
$('#load').fadeOut('normal');
}
return false;
});
});
the idea is that onclick it removes the content with the wrapper displays a loading gif and then loads the page content which is stored in the link referred to in the
but its not working is just reloading the whole page . . . . i have tried reading up and it says that you can use
You can extract from another page using the load method thus >$('#targetElement').load('page.htm #container') syntax.
and using the get function but im not to good at all this jquery is there a way I can do it in php or where am I going wrong with what I've done.
Try this....You were calling the load, and functions inside improperly...
$('#nav li a').click(function(){
var toLoad = $(this).attr('href')+' #content';
$('#content').hide('fast',loadContent);
$('#load').remove();
$('#wrapper').append('<span id="load">LOADING...</span>');
$('#load').fadeIn('normal');
//window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
function loadContent() {
$('#content').load(toLoad,showNewContent);
}
function showNewContent() {
$('#content').show('normal',hideLoader);
}
function hideLoader() {
$('#load').fadeOut('normal');
}
return false;
});
When a user clicks a link the browser by default unloads the current page and loads the page indicated by that link.
You need to prevent the default behavior of the browser, in order to do this you need to call the .preventDefault() method on the click event that was triggered for that link.
$('#your-context').click(function(event) {
event.preventDefault();
/**
* the rest of your code here
*/
});

Ajax Load Into <div> with child window scrolls to top of webpage

Here is my dilemma, I am calling my external pages via ajax into a <div>, within the <div> I have links that callback to the parent window which triggers the ajax load event for the next page. My problem is the call back brings the website to the top of the page. Here is the code;
Parent Window:
function Display_Load() {
$("#loading").fadeIn(900,0);
$("#loading").html("<img src='<?php echo $reg->get ('rel_addr'); ?>img/load.gif' height='50' width='50' />"); return false;
}
function Hide_Load() {
$('#midback').fadeIn('slow');
$("#loading").fadeOut('slow');
}
function loadContent(page) {
var param = "";
if (page) { param = page; } else { param='home'; }
Display_Load();
$('#midback').fadeOut('slow', function() {
$(this).load("<?php echo $reg->get ('rel_addr'); ?>"+param+".php",
Hide_Load()); return false;
});
Child Window Code:
<span id="events" class="more">more events...</span>
<script type="text/javascript">
$(document).ready(function() {
$('.more').click(function() {
var params = $(this).attr("id");
window.parent.loadContent(params);
}); return false;
});
I would also like to animate the height of the <div> (close it then reopen it to correct height of new page) as well, first I would like to get this out of the way however.
Try changing your click on the child to:
$('.more').click(function(e) {
e.preventDefault();
// . . .
return false;
});
See http://api.jquery.com/event.preventDefault/
The problem is when you click on the more class, you are likely appending a # to the end of your URL. This causes the page to jump up to the top. Per the documentation, the preventDefault will stop this from happening. For good measure, also return false from the click function.

call jquery function wordpress

Below code is jquery function
$(document).ready(function () {
$('#access').accordion({
create: function (event, ui) {
$('div#access> div').each(function () {
var id = $(this).attr('id');
$(this).load(id+'.html', function () {
$('#access').accordion('resize');
});
});
}
});
});
where access is the div id i have used for menu items in header.
below code is header.php page code
<div id="access" >
<ul>
<?php wp_list_pages('depth=1&title_li=');?>
</ul>
</div>
what i want is onclick of the menu item the javascript function should be called..
how to call function from php file?
You don't call the JavaScript function from PHP. The PHP merely enables you to build the HTML page dynamically. Once the page is ready, the JavaScript is called and it starts binding the events to the appropriate elements.
What you need to do is look at the generated code using the 'view source' or firebug and explore the structure of the generated HTML and then you can bind the event to the requested element.

Categories