I have a main view with a menu which helps me display another view. It's similar to this:
<div id="page">
<div id="menu">
Page1
Page2
</div>
<div id="content">
<!-- Page1 or Page2 are displayed here -->
</div>
</div>
I'm using php's Yii framework. Which makes me not to use <?php include("menuview.php"); ?>. So I'm looking for a different solution. I can do this with Ajax, but I would also like the link to change to mypage/controller/Page2. With Ajax I can only get it to this: mypage/controller/index#Page2
in main view, instead of include do
<?php echo $this->renderPartial('_page1', array('model'=>$model)); ?>
UPDATE:
protected/views/controller/page1.php and protected/views/controller/page2.php content at your liking
protected/views/layouts/custom.php:
<?php $this->beginContent('//layouts/main'); ?>
<div id="page">
<div id="menu">
<?php echo CHtml::ajaxLink('Page1', array('controller/page1'), array('update' => '#content')); ?>
<?php echo CHtml::ajaxLink('Page2', array('controller/page2'), array('update' => '#content')); ?>
</div>
<div id="content">
<?php echo $content; ?>
</div>
</div>
<?php $this->endContent(); ?>
protected/controllers/ControllerController.php:
class ControllerController extends Controller {
/**
* #var string the default layout for the views.
*/
public $layout = '//layouts/custom';
public function actionPage1() {
if (Yii::app()->request->isAjaxRequest)
$this->renderPartial('page1');
else
$this->render('page1');
}
public function actionPage2() {
if (Yii::app()->request->isAjaxRequest)
$this->renderPartial('page2');
else
$this->render('page2');
}
}
UPDATE2:
If you need the link in address bar to change too then your only option is to use regular link and not ajax <?php echo CHtml::link('Page1', array('controller/page1')); ?>
using ajax the preferred way is using hash like you mentioned.
Related
i use the following code with custom fields:
But the problem is; when did not put any content in one of custom field it need be hide.
the class style of the other effect it, and that is something I don’t want to show.
<div class = "class2">
<? php the_sub_field ('filed2'); ?>
</div>
<div class = "class1">
<? php the_sub_field ('filed1'); ?>
</div>
I want hide one of both custom filed when on of them is empty.
How can i hide it?
<?php if (get_sub_field ('filed2') || get_sub_field('filed1'));{ ?>
<div class = "class2">
<? php the_sub_field ('filed2'); ?>
</div>
<div class = "class1">
<? php the_sub_field ('filed1'); ?>
</div>
<?php } ?>
You would want to wrap it in an if statement.
You can also do (if you need the conditional logic on a per-field basis):
<?php if (get_sub_field ('filed2'));{ ?>
<div class = "class2">
<? php the_sub_field ('filed2'); ?>
</div>
<?php }; if (get_sub_field ('filed1'));{ ?>
<div class = "class1">
<? php the_sub_field ('filed1'); ?>
</div>
<?php }; ?>
Maybe it's duplicate but I don't know how it calls and cannot find. I need to load a php-file (template of my block) and set content into it.
For example, template file:
<?php include_once 'boot.inc' ?>
<div class="widget">
<div class="widget-title">I need to put title here</div>
<div class="widget-body">I need to put content here</div>
</div>
And php function to insert new block with this template:
function nm_new_block($title, $content) {};
Can I do it in php?
1° Solution: You need to change you code into:
<?php include_once 'boot.inc' ?>
<?php include_once 'template.php' ?>
<div class="widget">
<div class="widget-title"><?php echo $title; ?></div>
<div class="widget-body"><?php echo $content; ?></div>
</div>
In template.php file you must have something like this:
<?php
$title="My Title";
$content="My Content";
?>
And you don't need function for this cause variables are stored in template.php
2° Solution: You need to implement a function to retrieve variables from external source:
<?php include_once 'boot.inc' ?>
<?php include 'functions.php' ?>
<div class="widget">
<div class="widget-title"><?php echoTitle(); ?></div>
<div class="widget-body"><?php echoContent(); ?></div>
</div>
In functions.php
<?php
function echoTitle(){
$title = 'Add code to get title here';
echo $title;
}
function echoContent(){
$content = 'Add code to get content here';
echo $content;
}
Replace Add code to get title here and Add code to get content here with code to get contents ex:
$title = $_POST['title']; supposing you want get title by a submission form
I do not have enough details to tell you more.
I am new in Yii framework. I am using Chtml form to send data from view to controller and want to print the result and insert it to database. But in my case its showing an empty array. I couldn't able to understand where I am doing wrong. So please help me regarding this.
in controller
public function actionTest1()
{
$obj = new Pad;
if(isset($_POST))
print_r($_POST);// to check the desired result
if(isset($_POST['Pad']))
{
$obj->attributes=$_POST['Pad'];
if($obj->save()) // insert the data
$this->redirect(array('view','id'=>$obj->id));
}
}
in view
<?php echo CHtml::beginForm('','post',array('id'=>'step1Form')); ?>
<div class="stopPad">
<div class="floatLeft padTop5 marRight5">
<?php
echo CHtml::radioButton('stoppad',false,array('value'=>'Stop Pad after goal is reached'));
?>
</div>
<div class="currencyText padTop4 floatLeft">Stop Pad after goal is reached</div>
<div class="clearBoth"></div>
</div>
<div class="stopPad">
<div class="floatLeft padTop5 marRight5">
<?php
echo CHtml::radioButton('stoppad',false,array('value'=>'No limites'));
?>
</div>
<div class="currencyText padTop4 floatLeft">No limites</div>
<div class="clearBoth"></div>
</div>
<div class="nextText">
<?php echo CHtml::link('Next >',array('pad/test1'));?>
</div>
<?php echo CHtml::endForm(); ?>
Please help. Thanks in advance!
You are not submitting your form and of course $_POST will be empty. Line below is just a link:
<?php echo CHtml::link('Next >',array('pad/test1'));?>
You must change it to :
<?php echo CHtml::submitButton('Next >');?>
And also add an action to your form.
Updating the question with the changes I have done. Its redirect me to pad/test1 showing array() as result.
in controller
public function actionTest1()
{
$obj = new Pad;
if(isset($_POST))
CVarDumper::dump($_POST);// to check the desired result
if(isset($_POST['Pad']))
{
$obj->attributes=$_POST['Pad'];
if($obj->save()) // insert the data
echo "=====";
}
}
in view
<?php echo CHtml::beginForm( '','post'); ?>
<div class="stopPad">
<div class="floatLeft padTop5 marRight5">
<?php
echo CHtml::radioButton('stoppad',false,array('value'=>'Stop Pad after goal is reached'));
?>
</div>
<div class="currencyText padTop4 floatLeft">Stop Pad after goal is reached</div>
<div class="clearBoth"></div>
</div>
<div class="stopPad">
<div class="floatLeft padTop5 marRight5">
<?php
echo CHtml::radioButton('stoppad',false,array('value'=>'No limites'));
?>
</div>
<div class="currencyText padTop4 floatLeft">No limites</div>
<div class="clearBoth"></div>
</div>
<div class="nextText">
<?php echo CHtml::button('Next >', array('submit' => array('pad/test1'))); ?>
</div>
<?php echo CHtml::endForm(); ?>
A common source of confusion among new Yii users is how the 'safe' validator works, how it works with other validators, and why it's necessary in the first place. This article means to clear up this confusion, as well as explain the notion of Massive Assignment.
http://www.yiiframework.com/wiki/161/understanding-safe-validation-rules/
For some pages of my application, I would like to use dvuhkolonchaty template. For this I would like to just use a different layout.
In the new layout except the variable $content, which displays the contents of a particular view, I would like to withdraw more other data in another column. Here is the code of the new layout:
<?php /* #var $this AdminController */ ?>
<?php $this->beginContent('/layouts/main'); ?>
<div class="container-fluid">
<div class="row-fluid">
<div class="span4">
<!-- Any data -->
</div>
<div class="span8">
<?php echo $content; ?>
</div>
</div>
</div>
<?php $this->endContent(); ?>
And that's just where Any data, I would like to display a different not template information (eg, shape or edit the list of properties is constantly changing depending on the viewing record ID).
Q How can I print information in the Any data?
All your controllers will extends Controller class;
In the Controller class, you make a property like this:
public $anyData = null;
In your any actions, you can set `$this->anyData = "anything";
then in your layout, you can write :
<div class="span4">
<!-- Any data -->
<?php
if($this->anyData!=null) {
//Process $this->anyData here, echo or do something you like;
}
?>
</div>
I'm working on a AJAX sign up form which will be submitted to the Zend Framework. Right now, the form's action doesn't seem to be executing. The response text is a duplicate of the current HTML document. Can someone please inform me of what I'm doing wrong, and what the correct URL should be? I've set the project up using modules. For example, should the correct url from the example below be: '/account/register/auth'
#application.ini
resources.router.routes.register.route = /register
resources.router.routes.register.defaults.module = account
resources.router.routes.register.defaults.controller = register
resources.router.routes.register.defaults.action = index
#application/modules/accout/controllers/RegisterController.php
<?php
class Account_RegisterController extends Zend_Controller_Action{
public function init(){
$this->view->register = new Account_Form_Register();
}
public function indexAction(){
}
public function authAction(){
$request = $this->getRequest();
$form = new Account_Form_Register();
if($request->isPost()){
print('submit');
}else{
print('nothing yet');
}
}
}
?>
#application/modules/account/views/scripts/register/index.phtml
<div class="register">
<form id="registerForm">
<h2 class="formHeader">Register</h2>
<div class="floatLeft">
<?php echo $this->register->fName; ?>
</div>
<div class="remaining">
<?php echo $this->register->lName; ?>
</div>
<div class="floatLeft">
<?php echo $this->register->uEmail; ?>
</div>
<div class="remaining">
<?php echo $this->register->aEmail; ?>
</div>
<div class="">
<?php echo $this->register->phone; ?>
</div>
<div class="floatLeft">
<?php echo $this->register->oPassword; ?>
</div>
<div class="remaining">
<?php echo $this->register->cPassword; ?>
</div>
<div id="formButton">
<?php echo $this->register->submit; ?>
</div>
</form>
</div>
As I said, maybe you should disable the view processing when doing AJAX action in order to return a json_encode($some_data) instead of HTML content.
Try
$this->_helper->viewRenderer->setNoRender(true);
Take a look at this article