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));
}
Related
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";
}
Ok first I have a "settings" table on my database in which I have the fields "name" and "value" its a configuration kind of table where the value could be anything from string to boolean values etc.
Now on my blade, I have a form with various inputs "texts" "selects" "checkboxes" etc. When submitting the form on the controller I run a foreach where for each attribute of the $request I store its key as the name and its value as its value on the database.
$agency_id = Auth::user()->agency->id;
$settings = AgencySettings::whereAgencyId($agency_id)->get();
foreach ($request->except('_token') as $key => $value)
{
$setting = $settings->where('name','=',$key)->first();
if (boolval($setting))
{
$setting->value = $value;
$setting->update();
}else{
$setting = new AgencySettings;
$setting->agency_id = $agency_id;
$setting->name = $key;
$setting->value = $value;
$setting->save();
}
}
All works well except the unchecked checkboxes which are not inside the $request.
I know I can handle them like so $request->has('name_of_checkbox') but because of the dynamic nature of the table on the database, I don't want to have hardcoded on my Controller the name of a specific setting.
My goal is that the code on my Controller will be the same regardless the number of different settings I will use on my frontend (maybe in the future there will be a need to add more).
So my question, is there a way to handle those checkboxes serverside without having to refer to them specifically, or a way to always return the value of the checkboxes to the server despite its state?
My first thought is to go with javascript and hidden inputs, but maybe there is a better way.
You could add a hidden field with the same name before every checkbox you want to receive, like :
<input type="hidden" name="checkbox-1" value="0" />
<input type="checkbox" name="checkbox-1" value="1" /> My checbox 1
That will send the hidden field with the 0 value when the field is unchecked and send the proper truthy value when it's checked.
NOTE: Just make sure you're adding the hidden field first so you'll receive just the checked one when the field is checked.
Other solution is to simply check if value for "checkbox-1" is set in post array.
So you will set default to 0 on your controller side and check if value exists instead of checking if it is 0 or 1.
(M.)
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
I am trying to use Magento 1.9 XmlConnect module to save the billing address function. In xml connect there is an action to do this, saveBillingAddressAction. In savebillingaddressAction method one line is trying to access an array from the POST variables like following -
$data = $this->getRequest()->getPost('billing', array());
How I can pass an array from client side to server side in POST variable so that billing param have the array with the needed data?
Magento repository - CheckoutController.php.
You can create arrays out of form elements using square brackets [].
<input type="hidden" name="billing[]" value="billing-info1">
<input type="hidden" name="billing[]" value="billing-info2">
<input type="hidden" name="billing[]" value="billing-info3">
This will return a zero based array (ie 0=>'billing-info1',1=>'billing-info2', etc).
If you'd like to use an associative array you just need to create a key:
<input type="hidden" name="billing[key0]" value="billing-info1">
<input type="hidden" name="billing[key1]" value="billing-info2">
<input type="hidden" name="billing[key2]" value="billing-info3">
Then your return will be something like:
'key0' => 'billing-info1',
'key1' => 'billing-info2',
'key2' => 'billing-info3'
I ended up using a session array and storing data there so that I can reference it again later. I just added my post data from each form into this array and referenced it later in my else block. Thanks for the help!
I am using CodeIgniter for a school project. I have some experience with PHP but am relatively new to this framework. I am having trouble using two forms on one page.
I have one form that displays a dropdown of artists. After clicking the submit button for this form, it updates the second form (another dropdown) on the same page with the portfolios belonging to the artist selected in the first dropdown.
I am trying to echo the values from each form just for testing purposes right now, I will implement other features later. My issue is that after my second form is submitted, the post value for the first form is changed. If I echo the selected value of the first form before submitting the second form, it shows the value that was selected. If I echo the value of the first form after both forms have been submitted, it shows the first available value from that dropdown.
I need to be able to take the values from both of these forms and then use them later after both forms have been submitted. So I can't have the values changing right when I need to use them, obviously, any help would be appreciated. Thank you much.
Controller
public function formtest(){
//Making a call to the model to get an array of artists from the DB
$data = $this->depot_model->get_artists_list();
$this->form_validation->set_rules('artist', 'Artist');// Commenting this out for now, 'required');
$this->form_validation->set_rules('ports', 'Portfolios', 'required');
if ($this->form_validation->run() == FALSE)
{
//Building the artists dropdown form
$data['data'] = form_open('formtest', 'class="superform"')
. form_label('Artist<br/>', 'artist')
. form_dropdown('artist', $data)
. form_submit('mysubmit', 'Submit')
. form_close();
//Setting up a temp array of the selected artist's portfolios
$ports = $this->depot_model->get_portfolios(url_title($data[$this->input->post('artist')]));
//Culling out the names of the portfolios from my temp array
$newdata = array();
foreach($ports as $port){array_push($newdata, $port['name']);}
//Building the artist's portfolio dropdown
$newdata['data'] = form_open('formtest', 'class="superform"')
. form_label('Portfolios<br/>', 'ports')
. form_dropdown('ports', $newdata)
. form_submit('mysubmit', 'Submit')
. form_close();
//Send the information to my view
$this->load->view('formtest', $data);
$this->load->view('formtest', $newdata);
}
else
{
//This echos out the first available value from my dropdown rather than the one I selected.
echo $data[$this->input->post('artist')];
echo "success";
}
}
The forms are separate, so when the second gets submitted, there is in effect no value received from the first form, as it isn't included as a field in the second. So you can do that, include say a hidden field in the second form that has the value of the artist. eg:
$newdata['data'] = form_open(
'formtest',
'class="superform"',
array('artist' => $this->input->post('artist'))
);