CodeIgniter: Accessing array values using set_value() - php

New to CodeIgniter, apologies this is simple stuff. I have a controller with an array which holds an array of values and associative fields.
Controller
$tests = array( "ID" => "1", "Fcilty_typ" => "MO");
View
<input type="text" name="Fcilty_typ" value="<?php echo set_value('Fcilty_typ','Fcilty_typ')?>"/>
How can I manipulate the array in the controller so it's key=>values are accessible in the view, within the set_vaue(); function.

<input type="text" name="Fcilty_typ" value="<?php echo set_value('Fcilty_typ',$tests['Fcilty_typ']) ?>"/>
you could also do something like this with ci form helper.
$form_fcilty = array(
'name' => 'Fcilty_typ',
'id' => 'Fcilty_typ',
'value' => $tests['Fcilty_typ'],
'maxlength' => '100',
'size' => '50',
);
echo form_input($form_fcilty);
that way you don't have to mix too much html and php to create the form. note you can also do the form_input array with set_value() if you need to show the submitted form value again after a validation fail.

Related

posted value is not displaying when use enctype as multipart/form-data in cakephp

I am working in cakephp.I have created one form that include file upload and textbox nad textarea.
Here, my html code looks like below :
<form action="" id="frmReg" method="post" enctype= "multipart/form-data">
<div style="position:relative; margin-bottom:30px;" class="fildtpart4">
<label><?php __d('statictext', 'Question', false); ?>:</label>
<span>
<textarea class="validate[required]" name="question1['question']" cols="" rows=""></textarea>
<div style="color: red; position:absolute ; bottom:-25px ;" id="charNum"></div>
</span>
<div class="clear"></div>
<div class="fildtpart3">
<label><?php __d('statictext', 'Currect Answer', false); ?></label>
<span>
<input name="question1['currect_ans']" type="text" class="validate[required]" id="" value="">
<input type="file" class="validate[required]" onchange="showMyImage1(this)" name="question1['sponsor_image']" id="" />
<input type="file" class="validate[required]" onchange="showMyImage2(this)" name="question1['question_image2']" id="" />
</span>
<input type="submit" value="submit" name="submit">
<div class="clear10"></div>
</div>
</div>
<form>
when I submit this form then it will shows only image not all data.
In my controller, I have written :
function add_polls()
{
print_r($this->params['form']);exit;
}
Then it gives output like :
array(
[question1]=>array(
[name] => Array
(
['sponsor_image'] => contact.jpg
['question_image2'] => contact.jpg
)
[type] => Array
(
['sponsor_image'] => image/jpeg
['question_image2'] => image/jpeg
)
[tmp_name] => Array
(
['sponsor_image'] => /tmp/phpUK7Vcj
['question_image2'] => /tmp/php3SCWGZ
)
[error] => Array
(
['sponsor_image'] => 0
['question_image2'] => 0
)
[size] => Array
(
['sponsor_image'] => 2305
['question_image2'] => 2305
)
)
)
Here, its not printed question1['currect_ans'] and question1['question'].When I remove enctype from the form then it will shows all value. So how can I resolve this problem?
Note: CakePHP vesrion is 1.3.13.
This is a mixture of how PHP handles files, and you not following the CakePHP conventions, causing CakePHP to "overwrite" your other form data.
You should use the form helper, that way you would avoid the problem in the first place, unless of course you'd fiddle with the field names and use such clashing ones again.
Adding everything in question1, including files, will result in the files being grouped under question1 in $_FILES, just as shown in your question, ie
Array(
[question1] => Array(
[name] => Array(
['sponsor_image'] => contact.jpg
['question_image2'] => contact.jpg
)
// ...
)
)
and the rest of the form data in $_POST using the same key, ie
Array(
[question1] => Array(
['question'] => 'foo',
['currect_ans'] => 'bar'
)
)
You should already smell the upcoming clash.
CakePHP will first fill $params['form'] with $_POST, and then iterate over $_FILES and use the keys to set the file data in $params['form'] too, which will lead to the previously set form data, ie the text inputs, to be overwritten, as both use the key, question1.
https://github.com/cakephp/cakephp/blob/1.3.13/cake/dispatcher.php#L248
https://github.com/cakephp/cakephp/blob/1.3.13/cake/dispatcher.php#L289-L293
tl;dr Follow the naming conventions, or use the form helper
Follow the CakePHP conventions and use the data key (and optionally a model name, which doesn't need to exist) to group the input, that way all data will be properly handled separately and made available via Controller::$data
name="data[question]"
name="data[currect_ans]"
name="data[sponsor_image]"
name="data[question_image2]"
No need to use quotes for nested keys btw.
Even better, as already mentioned, use the form helper, it will automatically create proper fieldnames.
See also
Cookbook > Core Helpers > Form > Field naming convention
Cookbook > Core Helpers > Form > File Fields
So i reproduced your issue and i'm reporting the following.
On my cakephp 3.x your form works great by submitting with 'regular' HTTP post. (I added an <input type="submit"> to your form).
[
'question1' => [
'question' => 'da',
'currect_ans' => 'da',
'sponsor_image' => [
'name' => 'Mobile version.png',
'type' => 'image/png',
'tmp_name' => '/tmp/php3A021O',
'error' => (int) 0,
'size' => (int) 149447
],
'question_image2' => [
'name' => 'Mobile version.png',
'type' => 'image/png',
'tmp_name' => '/tmp/phpi6VWxl',
'error' => (int) 0,
'size' => (int) 149447
]
]
]
So how are you submitting your data? Is it through ajax? What version of cakephp are you using? Try answering these questions so i can help you further on.
Have you tried getting the data using
$postedData = $this->request->data;

CakePHP multiple checkbox array HTML the right way

CakePHP's form generator for checkboxes ... when passing the following into the name:
<?php echo $this->Form->checkbox('checkList[]', array( 'value'=>1,'id' => 'holiday'.$holidaysDays['id'], 'error' => false, 'placeholder' => false,'div'=>false,'label'=>false,'class' => 'approveHolidayCheckbox', 'data-off-text'=>'No', 'data-on-text' =>'Yes', 'hiddenField'=>true) ); ?>
outputs:
<input type="checkbox" name="data[HolidaysApproval][checkList[]]" value="1" id="holiday238" class="approveHolidayCheckbox" data-off-text="No" data-on-text="Yes">
I read here:http://network-13.com/thread/3647-Creating-checkbox-arrays-with-CakePHP that the solution is adding a full stop to the field name (as below), where multiple checkboxes are output on the page. Is this the 'right' way to do this?
Couldn't see this particular scenario anywhere in the documentation.
<?php echo $this->Form->checkbox('checkList.', array( 'value'=>1,'id' => 'holiday'.$holidaysDays['id'], 'error' => false, 'placeholder' => false,'div'=>false,'label'=>false,'class' => 'approveHolidayCheckbox', 'data-off-text'=>'No', 'data-on-text' =>'Yes', 'hiddenField'=>true) ); ?>
Yes this is the correct way of doing this. When CakePHP builds the field names it uses PHP's explode() method. So checklist. essentially does the following:-
$fieldname = explode('.', 'checklist.');
Which results in:-
Array
(
[0] => checkList
[1] =>
)
So you would get inputs with the name data[Model][checklist][].
You can similarly use this for hasMany like fields, e.g. $this->Form->field('Model..name'); which would give you inputs with the name data[Model][][name].
Take a look at the FormHelper and you should easily be able to see how it builds the field names.

PHP Global Multidimensional Array... Or How would I do that?

I'm new to PHP and trying to learn the proper way to do things, I'm currently working on a PHP / HTML form so here is my code for my globa variable:
// General Function
global $variables;
$variables = array(
'login' => array(
'visible' => 'visible',
'required' => 'required',
'position' => get_option('order_username'),
'value' => $_POST['username'],
'error_req' => __('A username is required to register.'),
'error_misspelled' => __('Your username is not properly formatted.'),
'error_unavailable' => __('This username has already been registered.')
),
'province' => array(
'visible' => get_option('show_province'),
'required' => get_option('required_province'),
'position' => get_option('order_province'),
'value' => $_POST['easyreg_province'],
'error_req' => __('You need to fill your province to register.'),
'error_misspelled' => '',
'error_unavailable' => ''
)
);
This works fine and I can access those Variables from every other functions; first I know many people says not to use GLOBAL variables, I don't really understand why because in my case those data will be used and reused inside my form, I would prefer not to redeclare new variables in every function related because those data won't change during the processing.
If it's really not the best way... What could be a good way to do that? As you can see I have about 7 "values" by form "input".
Also, for a GLOBAL variable like that can you "declare" the values from inside a function? What I mean is that if I add a new fields to my form, I would like to have those declaration beside my field, instead of having it in the main PHP (which would also make my code look cleaner).
I would have thought something like that, but it seems to be only declaring it inside the function itself:
function form_display(){
<!-- Email field -->
$GLOBALS['variables']['email'] = array(
'visible' => 'visible',
'required' => 'required',
'position' => get_option('order_email'),
'value' => $_POST['email'],
'error_req' => __('An email is required to register.'),
'error_misspelled' => __('This is not an appropriate email format.'),
'error_unavailable' => __('This email has already been registered.')
);
?>
<div class="regpage_email required"
id="order_<?php echo($GLOBALS['variables']['email']['position']) ?>">
<td id="field_email"><label
for="email"><?php _e('E-mail Address: *') ?></label><br><input
id="email"
type="email"
name="email"
value=""
placeholder=""
required>
</td>
}

Cannot catch value from form_hidden on codeigniter?

I try to create Update Form with form helper CI. everything Work if on form_input. But, when id on form_hidden, it return NULL. this the script
on View
$hidden = array('name'=>'id_hidden','value'=>$datacompany[0]->id);
echo form_hidden($hidden); //I have edited
On Controller
function edit_company()
{
if(isset($_POST['EDIT']))
{
print_r($_POST);//return All value
$isi = array(
'id' =>$this->input->post('id_hidden'),//return null
'nip' =>$this->input->post('nip'),//return value
'nama' =>$this->input->post('nama'), //return value
'golongan' =>$this->input->post('golongan') //return value
);
echo $isi['id']; //the result id is null
}//end if
}//end Function
That Id I need for using on model. How Can I Fix That?
How to Catch ID from form_hidden?
I'm very appreciated Your Answer
thanks
While using my first comment will let you debug it in 3 seconds, Here's the answer :)
You're using the form_hidden in the wrong way.
An array in the form_hidden turns to this (from the docs)
$data = array(
'name' => 'John Doe',
'email' => 'john#example.com',
'url' => 'http://example.com'
);
echo form_hidden($data);
// Would produce:
<input type="hidden" name="name" value="John Doe" />
<input type="hidden" name="email" value="john#example.com" />
<input type="hidden" name="url" value="http://example.com" />
As you see, the 'keys' of the array turn to the names of the fields.
The value is the 'value' of the array. so in your example, you're making two hidden fields.
<input type="hidden" name="name" value="id_hidden">
<input type="hidden" name="value" value="$datacompany[0]->id">
You need to define like this a hidden field in CI:
$hidden = array('id_hidden',$datacompany[0]->id); // a name and value pair for a single instance.
echo form_hidden($hidden);
$hidden = array('id_hidden' => $datacompany[0]->id);
echo form_hidden($hidden);
I think this will fulfill your need.
Or if you want other attributes... Try with this..
$data = array(
'name' => 'username',
'id' => 'username',
'value' => 'johndoe',
'maxlength' => '100',
'type' => 'hidden',
'size' => '50',
'style' => 'width:50%',
);
echo form_input($data);
Depend on your need.

Codeigniter: Setting value of form for an edit page

I am trying to make an edit page where the form fields are populated from the database. I'm trying to get it working by just setting some random text, but I'm having trouble making it show.
I have the following in my controller:
public function edit($item_id) {
$this->data['title'] = "Edit Item";
$this->data['item_title'] = array(
'name' => 'item_title',
'id' => 'item_title',
'type' => 'text',
'value' => 'a title',
);
$this->data['url_slug'] = array(
'name' => 'url_slug',
'id' => 'url_slug',
'type' => 'text',
'value' => 'some-url-slug',
);
$this->template->build('admin/item/form', $this->data);
}
This is my view:
<?php echo form_open('admin/item/update_item', array('id' => 'item_form')); ?>
<input type="text" name="item_title" value="<?php echo set_value('item_title'); ?>" id="item_title" placeholder="Enter a title..."/>
<input type="text" name="url_slug" value="<?php echo set_value('url_slug'); ?>" id="url_slug" placeholder="url-slug-of-the-item"/>
When I go the /edit/id page, the placeholder is still showing and the value is blank. Why is not setting? It works fine when I use it for form validation.
I'm still new to codeigniter, so forgive my ignorance.
Can't you simply use the following?
<input type="text" name="item_title" value="<?= $item_title['value'] ?>" id="item_title" placeholder="Enter a title..."/>
Note: This would be valid for fuelphp; not 100% sure about codeigniter.
I ended up ditching the array and just doing $this->data['item_title'] = 'some title'; in the controller, and in the the view, set_value actually accepts a 2nd parameter, like this:
<?php echo set_value('item_title', $item_title); ?>
This does the trick, although I do get warnings if the variable doesn't exist, so I need to declare them all.
I ended up ditching the array and just doing $data['fullName'] = 'some title'; in the controller, and in the the view, set_value actually accepts a 2nd parameter, like this:
<?php
if(!isset($fullname)) {
$fullname = set_value('fullName');
}
echo form_open(base_url() . "broker/practice_send.html");
echo form_label("Name", "name");
$data = array(
'name' => 'fullName',
'id' => 'name',
'value' => set_value('fullName', $fullname)
);
echo form_input($data);
?>

Categories