where to write jquery ajax in yii form - forms

I"m trying to write custom auto complete for text field. can anyone pls tell me where to write this jquery, ajax code for this textfield in yii..
<div id="output" class="row">
<?php echo $form->labelEx($model,'id'); ?>
<?php echo $form->hiddenField($model,'id'); ?>
<?php echo $form->textField($model,'id');
'$(function () {
$("#search").change(function(){
$.ajax({url:BASE_URL + '/controller/lookup/',
type:"POST",
data:this.value,
success:function(data){
$("#output").html(data);
}
});
});
});'?>
any help pls,
Many Thanks

Try Yii::app()->clientScript->registerScript instead of Yii::app()->getClientScript()->registerScript

You should just make a new file, call it, 'myFuncs.js'. Place in a directory within your Yii Web App.
Then, in your view, simply call the js file.
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/path/to/your/scripts/myFunc.js');

Related

How do i use translate in the form using Zend?

<?php echo $this->translate("Hello World"); ?>
This works inside view. How do I get the same in the form?
In a Form, you can try this :
$this->getView()->translate("Hello World");

Getting HTML of a Cake PHP form

I am creating a form using Cake PHP. Is there any way to retrieve the basic HTML of the created form. For example,if we use Form Helper, we can create form using PHP itself. But now, I need only the html part of the created form for other use. Is it possible to retrieve it??
For example, say if I give input form like,
<?php
echo $this->Form->create('User');
echo $this->Form->input('email');
echo $this->Form->end('Save');
?>
I need output like this
<form action="index.html">
<input type="email" />
<input type="submit" value="Save" />
</form>
I can even create seperate function for attaining this objective. But I would like to know, if there is any other method for achieving this output
you can store FormHelper output in a string variable
<?php
$html_string = '';
$html_string .= $this->Form->create('User');
$html_string .= $this->Form->input('email');
$html_string .= $this->Form->end('Save');
?>
and then use your string elsewhere. But I'm not sure this is what you're searching for.
If I understand the question correctly, you want to use the form you created on another part of your site?
If that is the case, I would put the form itself in an Element and then call the Element wherever I wanted the form.
View/Elements/form.ctp
<?php
echo $this->Form->create('User');
echo $this->Form->input('email');
echo $this->Form->end('Save');
?>
Then in any view on your site you can call the Element using:
<?php echo $this->element('form');?>

Simplepie set_feed_url feed

and thanks in advance for your help. I'm using Simplepie to try to bring this feed:
http://www.p2rx.org/webservices/rssNews.cfm?Type=Tribal&getall=true
into this page:
http://www.tribalp2.org/events/news.php
As you can see, it isn't working. Although many other feed urls I've entered into:
$feed->set_feed_url('http://www.p2rx.org/webservices/rssNews.cfm');
work just fine. I've added
$feed->force_feed(true);
as well. What might the problem be? The full code is:
<?php
require_once('../php/autoloader.php');
$feed = new SimplePie();
$feed->set_feed_url('http://www.p2rx.org/webservices/rssNews.cfm?Type=Tribal&getall=true');
$feed->force_feed(true);
$feed->init();
$feed->handle_content_type();
?>
<?php foreach ($feed->get_items(0,30) as $item): ?>
<div class="item">
<h4><?php echo $item->get_title(); ?> - <?php echo $item->get_date('F j, Y'); ?></h4>
<p><?php echo $item->get_description(); ?></p>
</div>
<?php endforeach; ?>
<?php unset($feed); ?>
Thanks.
SimplePie can not display all feeds. There are crap feeds that do not follow the standards and even if you try to force it, SimplePie can not decipher them. However checking http://validator.w3.org/feed/ your feed validates.
Try not forcing feed, also try calling force feed after init. If the feed validates SimplePie should handle it.

Zend reusable widgets / plugins / miniapplications?

I'm new to Zend framework and trying to get some insights about code re-usability. I definitely know about modules but there seems to be a bit of uncertainty about what functionality should go into modules and what not.
What I'm trying to accomplish:
1) to have reusable mini programs/widgets/plugins (whatever you may call them) that one can simply plug into any site be doing this in layout or view:
<?php echo $this->contactform;?>
or this in the view:
<?php echo $this->layout()->blog;?>
I'd call them extension. so basically sort of what you'd see in Joomla/ WordPress/Concrete5 templates.
2) All code that is related to that specific extension should be in it's separate directory.
3) We should be able to output extensions only for certain modules/controllers where they are required. they shouldn't be rendered needlessly if it won't be displayed.
4) each extension may output multiple content areas on the page.
Do you have a nicely laid out structure / approach that you use?
It sounds like you need study up on view helpers. View helpers can be a simple as returning the App Version number or as complicated as adding html to multiple place holders. For example:
layout.phtml:
<h1><?php echo $this->placeholder('title'); ?>
<div class="sidebar">
<?php echo $this->placeholder('sidebar'); ?>
</div>
<div class="content">
<?php echo $this->layout()->content; ?>
</div>
in your view script foo.phtml for example:
<?php
$this->placeholder('title')->set('Hello World!');
$this->placeholder('sidebar')->set('Hello World!');
?>
<h1>Bar Bar!</h1>
Now if you want to be able to reuse that over and over again you can do this:
<?php
class Zend_View_Helper_MyHelper extends Zend_View_Helper_Abstract
{
public function myHelper()
{
$this->view->placeholder('title')->set('Hello World!');
$this->view->placeholder('sidebar')->set('Hello World!');
return '<h1>Bar Bar!</h1>';
}
}
Now, replace the code in your foo.pthml with:
<?php
echo $this->myHelper();
Both examples of foo.phtml output:
Hello World!
Hello World!
Bar Bar!
Of course this is very simplified example, but I hope this helps point you in the right direction. Happy Hacking!

Zend Framework: headTitle()->append() issue

Has anyone run into this problem...
In my layout.phtml I have:
<head>
<?= $this->headTitle('Control Application - ') ?>
</head>
then in index.phtml I have:
<? $this->headTitle()->append('Client List'); ?>
I expect that, when I go to my index action, the title should be 'Control Application - Client List' but instead I have 'Client ListControl Application - '
What is going on? How can I fix this?
Default behaviour of the headTitle() is to append to the stack. Before calling headTitle() in layout.phtml, your stack is:
Clientlist
Then, you call headTitle with the first argument and no second argument (which makes it default to APPEND), resulting in the following stack:
ClientListControl Application -
The solution, in layout.phtml:
<?php
$this->headTitle()->prepend('Control Application -');
echo $this->headTitle();
?>
Additionally, you can use the setPrefix method in your layout as such:
<head>
<?= $this->headTitle()->setPrefix('Control Application') ?>
</head>
And in your controllers/actions/etc use the standard append/prepend:
<?php
$this->headTitle()->setSeparator(' - ');
$this->headTitle()->append('Client List');
?>
I don't actually use headTitle, but do use ZF, and I had a quick look on the mailing list, this might solve the problem:
<head>
<?= $this->headTitle('Control Application') ?>
</head>
Then:
<?php
$this->headTitle()->setSeparator(' - ');
$this->headTitle()->prepend('Client List');
?>
This happens because the layout is the last script to be executed. So you actually do the append BEFORE the set of the title, so that there's nothing to append to yet.
Set the main title (Control Application) in a Controller. For example I always do it in the predispatch action of a initPlugin so that it is execute before any other Controller Action, and I can append or prepend at will.
To use such a plugin just define a new Class extending Zend_Controller_Plugin_Abstract and define a function preDispatch(Zend_Controller_Request_Abstract $request) where you can put all your common-to-the-whole-site code, and to register the plugin just put it into the controllerFront of your bootstrap: $controller->registerPlugin(new InitPlugin());