How to save dynamical fields data to database in laravel5 - php

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

Related

How to give dynamic name in array in input field?

I have a form which will have dynamic value and it will check that property id and then save it to the database.
For example in my database there is a table with title having the id=1, type having the id=2, description having the id=3, and after the form is submitted it will check if the field is title or type or description and it will save it in database that is if it is title it will save value of field title with propertyid value 1.
<form method="post" action="something.php">
<input type="text" name="field[][title]" value="edison">
<input type="text" name="field[][type]" value="book">
<input type="text" name="field[][description]" value="some description">
</form>
it is not inputting normal array in php with using foreach, I am not understanding how to get the value inside the index of the array to check with the sql database, that is to check if the field[][title]" then title has id 1 and if the field is field[][description]" it will check the sql for the property id of description that is 3
You need to look at it in reverse.
You first need to query your database so that you have a mapping of which field associates with which ID.
Then, when your information is posted, you can iterate over that mapping, detect if they have been posted, and use them accordingly:
$mapping = loadFieldNamesToFieldId();
/*
mapping should look something like:
$mapping = [
'title' => 1,
'type' => 2
];
*/
foreach ($_POST as $field_name => $field_value) {
if (isset($mapping[$field_name]]) {
$id = $mapping[$field_name];
// at this point you know that the user submitted a field which
// had $field_value, and which ID it relates to in your database
}
}
At which point you can just format your form as so:
<input type="text" name="title" value="edison">
I found this answer and thought of posting this as this relates to the answer
foreach ($_POST as $param_name => $param_val)
{
echo "Param: $param_name; Value: $param_val<br />\n";
}

Codeigniter - How to set name property from database

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'].

Protect values after form submission codeigniter

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;
}
}
?>

How to send Jqxgrid in a form without using hidden textbox?

My CI view consists of a grid and data is added dynamically to this grid.This is saved to a database when a save button is clicked..
Here is the screenshot of the view.
[IMG]http://i40.tinypic.com/16a7dhl.png[/IMG]
When I submit the form,Grid data is first stored in an array and then array elements are joined together with a seperator in between them into a string.This string is stored in a hidden textbox and is submitted along with the form.After submission,in the controller they are seperated again & stored in the database.I have read that this method is prone to error.
Is there a better way to send array of data in a table to the controller than the above method? I have used Jqxgrid.
Use the jqxGrid's "getrows" to get all records as an Array.
You can use an array for the names of the fields.
<input type="hidden" name="field_name[]" value="foo">
<input type="hidden" name="field_name[]" value="bar">
$post = $this->input->post();
extract($post);
foreach($field_name as $k => $v){
$this->db->insert('tablename', array('fieldname' => $v));
}

Insert DB Value into Form Field

The Question: How do I insert values from a database table (#__mytable) into form text fields (motitle and modescription) which have been rendered from an XML file within the Joomla 3.0 platform?
-
I've been trying for days to solve this "easy" Joomla! based undocumented challenge.
I have followed Joomla!'s guide for Developing an MVC, read most of their out-of-date documentation and torn apart the com_content component but have still no idea how to populate my fields.
I've been playing with $this->form->bind($this->item);.
Below I have included some of my code to show the structure I am using. Please feel free to point out any issues you spot along the way.
Models\Forms\item.xml
<?xml version="1.0" encoding="UTF-8"?>
<form>
<fields name="groupOPTIONS">
<fieldset name="Options">
<field
type="text"
name="motitle"
id="motitle"
label="Title"
description="MY TEXT FIELD DESCRIPTION"
maxLength="255" />
<field
type="textarea"
name="modescription"
id="modescription"
label="Description"
description="MY TEXT FIELD DESCRIPTION"
rows="15"
cols="5"
maxLength="255" />
</fieldset>
</fields>
</form>
Models\item.php
jimport('joomla.application.component.modelitem');
class MagicObjectsModelItem extends JModelForm {
public function getForm($data = array(), $loadData = true) {
// Get the form 'items'
$form = $this->loadForm('com_magicobjects.item', 'item',
array('control' => 'jform', 'load_data' => $loadData));
if (empty($form)) {
return false;
}
return $form;
}
protected function loadFormData() {
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_magicobjects.item.edit.data', array());
if (empty($data)) {
$data = $this->getDBItem(1);
}
return $data;
}
public function getDBItem($pk) {
//Obtain JDatabase static connection
$oDb = JFactory::getDbo();
$oQuery = $oDb->getQuery(true);
$sValueToMatch = $pk;
$oQuery
->select(array('mid', 'name', 'keyword', 'description'))
->from('#__mytable')
->where('mid = "' . $sValueToMatch . '"')
->order('mid ASC');
$oDb->setQuery($oQuery);
return $oDb->loadObjectList();
}
views\item\view.html.php
jimport('joomla.application.component.view');
function display($tpl = null) {
// Initialise variables.
$this->form = $this->get('Form');
$this->item = $this->get('Item');
//Display the view
parent::display($tpl);
}
Views\item\tmpl\default.php
foreach ($this->form->getFieldset('Options') as $field) {
echo $field->label;
echo $field->input;
}
By performing a print_r() on item I can see I have the data, but I need to insert the data into the fields shown.
In case this it's still a problem because the form definition is defining a <fields /> group.
This mean that the form fields will be output as follows:
input type="text" name="[groupOPTIONS][motitle]"
based on the example you give above. If you remove the fields grouping it will probably work. It's annoying though as certain JForm methods only work on groups...
You would probably need to change the way in which the data was being passed into the form if you wanted to keep the fields grouping (e.g. by overloading getItem).
Hope that makes sense...
In Joomla 3, the JForm::bind() method appears to accept either an object or associative array as a parameter. All of the object/array fields are then stored in a protected JRegistry type data member called JForm::$data.
When you're trying to display a form field (by calling JForm::getInput()), the call stack is as follows
JForm::getInput() -> JForm::getField() -> JForm::loadField() -> JForm::getValue()
JForm::getValue() returns a value from the (aforementioned JRegistry) JForm::$data data member. The JForm::loadField() method also passes the default value (defined by the form) to the JForm::getValue() method in case a value in the JForm::$data variable doesn't exist.
In terms of doing this inside a model, you might want to generate a object or assoc array from a database query or table (ensuring that the field names correspond with the field names defined in the form xml) and then pass it to JForm::bind() as a parameter. However, if you are using JModelForm, I think you should only override JModelForm::loadFormData() to pass the object/assoc array to the loaded form.
See : libraries/joomla/form/form.php
Hope that helps :)
Instead of $oDb->loadObjectList(); use $oDb->loadAssoc(); in your getDBItem() function.
Check this - http://docs.joomla.org/Accessing_the_database_using_JDatabase/1.5
Check this also - joomla loadformdata
Hope this will work.

Categories