HTML input text box vs CakePHP Automagic Form Elements - cakephp

I was manually creating a simple form with one input text box field like this:
<form action="/user/add" method="post">
<input type="text" name="data[user_id]" value="1">
But when I call $this->model->save($this->data) in the Controller,
nothing was saved to the Table.
Only when I used this and the data in the field was written to the database successfully:
$form->create(null, array('url' => '/user/add'));
echo $form->input('user_id', array('label' => 'User ID', 'value' => '1'));

If you want to create the form manually,the name of the input part should be
<input type="sometype" name="data['modelname']['fieldname']" value="somevalue">
And in your code that should be
<form action="/user/add" method="post">
<input type="text" name="data['User'][user_id]" value="1">
See automagic form elements in the cookbook.

Related

get form data wordpress in same page

I am learning word press. I have 2 questions
I have created a page and assigned my template file for it.
1. Question
How to get my form input values in same page or in another page template?
<form id="formId" action="" method="post">
<input style="width:88%" type="text" name="name">
<input type="submit" name="subs" value="Submit" />
</form>
In same file I put:
<?php
if( isset($_POST['subs']) ) {
echo $_POST['name'];
}
?>
I also tried $_SERVER['PHP_SELF']; in action but it's not displayed.
How could I store it in my own table?
When click submit, it refreshes and displays "No Results Found".
2. Question
How to show the submitted values in wp-admin?
Here's the word press code flow I can't solve.
Change Form action to the same page:
<form action="<?php the_permalink(); ?>
try to use
action="<?php echo get_permalink(); ?>"
instead of
action="<?php echo $_SERVER['PHP_SELF']; ?>"
in your form

How to format the name of the checkbox while send by a GET cakephp

I would like to know if it's possible to format the name of the checkbox when I try to send it to my other page via a GET method.
Actually I've got a multiple checkboxes that generate something like:
<div class="checkbox"><input type="checkbox" name="test[]" value="1" id="ResearchTest1" /><label for="ResearchTest1">First Test</label></div>
<div class="checkbox"><input type="checkbox" name="test[]" value="2" id="ResearchTest2" /><label for="ResearchTest2">Second Test</label></div>
<div class="checkbox"><input type="checkbox" name="test[]" value="3" id="ResearchTest3" /><label for="ResearchTest3">Third Test</label></div>
And when I send my form, my URL looks like:
research%3D%26test%3D%26test%5B%5D%3D1%26test%5B%5D%3D2%26test%5B%5D%3D3
Which is :
research=&test=&test[]=1&test[]=2&test[]=3
And what I would like, will be:
research=&test1=1&test2=2&test3=3
Or
research=&test=1&test=2&test=3
Or, much better:
research=&test=1,2,3
Any ideas ?
That is how checkboxes work, if you want test1=1&test2=2&test3=3 then you should name each checkbox individually.
echo $this->Form->checkbox('foo', array('name' => 'test1'));
echo $this->Form->checkbox('foo', array('name' => 'test2'));
echo $this->Form->checkbox('foo', array('name' => 'test3'));
This will make your processing much harder.
You can do research=&test=1,2,3 with JS join(). Why you want to make this difficult for yourself I dont know. You can easily get that same format in the controller doint the GET with implode(',', $theData)
I would recommend using standards for submitting your form and process the data later.
You can define the name of the input using the FormHelper:
echo $this->Form->checkbox('yourInputId', array('name' => 'yourInputName'));
The problem is that I use a multiple checkboxes:
$this->Form->input('tests', array('type' => 'select', 'multiple' => 'checkbox', 'options' => $options));
So I can't rename each checkbox individually.
Maybe I should not use cakephp for this, but try to write my own checkboxes directly in html?

cakePHP creating custom id's for checkboxes

I have a checkbox list that I create using the form helper
echo $form->input('Interest.interest_id', array('label' => __l('Interests'), 'multiple' => 'checkbox'));
It then created for each checkbox and automatic id
eg.
<input id="InterestInterestId1" type="checkbox" value="1" name="data[Interest][interest_id][]">
<input id="InterestInterestId2" type="checkbox" value="2" name="data[Interest][interest_id][]">
Is it possible to have my own unique id that I create for each checkbox? For example customInterestInterestId1, customInterestInterestId2 ...
You should be able to do this:
echo $form->input('Interest.interest_id',
array('label' => __l('Interests'),
'multiple' => 'checkbox',
'id'=>'your_custom_id_')); // add ID to the array
It works for the other auto-magic input types; but I haven't tested it with a multiple check box.
Cake will then generate:
... id="your_custom_id_1" ...
... id="your_custom_id_2" ...

CakePHP Form Helper just generated too much code for me

I am using cakePHP.
I used the cakePHP built-in Form Helper to generate an input text box:
echo $form->input('quote', array('label'=>'Post Number', 'class'=>''));
But when I looked at the HTML source code, I found out these:
<div class="input text">
<label for="ReplyQuote">Post Number</label>
<input name="data[Reply][quote]" type="text" class="" maxlength="12" value="1" id="ReplyQuote" />
</div>
It's really more than enough. I mean the code generated by the Cake built-in Form Helper.
Those DIV tags with class named in a strange naming convention way are not helpful,
because there is space in between the Class name like:
<div class="input text">
Does CakePHP have any options for users to omit those DIV Tags?
Yes.
Check out the options array that you can pass to the FormHelper::input() method. Book reference is at http://book.cakephp.org/view/189/Automagic-Form-Elements
In short, the form helper is adding two distinct classes to the div -- input, and text. If you don't want a div, just do:
echo $form->input( 'quote', array( 'label' => 'Post Number', 'div' => false ) );
Setting the options['class'] value only affects the class selector assigned to the actual input itself (see in your HTML code how the input tag has class=""?)
you can also use $form->text() for input box.

Form helper for creating Radio button in Cakephp

i am trying to create a Radio button using Cakephp like the one the result should resemble like
<div data-attr="radio" id="1">
<label id="label1">Untitled1</label><br/>
<input type="radio" value="option1" id="Radio11" name="Workexperience"/>
<label for="Radio11">Option1</label>
<input type="radio" value="option2" id="Radio12" name="Workexperience"/>
<label for="Radio12">Option2</label>
</div>
how to generate so using Form helper..
Please suggest me..
This might help,
http://book.cakephp.org/view/189/Automagic-Form-Elements#options-before-options-between-options-separator-a-191
For radio type input the 'separator' attribute can be used to inject markup to separate each input/label pair.
Code View
<?php echo $form->input('field', array(
'before' => '--before--',
'after' => '--after--',
'between' => '--between---',
'separator' => '--separator--',
'options' => array('1', '2'),
'type' => 'radio'
));?>
Output:
<div class="input">
--before--
<input name="data[User][field]" type="radio" value="1" id="UserField1" />
<label for="UserField1">1</label>
--separator--
<input name="data[User][field]" type="radio" value="2" id="UserField2" />
<label for="UserField2">2</label>
--between---
--after--
</div>
Looks like $form->radio() should do what you need. I don't know if it will look exactly like your example.

Resources