Repopulating hidden fields with CodeIgniter Form Validation class - forms

I am stuck how to repopulate a hidden field in a form after an unsuccessful form validation.
The repopulation uses set_value() function to print out the chosen value for other fields, but with the hidden field, there is already something printed in the value field.
Here is my view code:
<? echo validation_errors();?>
<? echo form_open('projects/start');?>
<input type="hidden" name="project_type_id" value="<? echo $this->uri->segment(3);?>" >
<input type="text" name="site" value="<?echo set_value('site');?>">
<input type="submit" value="submit">
<?echo form_close();?>

Pass the URI segment value in second parameter, after submit the form it will take the submitted value, It repopulate submitted value not URI segment value.
<?php echo validation_errors(); ?>
<?php echo form_open('projects/start'); ?>
<input type="hidden" name="project_type_id" value="<?php echo set_value('project_type_id', $this->uri->segment(3)); ?>" />
<input type="text" name="site" value="<?php echo set_value('site'); ?>" />
<input type="submit" value="submit" />
<?php echo form_close(); ?>
Hope this helps you. Thank you.

Taking a guess here that you want to fill it with the URI segment when it first loads, and that this is not available once the form submits.
Perhaps this would work
<input type="hidden" name="project_type_id" value="<? echo ($this->input->post())? set_value('project_type_id'): $this->uri->segment(3);?>" >
Depending on how you built your site it might also be possible to do
<? echo form_open('projects/start/' . $this->uri->segment(3));?>
To keep the URI

Related

Can I write an input element, what is posted (or getted) in every form?

I have many different forms in my page, all of them reloads the same page, but there is one input, which is always needed for the page to run.
Can I somehow set an input to send with all the forms on the page?
<input type='hidden' id='user' name='user' value="<?php echo $user; ?>">
<input type='hidden' id='site' name='site' value="<?php echo $defaultsite; ?>">
<form action='main.php' method='post'>
<button class='button1'>Back</button>
</form>
<form action='over_store.php' method='post'>
<button class='button1'>Overstore</button>
</form>
<form action='avg_resources.php' method='post'>
<button class='button1'>Avarage stored value</button>
</form>
<form action='avg_decreases.php' method='post'>
<button class='button1'>Avarage product decreasing</button>
</form>
I know, I can add the 'site' and 'user' input to all the forms, but is there any siplier solution? Can I post those two inputs with all the forms?

How to set value 0 (zero) in input?

I made ​​a simple form to arrange the structure of the html code.
I got a problem when I want to make the number 0 in the input tag value without clear it after submit.
value on this input can be changed, but I want to set the default to be 0 = This will be 0px;
as an example -> style="padding: px px px px" it is the wrong structure, this should be -> style="padding: 0px 0px 0px 0px"
I make the value to be like this.
this will prevent the value lost after submitted.
how do i give value 0, but does not remove this feature?
//Edited, It just a simple for example://
<?php
echo "style='padding:".$ul_padding_top.$ul_pd_px." ".$ul_padding_right.$ul_pd_px." ".$ul_padding_bottom.$ul_pd_px." ".$ul_padding_left.$ul_pd_px.";'";
?>
<form action="/" method="post">
<input type="text" name="ul_padding_top" value="<?php echo isset($_POST['ul_padding_top']) ? $_POST['ul_padding_top'] : '' ?>" />px = padding-top<br/>
<input type="text" name="ul_padding_right" value="<?php echo isset($_POST['ul_padding_right']) ? $_POST['ul_padding_right'] : '' ?>" />px = padding-right<br/>
<input type="text" name="ul_padding_bottom" value="<?php echo isset($_POST['ul_padding_bottom']) ? $_POST['ul_padding_bottom'] : '' ?>" />px = padding-bottom<br/>
<input type="text" name="ul_padding_left" value="<?php echo isset($_POST['ul_padding_left']) ? $_POST['ul_padding_left'] : '' ?>" />px = padding-left<br/>
<input class="foo_ul_pd" type="hidden" name="ul_pd_px" value="px" <?php if(isset($_POST['ul_pd_px'])) echo "checked='checked'"; ?> /> padding value "px"<br/>
<input class="button" type="submit" value="Generate!">
</form>
I think your looking for
<input class="foo" type="checkbox" name="padding" checked />
Or you could use Jquery
$('.foo').prop('checked',true);
And to read it with Jquery can do
$('.foo').prop('checked');
EDIT:
Just re-read your description you say you wish to put the value 0 in a field you will need to change type="checkbox" to type="text" at which point you can do value="0"
EDIT 2:
This won't work
<input class="foo_ul_pd" type="hidden" name="ul_pd_px" value="px" <?php if(isset($_POST['ul_pd_px'])) echo "checked='checked'"; ?> />
a input type="hidden" is basically a type="text" so you would just need to use
<input class="foo_ul_pd" type="hidden" name="ul_pd_px" value="0" />

Joomla - ignored form fields with $_POST

I've been trying to create a joomla form in my view. But when I use the "apply" function and try to retrieve information from the $_POST global I lack two out of three fields. I can't for my life figure out what's wrong.
Since I get one of the fields I assume that I've created the form in a somewhat Joomla fashion.
<form action='<?php echo JRoute::_('index.php?option=com_mycomponent'); ?>' method='post' name="adminForm">
<fieldset><legend><?php echo JText::_('COM_MYCOMPONENT_SETTING_LEGEND'); ?></legend>
<label for=' <?php echo JText::_('COM_MYCOMPONENT_FORM_NAME'); ?>'> <?php echo JText::_('COM_MYCOMPONENT_USE'); ?>: </label>
<select name=' <?php echo JText::_('COM_MYCOMPONENT_FORM_NAME'); ?>' id=' <?php echo JText::_('COM_MYCOMPONENT_FORM_NAME'); ?>'>
<option value='0'> <?php echo JText::_('COM_MYCOMPONENT_NO')?></option>
<?php
if ($this->m_use_osn)
echo "<option value='1' selected>";
else
echo "<option value='1'>";
?>
<?php echo JText::_('COM_MYCOMPONENT_YES')?></option>
</select>
<br />
<label for='<?php echo JText::_('COM_MYCOMPONENT_URL_FORM_NAME'); ?>'> <?php echo JText::_('COM_MYCOMPONENT_URL'); ?>: </label>
<?php
if (!empty($this->m_osn_url))
echo "<input type='text' value='".$this->m_osn_url."' /><br />";
else
echo "<input type='text' placeholder='".JText::_('COM_MYCOMPONENT_URL_PLACEHOLDER')."' /><br /> ";
?>
<label for='<?php echo JText::_('COM_MYCOMPONENT_KEY_FORM_NAME'); ?>'> <?php echo JText::_('COM_MYCOMPONENT_KEY'); ?>: </label>
<?php
if (!empty($this -> m_osn_key))
echo "<input type='text' value='" . $this -> m_key . "' /><br /> ";
else
echo "<input type='text' placeholder='".JText::_('COM_MYCOMPONENT_KEY_PLACEHOLDER')."' /><br />";
?>
<input type="hidden" name="task" value="" />
<input type='submit' value=' <?php echo JText::_('COM_MYCOMPONENT_SUBMIT'); ?> />
</fieldset>
</form>
However, what a var_dump($_POST) gives me is this:
array(3) {
["mycomponent_use"]=>
string(1) "0"
["task"]=>
string(5) "apply"
["option"]=>
string(14) "com_mycomponent"
}
So, for some reason I'm not getting any of the text input fields. I've tried to get the data through JFactory::getApplication()->input... as well, for some reason it ignore my <input type='text' />. Do I have to register these in the $_POST global in some way? Since I cannot use a regular submit button but must depend on the joomla bar I am getting a bit frustrated with getting a semi empty $_POST.
You have forgot to give the name of input fields in form. Please assign the distinct name for each field.You may refer this.
Dealing with Forms

Codeigniter update mysql table data from form with checkbox

New to codeigniter and trying to get my head around updating checked rows from a user form.
My view generates a form with MySQL data as below:
<?php
echo form_open('masterdata/update_customers');
?>
<table>
<tr>
td> </td><td>Customer Name</td><td>postalcode</td>
<tr>
<?php if(isset($records)) : foreach ($records as $row) : ?>
<tr>
<td>
<input type=checkbox name="editcustomer[]" id="editcustomer[]" value="<?php echo $row->id ?>">
</td>
<td>
<input type="text" name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" >
</td>
<td>
<input type="text" name="postalcode_<?php echo $row->id ?>" id="postalcode_<?php echo $row->id ?>" value="<?php echo $row->postalcode ; ?>" >
</td>
</tr>
<?php endforeach ; ?>
</table>
<input type="submit" value="Update Selected">
<?php else : ?>
<h2> No Records Found</h2>
<?php endif; ?>
<?php echo form_close(); ?>
This works perfectly well as I get my unique name and values for all input fields.
My issue is now trying to pass the selected checkboxes and there values to codeigniter and get it to update each row.
traditionally I would use foreach($_POST['editcustomer'] as $editcustomer){ but cant get my head around this in codeigniter.
my controller function, update_customers at this stage is very basic:
function update_customers()
{
$this->form_validation->set_rules("customer_name","`Customer Name`","required|min_length[6]|xss_clean");
$this->form_validation->set_rules("postalcode","`Postal Code`","required|xss_clean|min_length[6]");
if ($this->form_validation->run() == FALSE){
$data["message"]="";
$data['title']="Master Data Home Page";
$this->load->view("master_data/view_master_data_header",$data);
$this->load->view("master_data/view_master_data_nav");
$this->load->view("master_data/view_content_master_data_manage_customers");
$this->load->view("master_data/view_master_data_footer");
} else {
$data = array(
'customer_name' => $this->input->post('customer_name'),
'postalcode' => $this->input->post('postalcode'),
);
$this->model_master_data->update_customer_records($data);
$this->customers_updated();
}
}
My model function, update_customer_records is:
function update_customer_records($data)
{
$this->db->where('id',$this->input->post('id'));
$this->db->update('customers',$data);
}
I know there is quite a bit missing here, like processing only the rows checked. but not sure how to add this in. secondly, my unique name and id's being generated, as per my view is:
name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>"
how do get the update to apply to the unique form input name? I need to append the prefix customername_ and postalcode_ to the input->post(?) value?
Any direction or assistance here will be appreciated. thanks a million in advance.
With CodeIgniter, if you have to get checkbox values, you have to use $this->input->post('name_of_your_checkbox'). It's return an array.
For example, if you have that in a view:
<input type="checkbox" name="editcustomer[]" value="1" />
<input type="checkbox" name="editcustomer[]" value="2" />
<input type="checkbox" name="editcustomer[]" value="3" />
From the controller:
public function update_customers() {
$editcustomer = $this->input->post('editcustomer');
}
Return:
Array {
[0] => 1
[1] => 2
[2] => 3
}

Codeigniter Form Helper Duplicate from_hidden?

Im totally stumped.. Im creating some form elements using the CI form helper and for some weird reason, its creating a duplicate version.
Here is my PHP
<div id="receiveInventoryItemDetails">
<p><?php echo form_open('#', array("class" => "nyroModal form label-inline"));?></p>
<?php echo form_hidden('item_id', '', "readonly = true"); ?>
<?php echo form_hidden('purchase_order_id', '', "readonly = true"); ?>
<p><?php echo form_label('Item Name', 'item_name');?><?php echo form_input('item_name', '', "readonly = true"); ?></p>
<p><?php echo form_label('Item QTY', 'item_qty');?><?php echo form_input('item_qty', ''); ?></p>
<?php echo form_close();?>
</div>
<div class="buttonrow">
<button class="btn-sec" onclick="inventoryC.receiveSubmitItem();"><span>Add To Inventory</span></button>
</div>
Here is the HTML output
<div id="receiveInventoryItemDetails">
<p><form action="https://mysite.com/#.abl" method="post" accept-charset="utf-8" class="nyroModal form label-inline"></p>
<input type="hidden" name="item_id" value="" />
<input type="hidden" name="item_id" value="" />
<input type="hidden" name="purchase_order_id" value="" />
<p><label for="item_name">Item Name</label><input type="text" name="item_name" value="" readonly = true /></p>
<p><label for="item_qty">Item QTY</label><input type="text" name="item_qty" value="" /></p>
</form>
</div>
<div class="buttonrow">
<button class="btn-sec" onclick="inventoryC.receiveSubmitItem();"><span>Add To Inventory</span></button>
</div>
You can't use html attributes via third parameter. Look at form helper source code
This should be work:
<?php echo form_hidden('item_id', ''); ?>