Getting HTML of a Cake PHP form - forms

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');?>

Related

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.

Change URL on submit form

So I am trying to make a simple external popup that has 1 pull down menu for category and 4 columns of checkboxes as tags. (this is to feed a wordpress search engine plugin)
I use regular form submit for this but I end up with this as final URL:
Site.com/?category_name=VALUE&tag=TAG1&tag=TAG2&tag=TAG3
But I want my URL to be like this:
Site.com/?category_name=VALUE&tag=tag1+tag2+tag3
Could anyone point me in the right direction into achieving this?
CATEGORY_NAME is for searching a specific WordPress category
and the TAGS are regular post tags.
You can't do that with a checkbox. That is not how checkboxes work. It is not a choice, really. It the way that HTML, PHP, and your browser(s) are built.
Additionally, what you have done won't work. You've set the same variable to several different fields. Each time you set is, you overwrite the previous value. While your URL may look like ?category_name=VALUE&tag=TAG1&tag=TAG2&tag=TAG3 if you were to add var_dump($_GET); to your script you'd see that the only thing PHP sees is the last one-- tag=TAG3.
You can get the code working by using square brackets-- []-- in the checkbox names. An example in very minimal code (for demonstration purposes only):
echo '<form>';
echo '<input type="checkbox" name="tag[]" value="tag1">';
echo '<input type="checkbox" name="tag[]" value="tag2">';
echo '<input type="checkbox" name="tag[]" value="tag3">';
echo '<input type="checkbox" name="tag[]" value="tag4">';
echo '<input type="checkbox" name="tag[]" value="tag5">';
echo '<input type="checkbox" name="tag[]" value="tag6">';
echo '<input type="submit" value="Clickie">';
echo '</form>';
The 'tag' part of you URLs will look like this however:
&tag[]=tag1&tag[]=tag4&tag[]=tag6
That is just how checkboxes work. There are two workarounds that I can think of.
The first is to use Javascript to populate a hidden field in your form.
echo '<form>';
echo '<input type="hidden" name="tag" value="">';
echo '<input type="checkbox" value="tag1">';
echo '<input type="checkbox" value="tag2">';
echo '<input type="checkbox" value="tag3">';
echo '<input type="checkbox" value="tag4">';
echo '<input type="checkbox" value="tag5">';
echo '<input type="checkbox" value="tag6">';
echo '<input type="submit" value="Clickie">';
echo '</form>';
You would have your Javascript watch for clicks on those checkboxes and fill the value into the hidden tag field. In order to get the URL you want you have to remove the name from the checkboxes, which makes this form entirely dependent upon Javascript. Without Javascript it won't work. I consider that bad design.
The other option is to use mod_rewrite to try to rewrite your URL, but honestly, I don't know if mod_rewrite is capable of the complex regex you would need to make that work.
I don't think either workaround in worth the effort or the price. I would suggest you use the square brackets and process the array at the receiving end. In other words, if you have &tag[]=tag1&tag[]=tag4&tag[]=tag6 then this will get a string like what you want in your URL:
if (isset($_GET['tag'])) {
$tstr = implode('+',$_GET['tag']);
}
echo $tstr;
You can use that in your search function, whatever you are using for that.
This is what I did. Instead of adding a extra function to my wordpress (which was causing some unexpected error when testing deeper) I simply created a PHP to catch the URL before submiting further.
This is what it looks like:
<?php
if (isset($_GET['tag'])) {
$tstr = implode('+',$_GET['tag']);
}
$cat = ($_GET['category_name']);
header("Location: http://url.com/?category_name=$cat&tag=$tstr");
?>
I don't know if this is correct or secure, but it works :D
Thanks for your help !

Symfony: form field access with no sfForm object

i'm working on a symfony project and i developed a form to upload a file and save its info to a table in my model. And didn't use the sfForm class to implement my form.
Here you have my form
<form name="new_file" action="<?php echo url_for('home/uploadFile');?>" method="post">
<input type="file" id="file">
<input value="<?php echo $codigo_maestro?>" id="master_id">
<input value="<?php echo $codigo_entidad?>" id="entity_id">
<input type="submit" value="Upload">
</form>
So now i'm trying to access the fields of the sumbited form in my action function and don't know how :(
$request->getParameter('file');
$request->getParameter('master_id');
$request->getParameter('entity_id');
this code didn't work.
So please help me solve this! How can i access the fields of my form from the action??
You need to add a name to your form fields, that's the name you can access them via $request->getParameter().
finally i did this way, maybe not most elegant, but working
in action.class.php
$file= $_FILES['file'];
$filesize = $archivo['size'];
$filetype = $archivo['type'];
$filename = str_replace(' ','-',$file['name']);

Zend controller/view newbie puzzle: $_GET & $_POST empty - on receipt from HTML form within view

Zend newbie here ... And just to make it better, my mission is to build on top of someone else's pre-existing Zend site.
(BTW: zf show version --> Zend Framework Version: 1.11.1 -- I seem to have Zend_Form).
Here's the curious bit. All the forms are built in HTML within views. They seem to work, although I can't figure out how -- especially given what I am seeing.
I followed the convention and created a view for a test form and wrote the form:
<form action="<?php echo $this->url(array('controller'=>'ControllerName','action'=>'submit'));?>" method="post" style="margin-left:20px">
<p class="bold setmgr">Your email here:</p>
<div class="field">
<input class="text" type="text name="custEmail"/>
</div>
<div class="field">
<input class="button" value="Submit and be free!" type="submit"/>
</div>
</form>
The submitAction member in the controller is firing correctly. No problem.
But ALL the places I could look for the POST data appear to be empty!
echo "obj custEmail = [" . $this->_request->getPost('custEmail') . "]\n";
echo "GET custEmail = [" . $_GET['custEmail'] . "]\n";
echo "POST custEmail = [" . $_POST['custEmail'] . "]\n";
if ($this->_request->isPost()) {
$data = $this->_request->getPost();
Zend_Debug::dump($data);
}
They all produce nothing.
I'd be much obliged for a solution or even a clue about what is going wrong.
Thanks for reading.
Your form is not in the correct format.As it's PHP you can use form like this or you can even generate a ZEND_FORM(which is profound way to do it).It's always a good practise to work around with ZEND_FORM.If you still want to use this and the go by your way,here is th snippet I modified for you.
I am modifying the Code for you.Your View should have this form in it;
<form action="" method="post" style="margin-left:20px">
<p class="bold setmgr">Your email here:</p>
<div class="field">
<input class="text" type="text" name="custEmail"/>
</div>
<div class="field">
<input class="button" value="Submit and be free!" type="submit" name="submit"/>
</div>
</form>
<?php
echo $this->custEmail;
?>
Now write the following one on your ACTIOn,i.e. submitAction;
public function submitAction()
{
if ($this->getRequest()->isPost())
{
$custEmail = $this->getRequest()->getPost('custEmail');
echo $custEmail;
$this->view->custEmail = $custEmail;
}
}
Now check if it works for you or not.
Create a form using Zend_Form. When ZF already has a way to create forms, you should use that. Your method is like a hack and is not a recommended way to do things.
Check here on how to create a Zend_Form
http://framework.zend.com/manual/en/zend.form.html

porting template to zend framework

I got few issues proting a pear based form to zend form.
I have few elements I need :
Basic Elements
Groups
Group Elements
Sections
I previously used templates to render the forms on Pear. I obviously cannot use pre-existing zend decorators, since I need to specify css classes for each of the components of my base elements.
To see the issue I need to render this, which is the template for a basic element :
<li class = "{position_in_the_form} {error}">
<label class="{label_class}"> {label}
[<span class="required_class"> * </span>]
</label>
<div> {element_content} </div>
[<p class = "{error_class}"> {error_message} </p>]
</li>
So as you can see I have many dynamic things I would like to be able to specify : position in the form, class for the label, class for the required section, the class for the error.
I would also like to be able to specify this from an ini file. I manage to set up the basic meta from the ini but not custom fields.
One of the reason I cannot use basic decorators is that I need to have "error" in the "li" class when there is an error in the element or the sub_form.I'm not sure this is possible with the error decorator... (correct me if I'm wrong)
Also, for the group I need something handling the errors, and since the core groups don't handle errors I need to subclass the sub_form. But how can I create a subform in an ini file and I don't know how to provide parameters to the sub form fromn the ini.
The main idea here is to be able to have visual and logic groups of elements in a form. For example I need a 'name' group with fullname, middle name, etc. This also implies a global validator for this "name" group.
An other thing is that I want to be able to position these groups : left half, right half, full
I got the css ready for this and working with pear.
So what I need is a simple solution, with few code and ini configurations. Unfortunately I think I got stuck in something too complicated, so if someone has any idea about a simple architecture it would be amazing!
Thanks in advance for your help,
Best, Boris
In your complex decoration need, you might want to use the ViewScript Zend_Form_Element_Decorator
$element->setDecorators(array(
array('ViewScript', array('viewScript' => 'path/to/your/views/element.phtml')),
));
and then in path/to/your/views/element.phtml, more or less something like
<li class="<?php echo $this->element->getAttrib('position_in_the_form') ?> <?php echo $this->element->hasErrors() ? 'error' : '' ?>">
<label class="<?php echo $this->element->getAttrib('label_class') ?>">
<?php echo $this->formLabel($this->element->getName(),
$this->element->getLabel()) ?>
<? if ( $this->element->isRequired() ) { ?>
[<span class="required_class"> * </span>]
<? } ?>
</label>
<div> <?php echo $this->{$this->element->helper}(
$this->element->getName(),
$this->element->getValue(),
$this->element->getAttribs()
) ?> </div>
<? if ( $this->element->hasErrors() ) { ?>
[<p class="<?php echo $this->element->getAttrib('error_class') ?>"> <?php echo $this->formErrors($this->element->getMessages()) ?> </p>]
<? } ?>
</li>
This is only a drafty snippet of code, but should lead you in the direction you aim.
Regards