I'm using CodeIgniter and I have 2 questions:
How do I call result from model to name property in any form inputs?
How do I get the dynamic name property posted in Controller?
This is what I tried in my HTML:
<input type="text" name="saa_<?php echo $row1['Tube_No']; ?>" value="<?= $row1['Pin_No']; ?>">
And this is what I tried in my controller:
$data = array(
'SA_No' => $this->input->post('saa_.$row1'),
);
How do i get my form input name property get posted to controller.
First you need to get the value of $row1['Tube_No']
$valueDefinedWhenRenderingTheView = $row1['Tube_No'];
then you need to access it like this:
$data = array(
'SA_No' => $this->input->post('saa_' . $valueDefinedWhenRenderingTheView)
);
because, when your defining the input name to be the concatenation of "saa_" and the value of $row1['Tube_No'].
Related
I am using ion auth to edit some user data.This is the code
<h1><?php echo lang('edit_user_heading');?></h1>
<p><?php echo lang('edit_user_subheading');?></p>
<div id="infoMessage"><?php echo $message;?></div>
<?php echo form_open(uri_string());?>
<p>
<?php echo lang('edit_user_fname_label', 'first_name');?> <br />
<?php
$data = array(
'name' => 'first_name',
'value' => set_value('first_name', $first_name),
'class' => 'form-control'
);
echo form_input($data);?>
</p>
This produces the input plus the value but without the input class
echo form_input($first_name);?>
My first code snippet is an attempt at adding input class and populating the input field. This gives me a conversion error Message: Array to string conversion
How should i add form class inside the edit form field?
The error is not coming from trying to add a class to your input field, but from set_value(), another CI helper function. The variable $first_name is sent as an array to the view, hence the error message.
As we don't know, which array value you need, you could try to access the first one with set_value('first_name', $first_name[0]), but this depends how your array (or array of objects) is structured, a echo '<pre>';print_r($first_name); die(); helps you to debug this.
CI set_value() is defined as described here:
set_value($field[, $default = ''[, $html_escape = TRUE]])
Parameters:
$field (string) – Field name
$default (string) – Default value
$html_escape (bool) – Whether to turn off HTML escaping of the value
How to save dynamical fields to database the problem is i don't know the names of fields we have module the user will add fields with name dynamically and i will save that fields names in database when the user checks the form for example he added fields in posts form so i want to save that fields data to database.Here's my code
<form>
<input type="text" name="firstname" id="firstname">
<?php foreach($extrafields as $extrafields1 )
{
?>
<input type="<?php $extrafields1->type; ?>" name="<?php $extrafields1->name; ?>" id="<?php $extrafields1->id; ?>" <?php $extrafields1->extraparameters; ?>>
<?php
}
</form>
If you can't define the name of the columns in the database, you'll have to save the data as JSON. So you'd create a column on your model called extra_parameters that would be a text column. When you post the form data, you'd need to update the model something like this:
//remove the expected parameters so you only have the extra ones
$extraparams = $request->except(['firstname','...']);
$model->extra_parameters = $extraparams;
You should also cast the column to an array in your model. This allows you to set $model->extra_parameters using an array, and it will automatically convert it to JSON when saving to the DB. It also lets you use $model->extra_parameters['param_name'] to access the fields.
//in your model file
protected $casts = [
'extra_parameters' => 'array'
];
see the array casting section here for more: https://laravel.com/docs/master/eloquent-mutators#attribute-casting
What I am trying to do is show the form validation errors if there are any and prepopulcate the form.
View
<input type="hidden" value="<?php echo $_GET['project_id']; ?>" name="project_id" id="project_id">
<input type="text" value="<?php echo set_value('name'); ?>" name="name" id="name">
Controller
$validation_rules = $this->access_m->rules;
$this->form_validation->set_rules($validation_rules);
if($this->form_validation->run()){
// process the form
}else{
$data= array(
'subview'=>'access/access',
);
$this->load->view('layout', $data, FALSE);
}
Now what the problem is, I can prepopulate the value for the name, but how do I prepopulate the value for the project_id field. Since the value for the field is extracted from the GET method, is there any way I can pass it back to the view so that I can use it again.
I could use a redirect and add the project ID to the url as example.com/access?project_id=23 but then I would loose the values to prepopulate to the rest of the form fields.
Any solutions ??
Create method that accepts parameters example.ufo/controller/accept/<id>
From now on you don't need hidden field, your id is accesible within the method more info is here http://ellislab.com/codeigniter/user-guide/general/controllers.html#passinguri
example:
<?php
class User extends CI_Controller {
public function accesible( $id = false) {
//necessary checks, is id valid? is it numeric? is id in database?... does user have permission to work with element with this ID?
echo $id;
}
}
?>
i am trying to populate two form fields from data that is retrieved from a database, in order for the user to update them. The table is called records and it is quite simple:
Record_ID
title
content
My model:
function get_data()
{
$r = $this->uri->segment(3);
$query = $this->db->get_where('records', array('Record_ID' => $r));
return $query->result();
}
My controller:
function set_values()
{
$data = $this->entries_model->get_data();
$this->load->view('update_view', $data);
}
and my update record view:
<?php
echo form_open('site/update',$data);?>
Title:
<?php echo form_input('title',set_value('title'));?>
Content:
<?php echo form_input('content',set_value('content'));
echo form_submit('submit', 'Submit');?>
<?php echo form_close();?>
The problem is that i get the following error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/update_view.php
Line Number: 10
My question is twofold:
How do i access this data in my view form and
how do i populate the respective fields with it.
I am new to Codeigniter, my questions may look simplistic but any help would be appreciated. Thanks in advance.
There are a few things going on here:
$data is an array or object that is passed to a view. It's ELEMENTS are then available as variables in the view. So, $data['myelement'] = 'somevalue' in the controller would be accessed as $somevalue in the view.
If you pass a 2nd parameter to the form_open() method, it is expected to be a key/value pair of attributes for the tag that will be generated. like, array('class' => 'form_class', 'id' => 'form_id')
If you want to set the values of your form inputs, use the view helper function set_value(). In your case, use the controller to set elements in the $data array you'll pass to the view. $data['form_values'] = array('title' => $title, 'content' => $content);
Then, in the view:
You should pass a array to your view file. So replace:
$data = $this->entries_model->get_data();
with:
$data['entries_data'] = $this->entries_model->get_data();
and on your view file replace:
echo form_open('site/update',$data);?>
with:
echo form_open('site/update',$entries_data);?>
first you need to pass data in proper way
replace
$data = $this->entries_model->get_data();
with:
$data['data'] = $this->entries_model->get_data();
for setting value in set_value you need to do the in-line condition check to check either data is an object or not if object then put value other wise just empty
<?php echo form_input('title',set_value((is_object($data)?$data->title:'')));?>
you have to do the same thing for your all form fields
Jcory has answered your question but let me add a little to it.
In you model instead of return $query->result(); do this return $query->row(); this is because using returning a return object requires that you should loop through the resultset in your view
Instead of $data = $this->entries_model->get_data(); do this $data['entry'] = $this->entries_model->get_data();
In your view do this <?php echo form_input('title',set_value('title',$entry->title));?>
I hope these changes may solve the problem
How might one use the url_for or link_to helpers to create a link to the same page passing along all GET parameters?
I have a search page which accepts multiple parameters as filters to hide certain results. For example, search.html?query=abc&people=1&groups=0 would search for all items containing 'abc' that aren't groups. I would therefore like a link which toggles these filters, passing the rest of the parameters unchanged to the current page. In the above example, links would be created to search.html?query=abc&people=1&groups=1 and search.html?query=abc&people=0&groups=0.
I am wondering if there is any way to specify 'this' as a route, such that if the route or page changed, the code wouldn't have to.
Moreover, how can one pass all parameters to the helper? $sf_params can be used to access all the parameters, and the 'query_string' property can be passed to the helper, but is there any method of combining the two short of creating the string manually?
GET parameters can be passed to a link_to method as an array in the third argument, or url_for in the second. This is illustrated as follows:
link_to('Text to display', 'routename', array('group' => 0, 'people' => 1), $link_attributes);
url_for('routename', array('group' => 0, 'people' => 1));
This is hidden in the source code. Here, $params is the list of GET parameters, and $options HTML attributes to add to the tag.
function link_to2($name, $routeName, $params, $options = array())
function url_for2($routeName, $params = array(), $absolute = false)
The $sf_params function can be converted to a suitable array for passing with getAll(). See the documentation here. Note that this is different from previous versions of Symfony. Also be aware you might have to call getRawValue() from within a template:
$params = $sf_params->getRawValue()->getAll();
$sf_context->getInstance()->getRouting()->getCurrentRouteName() can then be passed as $routeName to get the current route. Thanks to Tom for this
put vars in hidden form data types to pass them on
<form action="page.php" method="GET">
<input name="people" type="hidden" value="<?php echo htmlspecialchars($_GET['people'], ENT_QUOTES); ?>">
<input name="groups" type="hidden" value="<?php echo htmlspecialchars($_GET['groups'], ENT_QUOTES); ?>">
<input name="query" type="text" value="some_data">
<input name="send" type="submit" value="send">
</form>