On a page I have this form:
<form action="/store_collections/create" method="post">
<input type="text" name="data[test]" placeholder="Create new collection" class="span2">
</form>
I submit the form which sends it to the StoreCollectionsController:
<?php
App::uses('AppController', 'Controller');
class StoreCollectionsController extends AppController {
public function create() {
print_r($this->request->data);
}
The print_r returns this:
Array
(
[test] => fdsdata[test]=fds
)
How is this possible?? The field's name is appended to the value. The value should be fds but it's fdsdata[test]=fds.
Any ideas? I removed everything from the controller ($uses...) to see what it could be. All the other forms on the site is fine. Just the data submitted to this controller.
The input control Name property in your example above is missing the name of the model. Are you using the form helper to create the form on the view? Try this in your view:
<?php echo $this->Form->create('StoreCollection'); ?>
<?php echo $this->Form->input('test'); ?>
<?php echo $this->Form->end('submit'); ?>
Related
My function is when I will check then 1 will insert and if not then by default the field will fill with 0.At the time of insert problem is not happen, it store '1' to db but at the time of update with check box it not able to send '1' again to db, it sending 0.
AT THE INSERT TIME
View
.
<?php echo form_checkbox("feature_opd","1", set_checkbox("feature_opd","1")); ?>
<?php echo form_checkbox("feature_gyane","1", set_checkbox("feature_gyane","1")); ?>
<?php echo form_checkbox("feature_iui","1", set_checkbox("feature_iui","1")); ?>
Model
public function add_article($array)
{
return $this->db->insert('table_name',$array);
}
AT THE UPDATE TIME
View
<div>
<?php
if($article->feature_opd == 1)
{?>
<input type="checkbox" name="feature_opd" checked >
<?php
}
else
{?>
<input type="checkbox" name="feature_opd" >
<?php
}
?>
</div>
<div>
<?php
if($article->feature_gyane == 1)
{?>
<input type="checkbox" name="feature_gyane" checked>
<?php
}
else
{?>
<input type="checkbox" name="feature_gyane" >
<?php
}
?>
</div>
<div>
<?php
if($article->feature_iui == 1)
{?>
<input type="checkbox" name="feature_iui" checked>
<?php
}
else
{?>
<input type="checkbox" name="feature_iui" >
<?php
}
?>
</div>
Model
public function update_article($article_id , array $article)
{
return $this->db
->where('id',$article_id)
->update('ivf_productsettings', $article );
}
My Query is how I will update db with check box. I am facing problem to update with check box in Codeigniter, Please suggest me , if my code going to tough then please suggest me in simple way how to update with check box in codeigniter .I am new in ci .
You can create your update checkboxes like so:
<?php echo form_checkbox("feature_iui","1", set_checkbox("feature_iui","1", $article->feature_iui)); ?>
The default value will apply from the db.
Aside from that there is nothing aside from user input preventing the checkboxes from being properly sent. Thus, any code that is causing a 0 being put in the db is probably due to how you are processing the checboxes in your controller update function. As I've said in the comments, checkboxes only submit 1's never 0's.
I am able add/edit/delete with a form created using Cakephp FormHelper.
But I want to use my own form without html helper. And it is a big form. In edit, I set an entity to view. I gave names to elements same as column names.
But the values are not set.
The view is in a plugin.
Can anybody please tell me how to automatically set values in a user-defined form.
I have created a form tag in the form as follows, hoping that "context" will be set and values will appear
<?php echo $this->Form->create( $company ); ?>
and also end , end of the form.
<?php echo $this->Form->end(); ?>
In between the form tags there are many html form elements.
I am posting one of them for which i have set the name same as column name
<div class="form-group panel">
<h6 for="pt-name">Former Name1</h6>
<input type="text" name="company_former_name_one" placeholder="Former Name 1" class="form-control">
</div>
My Controller Code :
public function profile( $id = null )
{
$company = $this->Companies->get( $id );
if ($this->request->is(['post','put']))
{
$post = $this->Companies->patchEntity($post, $this->request->data);
// $post->modified = date("Y-m-d H:i:s");
if( $this->Companies->save($post) )
{
}
}
$this->set('company', $company);
}
CakePHP is not magic
i cannot put the form helpers now, given the time line
The simplest and fastest way to do what you're asking is to use helpers.
I was wondering if there is any default way to set form "context" when not using form helpers
No.
For that to work would require the framework to parse the static html from the template, figure out which bits are supposed to be dynamic, apply some magic/assumptions and then dump the html. CakePHP does not work in this way.
Use helpers
Instead of static html, use appropriate helper methods. For example FormHelper::text
<div class="form-group panel">
<h6 for="pt-name">Former Name1</h6>
<?= $this->Form->text('former_name1') ?>
</div>
Which will generate something like:
<div class="form-group panel">
<h6 for="pt-name">Former Name1</h6>
<input name="former_name1" value="default" type="text">
</div>
This is my add.tcp
...
<?php
echo $this->Form->create('Group');
echo $this->Form->input('group_id', array('label' => 'ID'));
echo $this->Form->input('group_desc', array('label' => 'Group Description'));
echo $this->Form->end('Save');
?>
Table name: groups
Table fields: group_id, group_desc
PK: group_id
This is my controller
...
class GroupsController extends AppController {
public $helper = array('Html', 'Form', 'Session');
public $components = array('Session');
public function add() {
if ($this->request->is('post')) {
if ($this->Group->save($this->request->data)) {
$this->session.setFlash('');
$this->redirect(array('action' => 'index'));
}
}
}
}
When I display this view on the browser, there was nothing for the field group_id but there was for the group_desc, the HTML source for the look like this
...
<form action="/cakephp/index.php/groups/add" id="GroupAddForm" method="post" accept-charset="utf-8" name="GroupAddForm">
<div style="display:none;">
<input type="hidden" name="_method" value="POST">
</div><input type="hidden" name="data[Group][group_id]" id="GroupGroupId">
<div class="input text">
<label for="GroupGroupDesc">Group Description</label><input name="data[Group][group_desc]" maxlength="15" type="text" id="GroupGroupDesc">
</div>
<div class="submit">
<input type="submit" value="บันทึก">
</div>
</form>
Why it was hidden?
CakePHP automagically determines that you do not want a user to manually enter an ID, as such hides it for you.
If you would like to force ID field to be shown, set the type to text:
echo $this->Form->input('group_id', array('type' => 'text', 'label' => 'ID'));
Because primary key inputs are hidden by default.
CakePHP creates the primary key for you on add as auto increment INT or uuid CHAR
Manually creating your primary keys is not recommended.
you can change the hidden type to text:
echo $this->Form->input('group_id', array('label' => 'ID', 'type' => 'text'));
As per your database structure I want to suggest something:
Your group table has group_id as primary key
make it auto_increment from database and there is no need to put that id manually on add form, it will be saved automatically. Form fields generate as per model structure.
I'm building a non-javascript version of a website, as there are a number of customers who don't have javascript enabled. On this site, the customer selects what country they will be visiting and then displays the data related to that country accordingly.
I've managed to get this to work. They use a HTML drop down menu to select the country, click on Submit, and the page reloads with the data related to the selected country. However, it does not change the country displayed in the HTML drop down menu, so when the page reloads it reverts back to "Select A Country".
What I would like to have happen is that if you clicked on United Kingdom from the drop down box for example, when the page reloads the drop down should display United Kingdom.
Here is the code I am currently using for the view file:
<form name="countryselect" action="/selected-country/" method="post">
<select id="country-list" name="countryselected">
<?php foreach($countries as $coun) { ?>
<option value="<?php echo $coun['Tariff']['countryslug']; ?>"><?php echo $coun['Tariff']['countryname']; ?></option>
<?php } ?>
<input type="submit" value="Submit" />
</select>
</form>
And in my controller file I am using this:
$countries = $this->Tariff->find('all', array('conditions' => array('Tariff.gsmid' => '1')));
$this->set('countries', $countries);
if (!isset($_POST['countryselected'])) {
} else {
$countryselect = $_POST['countryselected'];
$tarcounselect = $this->Tariff->find('first', array('conditions' => array('Tariff.countryslug' => $countryselect)));
$this->set('tarcounselect', $tarcounselect);
}
Cheers!
If you use Cake, you should not build the form and the select manually but use the Cake FormHelper instead. It will then keep the selected country automatically:
Controller:
$this->set('countries', $this->Tariff->find('list', array('conditions' => array('Tariff.gsmid' => '1'), 'fields' => array('countryslug', 'countryname'))));
View:
<?php
echo $this->Form->create();
echo $this->Form->select('Tariff.countryslug', $countries);
echo $this->Form->end(__('Submit'));
?>
And then to retrieve the selected country in the controller:
if($this->request->is('post'))
{
$countryslug = $this->request->data['Tariff']['countryslug'];
}
I can get Cake to output a submit button using the following PHP:
<?php echo $this->Form->end(__('Submit')); ?>
which outputs this HTML:
<input type="submit" value="Submit">
but I want to use a specific input class to get the following:
<input type="submit" value="Submit" class="some class">
is this possible?
Thank you :).
Yes it's possible :
<?php
echo $this->Form->submit(__('Submit',true), array('class'=>'some class'));
echo $this->Form->end();
?>
Which is documented here.
This should do the trick:
<?php echo $this->Form->end(array('label' => __('Submit', true), 'class' => 'some class')); ?>