I have created a registration form that has a dropdown field, but I am unable to get it to link into the database to register the selection. I have form validation turned on, but it keeps saying that no value has been selected.
My other inputs work, as they're user entered. However, the values in this dropdown do not register as any value. Any assistance would be appreciated.
Thanks!
View
<div class = "form-group">
<?php echo form_label('Mobile Carrier'); ?><br/><!--Form Label-->
<?php
$data = array(
'None' => 'None',
'att' => 'AT&T',
'verizon' => 'Verizon',
'sprint' => 'Sprint',
'tmobile' => 'T-Mobile'
);
?>
<?php echo form_dropdown('Mobile Carrier', $data, 'None'); ?>
Controller
public function register(){
$this->form_validation->set_rules('mobile_carrier', 'Mobile Carrier', 'required'); }
User Model
public function create_user(){
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email' => $this->input->post('email'),
'phone_number' => $this->input->post('phone_number'),
'mobile_carrier' => $this->input->post('mobile_carrier'),
'username' => $this->input->post('username'),
'password' => $encrypted_pass
);
$insert_data = $this->db->insert('users', $data);
return $insert_data;
}
you are assigning the wrong name attribute when creating the dropdown list:
<?php echo form_dropdown('Mobile Carrier', $data, 'None'); ?>
the name attribute must match your $this->input->post('mobile_carrier') in your model. so the correct use would be:
<?php echo form_dropdown('mobile_carrier', $data, 'None'); ?>
more information: https://www.codeigniter.com/userguide3/helpers/form_helper.html, scroll to form_dropdown([$name = ''[, $options = array()[, $selected = array()[, $extra = '']]]])
Related
I have a question about cakePHP. I create two drop down lists in my view. When the user changes the value in one list, I want the second to change. Currently, I have this working like this: An on click event fires when the user selects from list box one. This fires a jQuery ajax function that calls a function from my controller. This is all working fine, but how do I re-render my control, asynchronously (or, view)? I i know I could just serialize the array to json and then recreate the control in javascript, but there seems like there should be a more "CakePHP" way. Isn't that what render is for? Any help would be great. Here's the code I have so far:
jQuery:
function changeRole(getId){
$.ajax({
type: 'POST',
url: 'ResponsibilitiesRoles/getCurrentResp',
data: { roleId: getId },
cache: false,
dataType: 'HTML',
beforeSend: function(){
},
success: function (html){
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
}
});
View:
<?php
echo 'Roles:';
echo'<select name="myOptions" multiple="multiple">';
foreach ($rolesResponsibility as $role) {
echo' <option onclick="changeRole(this.value);" value="'; echo $role["ResponsibilitiesRole"]["role_id"]; echo '">'; echo $role["r"]["role_name"]; echo '</option>';
}
echo '</select>';
echo 'Responsbility:';
echo'<select name="myOptionsResp" multiple="multiple">';
foreach ($respResponsibility as $responsibility) {
echo' <option value="'; echo $responsibility["responsibility"]["id"]; echo '">'; echo $responsibility["responsibility"]["responsibility_name"]; echo '</option>';
}
echo '</select>';
?>
Controller function:
public function getCurrentResp(){
$getId = $this->request->data['roleId'];
$responsibilityResp = $this->ResponsibilitiesRole->find('all',
array("fields" => array('role.role_name','ResponsibilitiesRole.role_id','responsibility.*'),'joins' => array(
array(
'table' => 'responsibilities',
'alias' => 'responsibility',
'type' => 'left',
'foreignKey' => false,
'conditions'=> array('ResponsibilitiesRole.responsibility_id = responsibility.id')
),
array(
'table' => 'roles',
'alias' => 'role',
'type' => 'left',
'foreignKey' => false,
'conditions'=> array('ResponsibilitiesRole.role_id = role.id')
)
),
'conditions' => array ('ResponsibilitiesRole.role_id' => $getId),
));
$this->set('respResponsibility', $responsibilityResp);
//do something here to cause the control to be rendered, without have to refresh the whole page
}
The js event is change fired on the select tag and NOT click
You can use the Form Helper to build your form.
Pay attention Naming things following the cakephp way.
Because your code is a bit confused i will make other simple example:
Country hasMany City
User belongsTo Country
User belongsTo City
ModelName/TableName (fields)
Country/countries (id, name, ....)
City/cities (id, country_id, name, ....)
User/users (id, country_id, city_id, name, ....)
View/Users/add.ctp
<?php
echo $this->Form->create('User');
echo $this->Form->input('country_id');
echo $this->Form->input('city_id');
echo $this->Form->input('name');
echo $this->Form->end('Submit');
$this->Js->get('#UserCountryId')->event('change',
$this->Js->request(
array('controller' => 'countries', 'action' => 'get_cities'),
array(
'update' => '#UserCityId',
'async' => true,
'type' => 'json',
'dataExpression' => true,
'evalScripts' => true,
'data' => $this->Js->serializeForm(array('isForm' => false, 'inline' => true)),
)
)
);
echo $this->Js->writeBuffer();
?>
UsersController.php / add:
public function add(){
...
...
// populate selects with options
$this->set('countries', $this->User->Country->find('list'));
$this->set('cities', $this->User->City->find('list'));
}
CountriesController.php / get_cities:
public function get_cities(){
Configure::write('debug', 0);
$cities = array();
if(isset($this->request->query['data']['User']['country_id'])){
$cities = $this->Country->City->find('list', array(
'conditions' => array('City.country_id' => $this->request->query['data']['User']['country_id'])
));
}
$this->set('cities', $cities);
}
View/Cities/get_cities.ctp :
<?php
if(!empty($cities)){
foreach ($cities as $id => $name) {
?>
<option value="<?php echo $id; ?>"><?php echo $name; ?></option>
<?php
}
}
?>
I have a page with a list of books, and every row has a checkbox. (I'm using cakephp 1.2).
I want to let the user to save multiple books.
For now I have this checkbox in my view (in a cycle, where I get the list of all books):
<?php echo $form->input('salva', array('value' => $cdBiblio['CdBiblio']['codiceBiblio'], 'label' => '', 'type' => 'checkbox')); ?>
This form:<?php echo $form->create('cd_biblios', array('action' => 'save')); ?>
And this button:
<?php echo $form->end('Add All');?>
In the controller, I have something like this for debugging:
debug($this->data);
foreach($this->data['cd_biblios']['salva'] as $item) {
debug($item);
}
But it's not working.
I noticed that it takes only the last book id of the list with debug($this->data); and if I check more than 1 book, it show 0 or the last book id, for example:
Array (
[cd_biblios] => Array
(
[salva] => 0
) )
SOLVED:
In the view I use Danial's code.
In the controller I use this code:
if(!empty($this->data)) {
$item=$this->data;
debug($item);
$dim=count($item['Model']['field']);
$i=0;
for ($i=0;$i<$dim;$i++)
if ($item['Model']['field'][$i]!=0)
{
$data = $this->Session->read('libri');
$data[] = $item['Model']['field'][$i];
$this->Session->write('libri', $data);
}
$this->Session->setFlash(__('I libri sono stati salvati', true));
$this->redirect($this->referer());
}
In View:
<?php
foreach($array as $each_elem) {
echo $form->checkbox(
'Model.field',
array(
'id'=>'abcd_'.$each_elem['Model']['id'],
'value' => $each_elem['Model']['id'],
'hiddenField' => false,
'name' => 'data[Model][field][]',
'label' => false,
'div' => false,
));
}
?>
On submit the form, you will get the checked values in controller in $this->request->data
You have a conception problem.
Your checkboxes must be inside a form, because they are input tags. So, to send those values to the controller you'll need to submit that form. In your code, i see that "Add All" is just a link. And a link won't submit the form.
Change your link to a button :)
Hope this helps
I'm using Lithium with MySQL.
I have a Users model that hasOne Contacts.
The Contacts model belongsTo Users.
I've listed a very basic version of my code, below.
My questions:
When I edit a user and submit the form, how do I make Users::edit save the Contact data, as well?
Also, how do I display contacts.email in the Users edit view?
models/Users.php
<?php
namespace app\models;
class Users extends \lithium\data\Model {
public $hasOne = array('Contacts');
protected $_schema = array(
'id' => array('type' => 'integer',
'key' => 'primary'),
'name' => array('type' => 'varchar')
);
}
?>
models/Contacts.php
<?php
namespace app\models;
class Contacts extends \lithium\data\Model {
public $belongsTo = array('Users');
protected $_meta = array(
'key' => 'user_id',
);
protected $_schema = array(
'user_id' => array('type' => 'integer',
'key' => 'primary'),
'email' => array('type' => 'string')
);
}
?>
controllers/UsersController.php
<?php
namespace app\controllers;
use app\models\Users;
class UsersController extends \lithium\action\Controller {
public function edit() {
$user = Users::find('first', array(
'conditions' => array('id' => $this->request->id),
'with' => array('Contacts')
)
);
if (!empty($this->request->data)) {
if ($user->save($this->request->data)) {
//flash success message goes here
return $this->redirect(array('Users::view', 'args' => array($user->id)));
} else {
//flash failure message goes here
}
}
return compact('user');
}
}
?>
views/users/edit.html.php
<?php $this->title('Editing User'); ?>
<h2>Editing User</h2>
<?= $this->form->create($user); ?>
<?= $this->form->field('name'); ?>
<?= $this->form->field('email', array('type' => 'email')); ?>
<?= $this->form->end(); ?>
Not many people know this but with lithium you can bind a form to multiple objects.
In your controller, return both a user and a contact object. Then in your form:
<?= $this->form->create(compact('user', 'contact')); ?>
You then render a field form a specific object like this:
<?= $this->form->field('user.name'); ?>
<?= $this->form->field('contact.email'); ?>
When the user submits the form, the data for both objects will be stored as:
$this->request->data['user'];
$this->request->data['contact'];
You can use this information to update the database as you would normally. If you only want to save the information if data from both objects are valid, you can call validate like this:
$user = Users::create($this->request->data['user']);
if($user->validates()) {
$userValid = true;
}
$contact = Contacts::create($this->request->data['contact']);
if($contact->validates()) {
$contactValid = true;
}
if($userValid && $userValid){
// save both objects
}
Hope that helps :)
How can I create two radio buttons with one being preselected based on the value of $foo? The snippet below creates them fine but does not select either of the two buttons.
$options = array('standard' => ' Standard','pro' => ' Pro');
$attributes = array(
'legend' => false,
'value' => false,
'checked'=> ($foo == "pro") ? FALSE : TRUE,
);
echo $this->Form->radio('type',$options, $attributes);
It's simple.. use the default value to $foo:
$options = array(
'standard' => 'Standard',
'pro' => 'Pro'
);
$attributes = array(
'legend' => false,
'value' => $foo
);
echo $this->Form->radio('type', $options, $attributes);
As you can see on the documentation:
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::radio
you should preselect the value for any form field from the controller
#see http://www.dereuromark.de/2010/06/23/working-with-forms/ "Default Values"
This is the way to go
$attributes = array();
$options = array('standard' => 'Standard', 'pro' => 'Pro');
if($foo === 'pro') {
$attributes['default'] = 'pro';
}
echo $this->Form->radio('type', $options, $attributes);
A better Solution is to set the defaults in the controller as Mark has pointed. That way you can set defaults at the end of your controller's action like...
Let's assume your Model is Member with membership_type field
$this->data['Member']['membership_type '] = 'pro';
For CakePHP 3.x, the following syntax should work.
$options = array('Y'=>'Yes','N'=>'No');
$attributes = array('div' => 'input', 'type' => 'radio', 'options' => $options, 'default' => 'Y');
echo $this->Form->input('add to business directory',$attributes);
HTH
My controller action is:
function add() {
if (!empty($this->data)) {
$this->Customer->create();
if ($this->Customer->save($this->data)) {
$this->Session->setFlash('A new Customer has been added');
$this->redirect(array('action'=>'index'));
}
else {
$this->Session->setFlash('The customer cannot be added this time. Try again later.');
$this->redirect(array('action'=>'index'));
}
}
}
My model is:
class Customer extends AppModel {
var $name = 'Customer';
var $validate = array(
'name' => array(
'length'=> array(
'rule' => array('between', 4,50),
'message' => 'Name must be minimum 4 and maximum 50 characters long.'
),
'checkUnique'=> array(
'rule' => 'isUnique',
'message' => 'This Name is already registered'
)
));
and this is my view:
<div class="customers form">
<?php echo $form->create('Customer',array('action'=>'add'));?>
<fieldset>
<legend><?php __('Add Customer');?></legend>
<?php
echo $form->input('Customer.name');
echo $form->input('Customer.balance',array('type'=>'hidden','default'=>0));
?>
</fieldset>
<?php echo $form->end('Submit');?>
</div>
every time I submit the form it splashes:
The customer cannot be added this time. Try again later.
For the hidden input field use 'value' instead of 'default' in your view:
$form->input('Customer.balance', array('type' => 'hidden', 'value' => 0));