How to use trim() function in CakePHP 2 form when adding data?
<?php
echo $this->Form->input('service_id');
echo $this->Form->input('name');
echo $this->Form->input('unit');
echo $this->Form->input('price');
echo $this->Form->input('date');
?>
In your controller, before you save() the data:
$this->data['YourModelName'] = array_map('trim', $this->data['YourModelName']);
You can also do it for each field individually instead, if you wish.
Related
I just started using Code Igniter framework and also just started learning on PHP OOP. I came across something when coding for the forms.
In a form if I have two buttons that would lead to different pages, what would be the most suitable way to do it? I found two ways. The first is to have a dynamic action/link, let's call it method A:
Method A
Variable $form_link is 'form_link'.
(View) main_user_view.php
<?php echo form_open($form_link); ?>
<?php echo form_button($add_user_button); ?>
<?php echo form_button($delete_user_button); ?>
<?php echo form_close(); ?>
(Controller) User.php
public function form_link()
{
// Value of button clicked
$form_submitted = $this->input->post('submit_form');
if($form_submitted == 'add_user')
{
redirect('User/add_user');
}
elseif($form_submitted == 'delete_user')
{
redirect('User/delete_user');
}
elseif($form_submitted == 'back')
{
redirect('User');
}
}
And the other way is instead of having a second button I would use an anchor and make an absolute path for it.
Method B
Variable $form_link is 'add_user' which is a function in the controller.
(View) main_user_view.php
<?php echo form_open($form_link); ?>
<?php echo form_button($add_user_button); ?>
<?php echo anchor('add_delete_user/delete_users_view', 'Delete', array('class'=>'btn btn-info', 'role'=>'button'));?>
<?php echo form_close(); ?>
The only problem I have with method A is that if in the form I have input fields, I cannot get the data through POST as redirect does not carry over the data to other functions. I resolved that by using method B where the anchor would lead to the function I want whereby I can get the POST data.
So my main question is, should I use method B instead whenever I have two or more buttons in a form?
You have to use button names for form post actions,
public function form_link()
{
if($this->input->post('add_user'))
{
redirect('User/add_user');
}
if($this->input->post('delete_user'))
{
redirect('User/delete_user');
}
}
What my opinion is also to use the Method B. To make the URL more nicer you can use custom routing (which is located at 'application/config/routes.php')
I likes to assign posted values to a variable,
In view i wrote the code,
<?php echo $this->Form->create('Register'); ?>
<fieldset>
<?php echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->input('Confirm password');
?>
</fieldset>
Form->end(__('Submit'));
and in controller,
if($this->request->is('post')){
//print_r($this->data);
$uname = $this->request->data('username');
echo "uname".$uname;
exit;
}
The problem is that i didnt get the value on $uname.
Make sure your debug is enabled. Use this - pr($this->request->data); to see what's in that variable. The CakePHP form naming data usually looks something like this
$this->request->data['Formname']['fieldname']
In your case it should be
$this->request->data['Register']['username']
please try using this code
$this->params['form']['YOUR_VARIABLE_NAME']
or
$this->data['FORMNAME']['YOUR_VARIABLE_NAME']
<?php echo form_dropdown('ord_id',$pacc_ord, $value['pac_cod'], 'id="IdPac" onchange="func1(this),func2(this, echo $value['prod_id'];);"')?>
i need to print the value of $value['prod_id'] (number)
in my function arguments. but it doesn't work
how can i solve that?
<?php echo form_dropdown('ord_id',$pacc_ord, $value['pac_cod'], 'id="IdPac" onchange="func1(this),func2(this, '.$value['prod_id'].');"')?>
You don't have to use echo when passing the values as parameters. Just concatenate (using '..') the prod_id into the last parameter.
I have a map on my site that has plots placed on it using data from my table. The plots are placed with javascript as so...
addPostCode('<?php echo $array[$UID][0]; ?>');
addPostCode('<?php echo $array[$UID][1]; ?>');
addPostCode('<?php echo $array[$UID][2]; ?>');
Somehow I need to see if the variable exists in my table, and then if so, generate an additional line and increment my array number, so based on the above 3 records, the next line would be...
addPostCode('<?php echo $array[$UID][4]; ?>');
Can I ask if this is possible?
You just need to loop through all of the members of the $array[$UID] variable.
<?php
foreach($array[$UID] as $thing) {
$encodedThing = json_encode($thing);
echo "addPostCode($encodedThing);\n"
}
?>
I used json_encode() because it does whatever escaping is necessary to make your data JavaScript-compatible.
If I understand you well, this is what you would do:
addPostCode('<?php $i=0; while (isset( $array[$UID][$i])) { echo $array[$UID][$i]; $i++ ; } ?>');
That would replace all your addPostCode() calls also.
Trying to call the projects controller and the editproject function and pass an id number. Can anyone tell me why the second line doesn't work? When I echo the value in the first line, it does give me the correct integer as a string
<?php echo $list[0]->id; ?>
<?php echo form_open('projects/editproject/',$list[0]->id ) ;?>
The error I keep getting is "Missing argument 1 for Projects::editproject()" My editproject function is function editproject($id).
I did try:
<?php echo $list[0]->id; ?>
<?php $pdata = (int)$list[0]->id; ?>
<?php echo form_open('projects/editproject/',$pdata ) ;?>
Thinking the call to the controller needed a variable for the data. Same error message as above. THanks for any help.
Did you mean to do this instead ?
<?php echo form_open('projects/editproject/'.$list[0]->id ) ;?>
The second argument of form_open() accepts an associative array of attributes, hence, you're mistakenly passing in the id into the second argument when it needs to be concatenated with the url instead.