Joomla - ignored form fields with $_POST - forms

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

Related

Form button not working properly in IE11(and probably more)

I've made page where you can get an overview over invoices.
Everything works great, except from with Internet Explorer...
When I click the see order button, nothing happens in IE.
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" onSubmit="window.location.reload()">
<tr><td><?php
$Date = $row['date'];
echo $Date;
?></td><td><?php echo $row['autoid'];?></td>
<input name='implodehideu' type='hidden' value='<?php echo $row['implodeuke'];?>;'>
<input name='implodehidew' type='hidden' value='<?php echo $row['implodehelg'];?>;'>
<input name='personid' type='hidden' value='<?php echo $row['primeid'];?>;'>
<input name='invoiceid' type='hidden' value='<?php echo $row['autoid'];?>'>
<td><input type="submit" name="seeorder" id="submit" value="Order overview"></td>
<td><?php echo $row['firstname'] . " " . $row['surname'];?></td>
<td><?php echo $row['amount'];?></td>
<?php
if ($paidboolean)
echo "<td bgcolor='#3AA849'>YES</td>";
else
echo "<td bgcolor='#e94336'>NO</td>";
if (!$paidboolean) {
?>
<td><input type="submit" name="confirm" id="submit" value="Mark as paid"></td>
<?php
} else {
?>
<td><input type="submit" name="notconfirm" id="submit" value="Mark as not paid"></td>
<?php
}
?>
<td><input type="submit" name="slett" id="submit" value="Slett"></td>
</form>
<?php
if (isset($_POST['seeorder'])) {
.....Things to show up
}
Try to set the id argument of the submit button to something else than 'submit'. That helped me with the same problem.
<input type="submit" name="slett" id="btnSubmit" value="Slett" />

Form functions contain

How I can send a form using functions php?
Example
function xxx1() { echo '<input type="text" name="xxx1" />'; }
function xxx2() { echo '<input type="text" name="xxx2" />'; }
PHP code
<form method="POST" action="">
<?php
xxx1();
xxx2();
?>
</form>
It that possible?
Add php tags around the functions.
I tested your code in the following matter and everything is working correctly.
<?php
function xxx1(){ echo '<input type="text" name="xxx1" />'; }
function xxx2(){ echo '<input type="text" name="xxx2" />'; }
?>
<form method="POST" action="">
<?php
xxx1();
xxx2();
?>
</form>

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
}

Repopulating hidden fields with CodeIgniter Form Validation class

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

mark the input box red if the required field of a form is not filled

i'm using Codeigniter. i have a form which takes information about users. What i want to do is to check whether all the required fields are filled up or not.If any of the required fields is not filled up i want mark that input box red. Right now my codes only check if the required fields are filled up or not. if not it says the "field is required" but how to mark input box.I'm a bit confused how to do this thing.Can somebody help me out with a little hint. Thanks.
the view for my form:
<?php
$attributes = array('class' => '', 'id' => '');
echo form_open('register', $attributes); ?>
<p>
<label for="name">Name <span class="required">*</span></label>
<?php echo form_error('name'); ?>
<br /><input id="name" type="text" name="name" value="<?php
echo set_value('name'); ?>"
</p>
<p>
<label for="username">Username <span class="required">*</span></label>
<?php echo form_error('username'); ?>
<br /><input id="username" type="text" name="username" value="<?php echo set_value('username'); ?>"
</p>
<p>
<label for="password">Password <span class="required">*</span></label>
<?php echo form_error('password'); ?>
<br /><input id="password" type="password" name="password" value="<?php echo set_value('password'); ?>"
</p>
<p>
<label for="email">Email <span class="required">*</span></label>
<?php echo form_error('email'); ?>
<br /><input id="email" type="text" name="email" value="<?php echo set_value('email'); ?>"
</p>
<p>
<label for="phone">Phone</label>
<?php echo form_error('phone'); ?>
<br /><input id="phone" type="text" name="phone" value="<?php echo set_value('phone'); ?>"
</p>
</p>
<p>
<input type="submit" value="Submit information" class="formbutton"/>
</p>
<?php echo form_close(); ?>
Generally, I'd recommend creating an textInputError class where you adjust the input style, and then apply it based on the existence of the error...
class="<?php echo (form_error('username') ? 'textInputError' : '') ?>"
in place in the input element...
<input id="username" type="text" name="username" value="<?php echo set_value('username'); ?>" class="<?php echo (form_error('username') ? 'textInputError' : '') ?>">